forked from os-climate/hazard
-
Notifications
You must be signed in to change notification settings - Fork 0
342 lines (304 loc) · 12 KB
/
bootstrap.yaml
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
name: "♻️ Update shared DevOps tooling"
# yamllint disable-line rule:truthy
on:
workflow_dispatch:
schedule:
- cron: "0 8 * * MON"
jobs:
update-actions:
name: "Update DevOps tooling"
runs-on: ubuntu-latest
permissions:
# IMPORTANT: mandatory to create or update content/actions/pr
contents: write
actions: write
pull-requests: write
steps:
- name: "Checkout primary repository"
uses: actions/checkout@v4
- name: "Pull devops content from repository"
uses: actions/checkout@v4
with:
repository: "os-climate/devops-toolkit"
path: ".devops"
- name: "Update repository workflows and create PR"
id: update-repository
env:
GH_TOKEN: ${{ github.token }}
# yamllint disable rule:line-length
run: |
#SHELLCODESTART
set -euo pipefail
# set -x
# Define variables
DEVOPS_DIR=".devops"
AUTOMATION_BRANCH="update-devops-tooling"
REPO_DIR=$(git rev-parse --show-toplevel)
GIT_ORIGIN=$(git config --get remote.origin.url)
REPO_NAME=$(basename -s .git "$GIT_ORIGIN")
EXCLUDE_FILE=".devops-exclusions"
DEVOPS_REPO='[email protected]:os-climate/devops-toolkit.git'
HEAD_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Content folder defines the files and folders to update
FILES="$DEVOPS_DIR/content/files.txt"
FOLDERS="$DEVOPS_DIR/content/folders.txt"
# Define functions
perform_folder_operation() {
FS_PATH="$1"
if [ -d "$DEVOPS_DIR"/"$FS_PATH" ]; then
echo "Scanning target folder content at: $FS_PATH"
return 0
else
echo "Upstream folder NOT found: $FS_PATH [skipping]"
return 1
fi
}
# Allows for selective opt-out components on a per-path basis
perform_operation() {
FS_PATH="$1"
if [ ! -f "$DEVOPS_DIR"/"$FS_PATH" ]; then
echo "Skipping missing upstream file at: $FS_PATH"
return 1
fi
# Elements excluded from processing return exit status 1
if [ ! -f "$EXCLUDE_FILE" ]; then
return 0
elif [ "$FS_PATH" = "$EXCLUDE_FILE" ]; then
# The exclusion file itself is never updated by automation
return 1
elif (grep -Fxq "$FS_PATH" "$EXCLUDE_FILE" > /dev/null); then
# Element listed; exclude from processing
return 1
else
# Element not found in exclusion file; process it
return 0
fi
}
# Only updates file if it has changed
selective_file_copy() {
# Receives a single file path as argument
# SHA_SRC=$(sha1sum "$DEVOPS_DIR/$1" | awk '{print $1}')
# SHA_DST=$(sha1sum "$1" 2>/dev/null | awk '{print $1}' || :)
# if [ "$SHA_SRC" != "$SHA_DST" ]; then
if ! (cmp "$DEVOPS_DIR/$1" "$1"); then
echo "Copying: $1"
cp "$DEVOPS_DIR/$1" "$1"
git add "$1"
fi
}
check_pr_for_author() {
AUTHOR="$1"
printf "Checking for pull requests by: %s" "$AUTHOR"
# Capture the existing PR number
PR_NUM=$(gh pr list --state open -L 1 \
--author "$AUTHOR" --json number | \
grep "number" | sed "s/:/ /g" | awk '{print $2}' | \
sed "s/}//g" | sed "s/]//g")
if [ -z "$PR_NUM" ]; then
echo " [none]"
return 1
else
echo " [$PR_NUM]"
echo "Running: gh pr checkout $PR_NUM"
if (gh pr checkout "$PR_NUM"); then
return 0
else
echo "Failed to checkout GitHub pull request"
echo "Check errors/output for the cause"
return 2
fi
fi
}
check_prs() {
# Define users to check for pre-existing pull requests
AUTOMATION_USER="github-actions[bot]"
if [[ -n ${GH_TOKEN+x} ]]; then
GITHUB_USERS="$AUTOMATION_USER"
else
GITHUB_USERS=$(gh api user | jq -r '.login')
# Check local user account first, if enumerated
GITHUB_USERS+=" $AUTOMATION_USER"
fi
# Check for existing pull requests opened by this automation
for USER in $GITHUB_USERS; do
if (check_pr_for_author "$USER"); then
return 0
else
STATUS="$?"
fi
if [ "$STATUS" -eq 1 ]; then
continue
elif [ "$STATUS" -eq 2 ]; then
echo "Failed to checkout pull request"; exit 1
fi
done
return 1
}
# Check if script is running in GHA workflow
in_github() {
if [ -z ${GITHUB_RUN_ID+x} ]; then
echo "Script is NOT running in GitHub"
return 1
else
echo "Script is running in GitHub"
return 0
fi
}
# Check if user is logged into GitHub
logged_in_github() {
if (gh auth status); then
echo "Logged in and authenticated to GitHb"
return 0
else
echo "Not logged into GitHub, some script operations unavailable"
return 1
fi
}
# Main script entry point
echo "Repository name and HEAD branch: $REPO_NAME [$HEAD_BRANCH]"
# Ensure working from top-level of GIT repository
CURRENT_DIR=$(pwd)
if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then
echo "Changing directory to: $REPO_DIR"
if ! (cd "$REPO_DIR"); then
echo "Error: unable to change directory"; exit 1
fi
fi
# Stashing only used during development/testing
# Check if there are unstaged changes
# if ! (git diff --exit-code --quiet); then
# echo "Stashing unstaged changes in current repository"
# git stash -q
# fi
# Configure GIT environment only if NOT already configured
# i.e. when running in a GitHub Actions workflow
TEST=$(git config -l > /dev/null 2>&1)
if [ -n "$TEST" ]; then
git config user.name "github-actions[bot]"
git config user.email \
"41898282+github-actions[bot]@users.noreply.github.com"
fi
if ! (check_prs); then
# No existing open pull requests found for this repository
# Remove remote branch if it exists
git push origin --delete "$AUTOMATION_BRANCH" > /dev/null 2>&1 || :
git branch -D "$AUTOMATION_BRANCH" || :
git checkout -b "$AUTOMATION_BRANCH"
else
# The -B flag swaps branch and creates it if NOT present
git checkout -B "$AUTOMATION_BRANCH"
fi
# Only if NOT running in GitHub
# (checkout is otherwise performed by earlier steps)
if ! (in_github); then
# Remove any stale local copy of the upstream repository
if [ -d "$DEVOPS_DIR" ]; then
rm -Rf "$DEVOPS_DIR"
fi
printf "Cloning DevOps repository into: %s" "$DEVOPS_DIR"
if (git clone "$DEVOPS_REPO" "$DEVOPS_DIR" > /dev/null 2>&1); then
echo " [success]"
else
echo " [failed]"; exit 1
fi
fi
# Process upstream DevOps repository content and update
LOCATIONS=""
# Populate list of files to be updated/sourced
while read -ra LINE;
do
for FILE in "${LINE[@]}";
do
LOCATIONS+="$FILE "
done
done < "$FILES"
# Gather files from specified folders and append to locations list
while read -ra LINE;
do
for FOLDER in "${LINE[@]}";
do
# Check to see if this folder should be skipped
if (perform_folder_operation "$FOLDER"); then
# If necessary, create target folder
if [ ! -d "$FOLDER" ]; then
echo "Creating target folder: $FOLDER"
mkdir "$FOLDER"
fi
# Add folder contents to list of file LOCATIONS
FILES=$(cd "$DEVOPS_DIR/$FOLDER"; find . -maxdepth 1 -type f -exec basename {} \;)
for LOCATION in $FILES; do
# Also check if individual files in the folder are excluded
if (perform_operation "$FOLDER/$LOCATION"); then
LOCATIONS+=" $FOLDER/$LOCATION"
fi
done
else
echo "Opted out of folder: $FOLDER"
continue
fi
done;
done < "$FOLDERS"
# Copy specified files into repository root
for LOCATION in ${LOCATIONS}; do
if (perform_operation "$LOCATION"); then
selective_file_copy "$LOCATION"
else
echo "Not updating: $LOCATION"
fi
done
# If no changes required, do not throw an error
if [ -z "$(git status --porcelain)" ]; then
echo "No updates/changes to commit"; exit 0
fi
# Temporarily disable exit on unbound variable
set +eu +o pipefail
# Next step is only performed if running as GitHub Action
if [[ -n ${GH_TOKEN+x} ]]; then
# Script is running in a GitHub actions workflow
# Set outputs for use by the next actions/steps
# shellcheck disable=SC2129
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "branchname=$AUTOMATION_BRANCH" >> "$GITHUB_OUTPUT"
echo "headbranch=$HEAD_BRANCH" >> "$GITHUB_OUTPUT"
# Move to the next workflow step to raise the PR
git push --set-upstream origin "$AUTOMATION_BRANCH"
exit 0
fi
# If running shell code locally, continue to raise the PR
# Reinstate exit on unbound variables
set -euo pipefail
git status
if ! (git commit -as -S -m "Chore: Update DevOps tooling from central repository [skip ci]" \
-m "This commit created by automation/scripting" --no-verify); then
echo "Commit failed; aborting"; exit 1
else
# Push branch to remote repository
git push --set-upstream origin "$AUTOMATION_BRANCH"
# Create PR request
gh pr create \
--title "Chore: Pull DevOps tooling from upstream repository" \
--body 'Automated by a GitHub workflow: bootstrap.yaml'
fi
# echo "Unstashing unstaged changes, if any exist"
# git stash pop -q || :
#SHELLCODEEND
- name: Create Pull Request
if: steps.update-repository.outputs.changed == 'true'
uses: peter-evans/create-pull-request@v6
# env:
# GITHUB_TOKEN: ${{ github.token }}
with:
# Note: Requires a specific/defined Personal Access Token
token: ${{ secrets.ACTIONS_WORKFLOW }}
commit-message: "Chore: Update DevOps tooling from central repository [skip ci]"
signoff: "true"
base: ${{ steps.update-repository.outputs.headbranch }}
branch: ${{ steps.update-repository.outputs.branchname }}
delete-branch: true
title: "Chore: Update DevOps tooling from central repository [skip ci]"
body: |
Update repository with content from upstream: os-climate/devops-toolkit
labels: |
automated pr
draft: false