Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement core functionality #11

Merged
merged 27 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/recommended"],
"extends": ["plugin:github/recommended", "plugin:github/typescript"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
"project": "./tsconfig.eslint.json"
},
"rules": {
"i18n-text/no-en": "off",
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": [
"error",
{"accessibility": "no-public"}
],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": [
"error",
{"allowExpressions": true}
],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
"filenames/match-regex": ["error", "^[a-z0-9-]+(.d|.config|.test)?$"]
},
"env": {
"node": true,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# `index.js` is the code that will run.
# For our project, we generate this file through a build process from other source files.
# We need to make sure the checked-in `index.js` actually matches what we expect it to be.
name: ci
name: Check dist

on:
push:
Expand All @@ -17,7 +17,7 @@ on:
workflow_dispatch:

jobs:
check-dist:
build:
runs-on: ubuntu-latest

steps:
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: ci
on:
pull_request:
push:
branches:
- main
- 'releases/*'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm install
- run: npm run all

test:
runs-on: ubuntu-latest
env:
SLACK_DEPLOY_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN_FOR_TESTING }}
SLACK_DEPLOY_CHANNEL: 'C040YVCUDRR' #github_action_testing
steps:
- uses: actions/checkout@v3
- name: Post to Slack
uses: ./
id: slack
- uses: ./
with:
thread_ts: ${{ steps.slack.outputs.ts }}
conclusion: true
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: ci
name: CodeQL

on:
push:
Expand All @@ -21,7 +21,7 @@ on:
- cron: '31 7 * * 3'

jobs:
codeql:
analyze:
name: CodeQL
runs-on: ubuntu-latest
permissions:
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/test.yml

This file was deleted.

7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": ["mrkdwn", "namoscato"],
"files.exclude": {
"dist": true,
"lib": true
}
}
7 changes: 3 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
MIT License

The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors
Copyright (c) 2022 Nick Amoscato (namoscato)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
178 changes: 85 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,109 +1,101 @@
<p align="center">
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
</p>
[![ci](https://github.com/namoscato/action-slack-deploy-pipeline/actions/workflows/ci.yml/badge.svg)](https://github.com/namoscato/action-slack-deploy-pipeline/actions/workflows/ci.yml)

# Create a JavaScript Action using TypeScript
# Slack Deploy Pipeline Notifications

Use this template to bootstrap the creation of a TypeScript action.:rocket:
Post [GitHub Action](https://github.com/features/actions) deploy workflow progress notifications to [Slack](https://slack.com/).

This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.
<img width="487" alt="Slack Deploy Pipeline Notifications example thread" src="https://user-images.githubusercontent.com/847532/189536394-f5b231ce-27ee-4d4d-8c87-3a59743c8f38.png">

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)
## Setup

## Create an action from this template
1. [Create a Slack App](https://api.slack.com/apps) for your workspace
1. Under **OAuth & Permissions**, add two Bot Token Scopes:
1. [`chat:write`](https://api.slack.com/scopes/chat:write) to post messages
1. [`chat:write.customize`](https://api.slack.com/scopes/chat:write.customize) to customize messages with GitHub username and avatar
1. Install the app to your workspace
1. Copy the app's **Bot User OAuth Token** from the **OAuth & Permissions** page
1. [Create a GitHub repository secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) with this token, named `SLACK_DEPLOY_BOT_TOKEN`
1. Invite the bot user into the Slack channel you will post messages to (`/invite @bot_user_name`)
1. Click the Slack channel name in the header, and copy its **Channel ID** from the bottom of the dialog

Click the `Use this Template` and provide the new repo details for your action
## Usage

## Code in Main

> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance.

Install the dependencies

```bash
$ npm install
```

Build the typescript and package it for distribution

```bash
$ npm run build && npm run package
```

Run the tests :heavy_check_mark:

```bash
$ npm test

PASS ./index.test.js
✓ throws invalid number (3ms)
✓ wait 500 ms (504ms)
✓ test runs (95ms)

...
```

## Change action.yml

The action.yml defines the inputs and output for your action.

Update the action.yml with your name, description, inputs and outputs for your action.

See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)

## Change the Code

Most toolkit and CI/CD operations involve async operations so the action is run in an async function.

```javascript
import * as core from '@actions/core';
...

async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}

run()
```

See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.

## Publish to a distribution branch

Actions are run from GitHub repos so we will checkin the packed dist folder.

Then run [ncc](https://github.com/zeit/ncc) and push the results:

```bash
$ npm run package
$ git add dist
$ git commit -a -m "prod dependencies"
$ git push origin releases/v1
```yaml
name: Deploy

on:
push:
branches:
- main

# 1. Configure required environment variables
env:
SLACK_DEPLOY_BOT_TOKEN: ${{ secrets.SLACK_DEPLOY_BOT_TOKEN }}
SLACK_DEPLOY_CHANNEL: 'C040YVCUDRR' # replace with your Slack Channel ID

jobs:
staging:
runs-on: ubuntu-latest
outputs:
slack_ts: ${{ steps.slack.outputs.ts }}
steps:
# 2. Post summary message at the beginning of your workflow
- name: Post to Slack
uses: namoscato/action-slack-deploy-pipeline@v1
id: slack

- name: Deploy to staging
run: sleep 10 # replace with your deploy steps

# 3. Post threaded stage updates throughout
- name: Post to Slack
uses: namoscato/action-slack-deploy-pipeline@v1
if: always()
with:
thread_ts: ${{ steps.slack.outputs.ts }}

production:
needs:
- staging
runs-on: ubuntu-latest
steps:
- name: Deploy to production
run: sleep 5 # replace with your deploy steps

# 4. Post last "conclusion" stage
- name: Post to Slack
uses: namoscato/action-slack-deploy-pipeline@v1
if: always()
with:
thread_ts: ${{ needs.staging.outputs.slack_ts }}
conclusion: true
```

Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project.
1. Configure required `SLACK_DEPLOY_BOT_TOKEN` and `SLACK_DEPLOY_CHANNEL` [environment variables](https://docs.github.com/en/actions/learn-github-actions/environment-variables).
1. Use this action at the beginning of your workflow to post a "Deploying" message in your configured channel.
1. As your workflow progresses, use this action with the `thread_ts` input to post threaded replies.
1. Denote the last step with the `conclusion` input to update the initial message's status.

Your action is now published! :rocket:
## Environment Variables

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
Both environment variables are _required_.

## Validate
| variable | description |
| ------------------------ | -------------------------- |
| `SLACK_DEPLOY_BOT_TOKEN` | Slack Bot User OAuth Token |
| `SLACK_DEPLOY_CHANNEL` | Slack Channel ID |

You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml))

```yaml
uses: ./
with:
milliseconds: 1000
```
## Inputs

See the [actions tab](https://github.com/actions/typescript-action/actions) for runs of this action! :rocket:
| input | description |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `thread_ts` | Initial Slack message timestamp ID |
| `conclusion` | `true` denotes last stage |
| `github_token` | Repository `GITHUB_TOKEN` or personal access token secret; defaults to [`github.token`](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context) |
| `status` | The current status of the job; defaults to [`job.status`](https://docs.github.com/en/actions/learn-github-actions/contexts#job-context) |

## Usage:
## Outputs

After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
| output | description |
| ------ | -------------------------- |
| `ts` | Slack message timestamp ID |
Loading