Skip to content

GitHub Actions - Release #99

GitHub Actions - Release

GitHub Actions - Release #99

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
# Use the github-actions bot account to commit.
# https://api.github.com/users/github-actions%5Bbot%5D
#
# The second `git checkout HEAD^` is to prevent errors from the post jobs like "Prepare node".
run: |
SOURCE_SHA=$(git rev-parse HEAD);
TAG_NAME="${{ steps.resolve-tag.outputs.tag_name }}"
pushd ./packages/github-actions
# Build the JavaScript actions.
npm ci --ignore-scripts
npm run build
# Build the PHP actions into .zip files.
./build-php-actions.sh
popd
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git rm -r .
git commit -q -m "Create the ${TAG_NAME} release build for the \`github-actions\` package."
git checkout HEAD^ -- ./packages/github-actions/actions
git restore --staged .
echo "/packages/github-actions/actions/*/src" > .gitignore
# Unzip all of the PHP actions and replace them.
for zipFile in $(find ./packages/github-actions/actions -name "*.zip" -mindepth 1 -maxdepth 1) ; do
actionDir="${zipFile%.*}"
rm -rf $actionDir
unzip $zipFile -d ./packages/github-actions/actions
rm $zipFile
echo "${actionDir}/src" | sed -e "s/^\.\//!/g" >> .gitignore
done
# Move all actions to the top directory.
git add ./packages/github-actions/actions
git mv ./packages/github-actions/actions/* ./
tee README.md << END
# Custom GitHub actions
### This is the release build of version \`${TAG_NAME}\`.
### :pushpin: Please visit [here to view the source code of this version](https://github.com/woocommerce/grow/tree/${SOURCE_SHA}/packages/github-actions).
END
git add README.md
git commit -q --amend -C HEAD
git push origin HEAD:refs/heads/tmp-gha-release-build
git push -d origin tmp-gha-release-build
git checkout HEAD^ -- ./packages/github-actions/actions
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 }}