-
-
Notifications
You must be signed in to change notification settings - Fork 535
234 lines (228 loc) · 10.6 KB
/
generate_snap_stable_config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
name: 'Snap Stable Config'
on:
# Needs several triggers:
# On tag push, this should run to "verify only" (i.e. *without* pushing it to the snap-stable-build repo)
# On regular push, this should run to "verify only" - if pertinent files are changed
# On release publish event, this should run *and* push to the snap-stable-build repo
# Permit manual triggering (that pushes to the snap-stable-build repo), in case snap config for the latest tagged release needs an update
push:
branches-ignore:
- 'l10n_**' # Push events to translation service branches (that begin with "l10n_")
paths: # Path filters are not evaluated for pushes to tags.
- '.github/workflows/generate_snap_stable_config.yml'
- '.ci/snap/**'
- 'snapcraft.yaml'
- 'pkg/snap/**'
# Sequence of patterns matched against refs/tags
tags:
- '*'
pull_request:
paths:
- '.github/workflows/generate_snap_stable_config.yml'
- '.ci/snap/**'
- 'snapcraft.yaml'
- 'pkg/snap/**'
release:
types:
- published
workflow_dispatch:
inputs:
git-ref:
description: Git Ref of Snapcraft Config (Optional)
required: false
concurrency: 'snap_stable_config_${{ github.event_name }}'
jobs:
generate_stable_snap_config:
name: 'Generate Snap Stable Config'
permissions:
contents: read
runs-on: ubuntu-20.04 # Use instead of ubuntu-latest until https://github.com/snapcore/action-build/issues/42 is resolved
steps:
- name: Generate Config
id: config
env:
GH_EVENT_NAME: '${{ github.event_name }}'
GH_EVENT_ACTION: '${{ github.event.action }}'
run: |
PUBLISH_CONFIG="false"
if [[ "${GH_EVENT_NAME}" == "release" ]] || [[ "${GH_EVENT_NAME}" == "workflow_dispatch" ]]; then
PUBLISH_CONFIG="true"
fi
echo "PUBLISH_CONFIG=${PUBLISH_CONFIG}" >> $GITHUB_OUTPUT
echo "PUBLISH_CONFIG=${PUBLISH_CONFIG}" >> $GITHUB_ENV
- name: Install Prereqs
run: |
sudo snap install yq
- name: Create working directories
id: preparefolders
run: |
mkdir -p "$HOME/data/github_releases_info"
echo "RELEASES_DATA=$HOME/data/github_releases_info" >> $GITHUB_OUTPUT
mkdir -p "$HOME/temp/github_releases_info"
echo "RELEASES_TEMP=$HOME/temp/github_releases_info" >> $GITHUB_OUTPUT
mkdir -p "${{ github.workspace }}/diff"
- name: Fetch latest GitHub Release info
uses: past-due/fetch-release-info@master
with:
github_repo: 'Warzone2100/warzone2100'
github_token: '${{ secrets.GITHUB_TOKEN }}'
calculate_asset_info: false
output_directory: '_dl/data/github_releases_info'
cache_directory: '_dl/temp/github_releases_info'
- name: Move Release info
run: |
cp -r --preserve=mode _dl/. $HOME/
sudo rm -rf _dl/
- name: Checkout (Latest)
if: github.event.inputs.git-ref == ''
uses: actions/checkout@v4
with:
path: warzone2100
submodules: recursive
persist-credentials: false
- name: Checkout (Custom Ref)
if: github.event.inputs.git-ref != ''
uses: actions/checkout@v4
with:
path: warzone2100
submodules: recursive
persist-credentials: false
ref: '${{ github.event.inputs.git-ref }}'
- name: Checkout snap-stable-build@master
uses: actions/checkout@v4
with:
repository: 'Warzone2100/snap-stable-build'
ref: master
path: snap-stable-build
- name: Generate Snap Stable Config
run: |
BASE_SNAPCRAFT_YAML="snapcraft.yaml"
SNAPCRAFT_STABLE_OVERRIDES_YAML=".ci/snap/snapcraft-stable-overrides.yaml"
# Verify that required files / folders exist in main repo
cd "${GITHUB_WORKSPACE}/warzone2100"
if [[ ! -f "${BASE_SNAPCRAFT_YAML}" ]]; then
echo "Missing expected snapcraft.yaml in warzone2100 repo"
exit 1
fi
if [ ! -d "pkg/snap" ]; then
echo "Missing expected pkg/snap folder in warzone2100 repo"
exit 1
fi
# Remove the old .snapcraft.yaml (note: has a "." prefix)
OLD_DOT_PREFIX_SNAPCRAFT_YAML_PATH="${GITHUB_WORKSPACE}/snap-stable-build/.snapcraft.yaml"
if [ -f "${OLD_DOT_PREFIX_SNAPCRAFT_YAML_PATH}" ]; then
echo "Removing old .snapcraft.yaml file"
rm "${OLD_DOT_PREFIX_SNAPCRAFT_YAML_PATH}"
fi
# Generate stable repo snapcraft.yml (that uses tags directly) from main repo snapcraft.yml via yq merge
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' "${BASE_SNAPCRAFT_YAML}" "${SNAPCRAFT_STABLE_OVERRIDES_YAML}" > "${GITHUB_WORKSPACE}/snap-stable-build/snapcraft.yaml"
# Write .stable_tag file that contains the desired published tag
LATEST_GITHUB_RELEASE_TAG=$(cat "${{ steps.preparefolders.outputs.RELEASES_DATA }}/latest.json" | jq --raw-output '.tag_name')
echo "LATEST_GITHUB_RELEASE_TAG=${LATEST_GITHUB_RELEASE_TAG}"
if [ -z "${LATEST_GITHUB_RELEASE_TAG}" ]; then
echo "Failed - empty LATEST_GITHUB_RELEASE_TAG"
exit 1
fi
echo "${LATEST_GITHUB_RELEASE_TAG}" > "${GITHUB_WORKSPACE}/snap-stable-build/.stable_tag"
echo "LATEST_GITHUB_RELEASE_TAG=${LATEST_GITHUB_RELEASE_TAG}" >> $GITHUB_ENV
# Copy pkg/snap directory to pkg/snap in dest repo (overwriting old version)
rm -rf "${GITHUB_WORKSPACE}/snap-stable-build/pkg/snap"
mkdir -p "${GITHUB_WORKSPACE}/snap-stable-build/pkg/snap"
cp -r pkg/snap/. "${GITHUB_WORKSPACE}/snap-stable-build/pkg/snap"
- name: Debug output generated config
run: |
cat "${GITHUB_WORKSPACE}/snap-stable-build/snapcraft.yaml"
- name: Commit changes to snap-stable-build
id: commit-snap-changes
working-directory: "${{ github.workspace }}/snap-stable-build"
run: |
git config user.name "GitHub Actions"
git config user.email "[email protected]"
git add -A
timestamp=$(date -u)
git commit -m "Snap stable build config for ${LATEST_GITHUB_RELEASE_TAG}" || { echo "PROCESS_DEPLOYMENT=false" >> $GITHUB_OUTPUT && exit 0; }
echo "PROCESS_DEPLOYMENT=true" >> $GITHUB_OUTPUT
# Get the new commit's SHA
NEW_COMMIT_SHA=$(git rev-parse --verify HEAD)
echo "NEW_COMMIT_SHA=${NEW_COMMIT_SHA}"
echo "GH_SNAPSTABLEBUILD_COMMIT_SHA=${NEW_COMMIT_SHA}" >> $GITHUB_OUTPUT
exit 0
- name: Snap build stable config (w/ most recent stable version)
id: build-stable
if: success() && (steps.commit-snap-changes.outputs.PROCESS_DEPLOYMENT == 'true')
continue-on-error: true
uses: snapcore/action-build@v1
with:
path: ${{ github.workspace }}/snap-stable-build
- name: Fail-check
id: fail-check
if: (steps.commit-snap-changes.outputs.PROCESS_DEPLOYMENT == 'true') && (steps.build-stable.outcome == 'failure' && steps.build-stable.conclusion == 'success')
run: |
# The build-stable step failed
if [[ "${PUBLISH_CONFIG}" != "false" ]] || [[ "${{ steps.config.outputs.PUBLISH_CONFIG }}" != "false" ]]; then
# The prior step had a FATAL error (because we're aiming to publish the config)
echo "::error ::Snap stable config failed to build (for: ${LATEST_GITHUB_RELEASE_TAG})"
exit 1
else
# Output a warning that this stable config will *not* work with the current stable release (but, presumably, with the next?)
echo "::warning ::Snap stable config failed to build (for: ${LATEST_GITHUB_RELEASE_TAG}). This may be acceptable, assuming it works for the next release."
fi
- name: Create diff artifact
id: create-diff
if: success() && (github.repository == 'Warzone2100/warzone2100') && (steps.commit-snap-changes.outputs.PROCESS_DEPLOYMENT == 'true')
env:
GH_SNAPSTABLEBUILD_COMMIT_SHA: '${{ steps.commit-snap-changes.outputs.GH_SNAPSTABLEBUILD_COMMIT_SHA }}'
working-directory: "${{ github.workspace }}/snap-stable-build"
run: |
git format-patch -k -1 --stdout "${GH_SNAPSTABLEBUILD_COMMIT_SHA}" > "${{ github.workspace }}/diff/snapstablebuild.diff"
DIFF_HASH="$(sha512sum "${{ github.workspace }}/diff/snapstablebuild.diff")"
echo "DIFF_HASH=${DIFF_HASH}" >> $GITHUB_OUTPUT
echo "snapstablebuild.diff (SHA512: ${DIFF_HASH}):"
cat "${{ github.workspace }}/diff/snapstablebuild.diff"
- name: Upload diff artifact
uses: actions/upload-artifact@v4
if: success() && (github.repository == 'Warzone2100/warzone2100') && (steps.commit-snap-changes.outputs.PROCESS_DEPLOYMENT == 'true')
with:
name: snap_stable_build_diff
path: '${{ github.workspace }}/diff/snapstablebuild.diff'
if-no-files-found: 'error'
deploy_stable_snap_config:
needs: generate_stable_snap_config
environment: snap_stable_build_deploy
name: 'Deploy Snap Stable Config'
permissions:
contents: read
runs-on: ubuntu-latest
if: (github.event_name == 'release' && github.event.action == 'published' && !github.event.release.prerelease) || (github.event_name == 'workflow_dispatch')
steps:
- name: Checkout snap-stable-build@master
uses: actions/checkout@v4
with:
repository: 'Warzone2100/snap-stable-build'
ref: master
path: snap-stable-build
ssh-key: '${{ secrets.SNAP_STABLE_BUILD_REPO_GH_DEPLOY_KEY }}'
- name: Fetch Snap Stable Config Changes
uses: actions/download-artifact@v4
with:
name: snap_stable_build_diff
path: '${{ github.workspace }}/diff'
- name: Apply Changes
working-directory: "${{ github.workspace }}/snap-stable-build"
run: |
git config user.name "GitHub Actions"
git config user.email "[email protected]"
git am -k "${{ github.workspace }}/diff/snapstablebuild.diff"
- name: Debug output generated config
run: |
cat "${GITHUB_WORKSPACE}/snap-stable-build/snapcraft.yaml"
- name: Push config to snap-stable-build
if: success() && (github.repository == 'Warzone2100/warzone2100')
id: push-snap-changes
working-directory: "${{ github.workspace }}/snap-stable-build"
run: |
#git pull --rebase
# Push the new commit to the snap-stable-build repo
git push
echo "Done."
exit 0