From e01303e47ee2778a9b011bcb6cd6f870ae7cf9e8 Mon Sep 17 00:00:00 2001 From: Kamaz Date: Sat, 15 Feb 2020 13:30:19 +0000 Subject: [PATCH 1/4] docs: documentation adn tests --- .github/workflows/test.yml | 10 +++ README.md | 141 +++++++++---------------------------- dist/index.js | 2 + docs/develop.md | 117 ++++++++++++++++++++++++++++++ src/main.ts | 3 + 5 files changed, 165 insertions(+), 108 deletions(-) create mode 100644 docs/develop.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5f1790e..ec5ce36 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,3 +29,13 @@ jobs: token: ${{secrets.GITHUB_TOKEN}} environmentUrl: https://github.com/features/actions state: success + incorrect-deployment-id: # make sure the action works on a clean machine without building + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + id: deployment + with: + deploymentId: invalid.deployment + token: ${{secrets.GITHUB_TOKEN}} + environmentUrl: https://github.com/features/actions diff --git a/README.md b/README.md index 95d9668..cad63fc 100644 --- a/README.md +++ b/README.md @@ -1,117 +1,42 @@ -

- typescript-action status -

+# deploy action -# Create a JavaScript Action using TypeScript +Action is a wrapper around deployment api. -Use this template to bootstrap the creation of a JavaScript action.:rocket: +## Inputs -This template includes compilication support, tests, a validation workflow, publishing, and versioning guidance. +| Output | Value | +|-----|-----| +| token | github token | -If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action) +## Outputs -## Create an action from this template +| Output | Value | +| --- | --- | +| deploymentId | a deployment number that can be used to set status later | -Click the `Use this Template` and provide the new repo details for your action - -## Code in Master - -Install the dependencies -```bash -$ npm install -``` - -Build the typescript -```bash -$ npm run build -``` - -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 contains 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. We will create a releases branch and only checkin production modules (core in this case). - -Comment out node_modules in .gitignore and create a releases/v1 branch -```bash -# comment out in distribution branches -# node_modules/ -``` - -```bash -$ git checkout -b releases/v1 -$ git commit -a -m "prod dependencies" -``` - -```bash -$ npm prune --production -$ git add node_modules -$ git commit -a -m "prod dependencies" -$ git push origin releases/v1 -``` - -Your action is now published! :rocket: - -See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) - -## Validate - -You can now validate the action by referencing the releases/v1 branch - -```yaml -uses: actions/typescript-action@releases/v1 -with: - milliseconds: 1000 -``` - -See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket: - -## Usage: - -After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and tested action +## Example usage ```yaml -uses: actions/typescript-action@v1 -with: - milliseconds: 1000 +- uses: kamaz/deploy-action@v1.0 + id: deployment + with: + token: ${{ secrets.GITHUB_TOKEN }} + environmentUrl: ${{ format('https://pr-{0}.example.com', github.event.number) }} +# +# Your deployment steps +# +- uses: kamaz/deploy-action@v1.0 + if: success() + with: + deploymentId: ${{ steps.deployment.outputs.deploymentId }} + token: ${{ secrets.GITHUB_TOKEN }} + environmentUrl: ${{ format('https://pr-{0}.example.com', github.event.number) }} + state: success +- uses: kamaz/deploy-action@v1.0 + if: failure() + with: + deploymentId: ${{ steps.deployment.outputs.deploymentId }} + token: ${{ secrets.GITHUB_TOKEN }} + environmentUrl: ${{ format('https://pr-{0}.example.com', github.event.number) }} + state: failure ``` diff --git a/dist/index.js b/dist/index.js index 3758e4e..8bf3ec7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2088,6 +2088,8 @@ function run() { try { const githubToken = core.getInput('token'); let deploymentId = core.getInput('deploymentId'); + core.debug(`Deployment id ${deploymentId}`); + core.info(`Deployment id ${deploymentId}`); const octokit = new github_1.GitHub(githubToken, {}); if (deploymentId === '') { const deploy = yield octokit.repos.createDeployment(createDeploymentPayload()); diff --git a/docs/develop.md b/docs/develop.md new file mode 100644 index 0000000..95d9668 --- /dev/null +++ b/docs/develop.md @@ -0,0 +1,117 @@ +

+ typescript-action status +

