-
Notifications
You must be signed in to change notification settings - Fork 7
154 lines (129 loc) · 5.34 KB
/
github-actions-release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 }}