Skip to content

GitHub Actions - Release #108

GitHub Actions - Release

GitHub Actions - Release #108

name: GitHub Actions - Release
on:
release:
types:
- published
- edited
workflow_run:
workflows:
- GitHub Actions - Create Release
types:
- completed
branches:
- release/actions
jobs:
Setup:
name: Setup and Checks
runs-on: ubuntu-latest
outputs:
release: ${{ steps.set-result.outputs.release }}
steps:
- name: Check tag name or workflow_run conclusion
uses: actions/github-script@v6
with:
script: |
const { payload, eventName } = context;
const tagReg = /^actions-v(0|[1-9]\d*)(\.(0|[1-9]\d*)){2}(-pre)?$/;
const failedWorkflowRun = eventName === 'workflow_run' && payload.workflow_run.conclusion !== 'success';
const mismatchedTagName = eventName === 'release' && ! tagReg.test( payload.release.tag_name );
if ( failedWorkflowRun || mismatchedTagName ) {
await github.rest.actions.cancelWorkflowRun( {
...context.repo,
run_id: context.runId,
} );
}
- name: Get release artifact
id: set-result
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: actions/github-script@v6
with:
script: |
const fs = require( 'fs' );
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts( {
...context.repo,
run_id: context.payload.workflow_run.id,
} );
const artifact = artifacts.find( ( el ) => el.name === 'release' );
const download = await github.rest.actions.downloadArtifact( {
...context.repo,
artifact_id: artifact.id,
archive_format: 'zip',
} );
fs.writeFileSync( `/tmp/release.zip`, Buffer.from( download.data ) );
await exec.exec( 'unzip', [ '/tmp/release.zip', '-d', '/tmp' ] );
const release = fs.readFileSync( `/tmp/release.json`, 'utf8' );
core.setOutput( 'release', release );
UpdateTags:
name: Create Release Build and Update Version Tags
runs-on: ubuntu-latest
needs: Setup
steps:
- name: Resolve tag name
id: resolve-tag
run: |
TAG_NAME="${{ github.event.release.tag_name }}"
if [ "$TAG_NAME" = '' ]; then
TAG_NAME="${{ fromJSON(needs.Setup.outputs.release || '{}').tag_name }}"
fi
echo "tag_name=${TAG_NAME}" >> $GITHUB_OUTPUT
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ steps.resolve-tag.outputs.tag_name }}
- name: Prepare node
uses: ./packages/github-actions/actions/prepare-node
with:
node-version: 14
cache-dependency-path: ./packages/github-actions/package-lock.json
install-deps: "no"
- name: Create and commit release build
id: commit-build
run: |
REPO_URL="${{ github.server_url }}/${{ github.repository }}"
TAG_NAME="${{ steps.resolve-tag.outputs.tag_name }}"
.github/scripts/github-actions-create-and-commit-build.sh "$REPO_URL" "$TAG_NAME"
# This `git checkout HEAD^` is to prevent errors from the post jobs like "Prepare node".
git checkout HEAD^ -- ./packages/github-actions/actions
# Set output for the next step
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Update version tags
uses: ./update-version-tags
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
sha: ${{ steps.commit-build.outputs.sha }}
release: ${{ needs.Setup.outputs.release }}