+ +# Create a JavaScript Action using TypeScript + +Use this template to bootstrap the creation of a JavaScript action.:rocket: + +This template includes compilication support, tests, a validation workflow, publishing, and versioning guidance. + +If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action) + +## Create an action from this template + +Click the `Use this Template` and provide the new repo details for your action + +## Code in Master + +Install the dependencies +```bash +$ npm install +``` + +Build the typescript +```bash +$ npm run build +``` + +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 contains 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. We will create a releases branch and only checkin production modules (core in this case). + +Comment out node_modules in .gitignore and create a releases/v1 branch +```bash +# comment out in distribution branches +# node_modules/ +``` + +```bash +$ git checkout -b releases/v1 +$ git commit -a -m "prod dependencies" +``` + +```bash +$ npm prune --production +$ git add node_modules +$ git commit -a -m "prod dependencies" +$ git push origin releases/v1 +``` + +Your action is now published! :rocket: + +See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) + +## Validate + +You can now validate the action by referencing the releases/v1 branch + +```yaml +uses: actions/typescript-action@releases/v1 +with: + milliseconds: 1000 +``` + +See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket: + +## Usage: + +After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and tested action + +```yaml +uses: actions/typescript-action@v1 +with: + milliseconds: 1000 +``` diff --git a/src/main.ts b/src/main.ts index fde9412..625ace7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -98,6 +98,9 @@ async function run(): Promise { const githubToken = core.getInput('token') let deploymentId = core.getInput('deploymentId') + core.debug(`Deployment id ${deploymentId}`) + core.info(`Deployment id ${deploymentId}`) + const octokit = new GitHub(githubToken, {}) if (deploymentId === '') { From c10fe6cbe3e8f7fff80ba56276eff28da305c72d Mon Sep 17 00:00:00 2001 From: Kamaz Date: Sat, 15 Feb 2020 22:05:40 +0000 Subject: [PATCH 2/4] fix: add is nan check --- dist/index.js | 10 +++++----- src/main.ts | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8bf3ec7..df11aa1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2076,7 +2076,7 @@ const createDeploymentStatusPayload = (deploymentId) => { return { owner, repo, - deployment_id: parseInt(deploymentId, 10), + deployment_id: deploymentId, state, description: 'this is pr', log_url: logUrl, @@ -2087,18 +2087,18 @@ function run() { return __awaiter(this, void 0, void 0, function* () { try { const githubToken = core.getInput('token'); - let deploymentId = core.getInput('deploymentId'); + let deploymentId = parseInt(core.getInput('deploymentId'), 10); core.debug(`Deployment id ${deploymentId}`); core.info(`Deployment id ${deploymentId}`); const octokit = new github_1.GitHub(githubToken, {}); - if (deploymentId === '') { + if (isNaN(deploymentId)) { const deploy = yield octokit.repos.createDeployment(createDeploymentPayload()); - deploymentId = `${deploy.data.id}`; + deploymentId = deploy.data.id; core.info(`Created deployment id: ${deploymentId}`); } const deploymentStatus = yield octokit.repos.createDeploymentStatus(createDeploymentStatusPayload(deploymentId)); core.info(`Created deployment status: ${deploymentStatus.data.id}`); - core.setOutput('deploymentId', deploymentId); + core.setOutput('deploymentId', `${deploymentId}`); } catch (error) { core.setFailed(error.message); diff --git a/src/main.ts b/src/main.ts index 625ace7..9401aa1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -76,7 +76,7 @@ const createDeploymentPayload = (): Octokit.ReposCreateDeploymentParams => { } const createDeploymentStatusPayload = ( - deploymentId: string + deploymentId: number ): Octokit.ReposCreateDeploymentStatusParams => { const {owner, repo} = deploymentContext() const state = core.getInput('state') as DeploymentState @@ -86,7 +86,7 @@ const createDeploymentStatusPayload = ( return { owner, repo, - deployment_id: parseInt(deploymentId, 10), + deployment_id: deploymentId, state, description: 'this is pr', log_url: logUrl, @@ -96,18 +96,18 @@ const createDeploymentStatusPayload = ( async function run(): Promise { try { const githubToken = core.getInput('token') - let deploymentId = core.getInput('deploymentId') + let deploymentId = parseInt(core.getInput('deploymentId'), 10) core.debug(`Deployment id ${deploymentId}`) core.info(`Deployment id ${deploymentId}`) const octokit = new GitHub(githubToken, {}) - if (deploymentId === '') { + if (isNaN(deploymentId)) { const deploy = await octokit.repos.createDeployment( createDeploymentPayload() ) - deploymentId = `${deploy.data.id}` + deploymentId = deploy.data.id core.info(`Created deployment id: ${deploymentId}`) } @@ -117,7 +117,7 @@ async function run(): Promise { core.info(`Created deployment status: ${deploymentStatus.data.id}`) - core.setOutput('deploymentId', deploymentId) + core.setOutput('deploymentId', `${deploymentId}`) } catch (error) { core.setFailed(error.message) } From ec6b978d43f9693891021b27b33748940c964681 Mon Sep 17 00:00:00 2001 From: Kamaz Date: Sat, 15 Feb 2020 22:12:12 +0000 Subject: [PATCH 3/4] feat: parameterize environments --- .github/workflows/test.yml | 2 ++ README.md | 6 ++++++ action.yml | 4 ++++ dist/index.js | 10 +++------- src/main.ts | 10 +++------- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec5ce36..59f5467 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,3 +39,5 @@ jobs: deploymentId: invalid.deployment token: ${{secrets.GITHUB_TOKEN}} environmentUrl: https://github.com/features/actions + environment: staging + state: failure diff --git a/README.md b/README.md index cad63fc..d3fa866 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ Action is a wrapper around deployment api. +https://developer.github.com/v3/repos/deployments/ + +https://developer.github.com/v3/repos/deployments/#create-a-deployment + +https://developer.github.com/v3/repos/deployments/#create-a-deployment-status + ## Inputs | Output | Value | diff --git a/action.yml b/action.yml index d8685f7..2f773f1 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,10 @@ inputs: description: Auto merge required: false default: 'false' + environment: + description: Transient environment + required: false + default: 'qa' transientEnvironment: description: Transient environment required: false diff --git a/dist/index.js b/dist/index.js index df11aa1..caa824c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2043,7 +2043,7 @@ const deploymentContext = () => { } }; const createDeploymentPayload = () => { - const { login, owner, ref, repo } = deploymentContext(); + const { owner, ref, repo } = deploymentContext(); const requiredContext = core .getInput('requiredContext') .split(',') @@ -2051,17 +2051,13 @@ const createDeploymentPayload = () => { const autoMerge = core.getInput('autoMerge'); const transientEnvironment = core.getInput('transientEnvironment'); const productionEnvironment = core.getInput('productionEnvironment'); + const environment = core.getInput('environment'); return { owner, repo, ref, required_contexts: requiredContext, - payload: JSON.stringify({ - user: login, - environment: 'qa', - description: 'deploying my lovely branch' - }), - environment: 'qa', + environment, transient_environment: isTrue(transientEnvironment), auto_merge: isTrue(autoMerge), production_environment: isTrue(productionEnvironment) diff --git a/src/main.ts b/src/main.ts index 9401aa1..403036e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -49,7 +49,7 @@ const deploymentContext = (): DeploymentContext => { } const createDeploymentPayload = (): Octokit.ReposCreateDeploymentParams => { - const {login, owner, ref, repo} = deploymentContext() + const {owner, ref, repo} = deploymentContext() const requiredContext = core .getInput('requiredContext') .split(',') @@ -57,18 +57,14 @@ const createDeploymentPayload = (): Octokit.ReposCreateDeploymentParams => { const autoMerge = core.getInput('autoMerge') const transientEnvironment = core.getInput('transientEnvironment') const productionEnvironment = core.getInput('productionEnvironment') + const environment = core.getInput('environment') return { owner, repo, ref, required_contexts: requiredContext, - payload: JSON.stringify({ - user: login, - environment: 'qa', - description: 'deploying my lovely branch' - }), - environment: 'qa', + environment, transient_environment: isTrue(transientEnvironment), auto_merge: isTrue(autoMerge), production_environment: isTrue(productionEnvironment) From f6aa3680a9d36b6eb94f19077e31d29223e8b9e9 Mon Sep 17 00:00:00 2001 From: Kamaz Date: Sat, 15 Feb 2020 22:36:24 +0000 Subject: [PATCH 4/4] feat: document input/output --- README.md | 26 +++++++++++++++++--------- action.yml | 4 ++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d3fa866..5012fab 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,30 @@ Action is a wrapper around deployment api. -https://developer.github.com/v3/repos/deployments/ +For detail documentation and explanation refer to: -https://developer.github.com/v3/repos/deployments/#create-a-deployment - -https://developer.github.com/v3/repos/deployments/#create-a-deployment-status +- [GitHub Deployments API](https://developer.github.com/v3/repos/deployments/) +- [GitHub Deployments API Create Deployment Section](https://developer.github.com/v3/repos/deployments/#create-a-deployment) +- [GitHub Deployments API Create Deployment Status Section](https://developer.github.com/v3/repos/deployments/#create-a-deployment-status) ## Inputs -| Output | Value | -|-----|-----| -| token | github token | +| Input | Optional | Default | Description | +| --------------------- | -------- | ------- | ---------------------- | +| token | No | - | github token | +| deploymentId | Yes | Empty | | +| state | Yes | pending | | +| environmentUrl | Yes | - | | +| requiredContext | Yes | Empty | Format 'value1,value2' | +| autoMerge | Yes | false | | +| environment | Yes | qa | | +| transientEnvironment | Yes | true | | +| productionEnvironment | Yes | false | | ## Outputs -| Output | Value | -| --- | --- | +| Output | Value | +| ------------ | -------------------------------------------------------- | | deploymentId | a deployment number that can be used to set status later | ## Example usage diff --git a/action.yml b/action.yml index 2f773f1..12c5cf9 100644 --- a/action.yml +++ b/action.yml @@ -34,6 +34,10 @@ inputs: description: Production environment required: false default: 'false' + # onStateFailureStopBuild: + # description: Github token + # required: false + # default: 'true' token: description: Github token required: true