Deploy To Artifacts #64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy To Artifacts | |
on: | |
workflow_dispatch: | |
inputs: | |
sha: | |
description: 'The commit ref or SHA' | |
required: true | |
default: 'master' | |
merged_sha: | |
description: 'The merge commit SHA' | |
base_sha: | |
description: 'The base commit SHA' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
outputs: | |
sha: ${{steps.prep.outputs.sha}} | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.ACTIVE_TOKEN}} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: context.payload.inputs.sha, | |
context: context.workflow, | |
state: 'pending', | |
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` | |
}); | |
- id: prep | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.ACTIVE_TOKEN }} | |
script: | | |
core.setOutput('sha', context.payload.inputs.merged_sha || context.payload.inputs.sha); | |
- uses: actions/checkout@v3 | |
with: | |
ref: ${{steps.prep.outputs.sha}} | |
- run: | | |
npm i | |
npx gulp build | |
npm pack | |
- id: package-name | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const { name, version } = require(require('path').join(process.env.GITHUB_WORKSPACE, 'package.json')); | |
core.setOutput('packageName', `${name}-${version}`); | |
core.setOutput('imageName', `${name}/${name}:${version}`); | |
- uses: actions/upload-artifact@v3 | |
with: | |
name: npm | |
path: | | |
${{steps.package-name.outputs.packageName}}.tgz | |
changes: | |
# TODO: currently cannot generate a list of changes after rebase | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.ACTIVE_TOKEN}} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: context.payload.inputs.sha, | |
context: context.workflow, | |
state: 'pending', | |
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` | |
}); | |
- uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.inputs.sha }} | |
fetch-depth: 0 | |
- run: | | |
git reset --soft `git merge-base "${{github.event.inputs.base_sha}}" HEAD` | |
git diff --name-only --cached > changes.txt | |
- uses: actions/upload-artifact@v3 | |
with: | |
name: changes | |
path: changes.txt | |
test: | |
runs-on: ubuntu-latest | |
needs: [build, changes] | |
environment: CI | |
steps: | |
- uses: actions/download-artifact@v3 | |
with: | |
name: changes | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.ACTIVE_TOKEN}} | |
script: | | |
function getInputs () { | |
const { sha, merged_sha } = context.payload.inputs; | |
return { | |
...merged_sha ? { merged_sha } : {}, | |
sha, | |
deploy_run_id: '${{github.run_id}}' | |
} | |
} | |
async function dispatchWorkflow (workflowName) { | |
await github.rest.actions.createWorkflowDispatch({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'master', | |
workflow_id: workflowName, | |
inputs: getInputs() | |
}); | |
} | |
// TODO: Optimize by running only necessary tests for corresponding changes | |
const fileList = require('fs').readFileSync('changes.txt').toString().split('\n').filter(line => line); | |
const tasks = []; | |
tasks.push('test-win.yml'); | |
tasks.push('test-mac.yml'); | |
tasks.push('test-lts.yml'); | |
tasks.push('test-stable.yml'); | |
await Promise.all(tasks.map(task => dispatchWorkflow(task))); | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.ACTIVE_TOKEN}} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: context.payload.inputs.sha, | |
context: context.workflow, | |
state: 'success', | |
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}#artifacts` | |
}); | |
- uses: actions/github-script@v6 | |
if: failure() || cancelled() | |
with: | |
github-token: ${{secrets.ACTIVE_TOKEN}} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: context.payload.inputs.sha, | |
context: context.workflow, | |
state: 'failure', | |
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` | |
}); |