This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
201 lines (185 loc) · 7.94 KB
/
action.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
name: 'Build GitHub Action Workflows'
description: >-
Builds workflows from templates in a specified repo. This requires
the templates cloned before running. It will also delete the
templates clone after use.
inputs:
github-token:
description: Token used to create the PR
required: true
templates-location:
description: >-
Relative path to the directory where the templates repository
was cloned. This directory will be deleted after use; do not
use it after this action has run.
required: true
templater-image-name:
description: >-
Name of the docker image to run for the templater action. It is
expected that you are logged in to the registry required to pull
this image.
required: true
extra-pr-label:
description: Label created PRs with this additional label
required: false
default: ""
create-pull-request:
description: Create a pull request after rendering templates
default: true
required: false
github-repository:
description: >-
Full name of the repository that is being rendered. e.g. smartlyio/ci-sla.
Default is the repository running the workflow
default: ${{ github.repository }}
required: false
local-render:
description: >-
Use the templates checkout directly without switching to the branch
requested by the project (usually master). Default false.
default: "false"
required: false
disable_workflows_prefix:
description: >-
Generated workflow files with this filename prefix will have the `on` section
automatically replaced with an invalid empty `on` list to prevent them from running.
Ignored if empty
default: ''
required: false
outputs:
has-changes:
description: Boolean. True if changes were made to the rendered workflows.
value: ${{ steps.collect-changes.outputs.has-changes }}
runs:
using: "composite"
steps:
- name: Fix default branch discovery for devbox
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
INPUT_GITHUB_REPOSITORY: ${{ inputs.github-repository }}
shell: bash
run: |
DEFAULT_BRANCH="$(gh api "/repos/${INPUT_GITHUB_REPOSITORY}" | jq -r .default_branch)"
echo "ref: refs/remotes/origin/${DEFAULT_BRANCH}" > .git/refs/remotes/origin/HEAD
- name: Run templater
shell: bash
env:
INPUT_GITHUB_REPOSITORY: ${{ inputs.github-repository }}
run: |
REPOSITORY_NAME="$(echo "$INPUT_GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//")"
render_arg="--no-update"
local_render="${{ inputs.local-render }}"
if [[ "${local_render}" == "true" ]]; then
render_arg="--local"
fi
docker pull "${{ inputs.templater-image-name }}"
docker run --hostname "workflow-update-${REPOSITORY_NAME}" --rm \
--mount type=bind,src="$(pwd),dst=/devbox/workspace" \
--mount type=bind,src="$(pwd)/${{ inputs.templates-location }},dst=/devbox/github-actions-templates" \
"${{ inputs.templater-image-name }}" \
render workflows "$render_arg"
- name: Remove github-actions-templates checkout
shell: bash
# Remove templates dir so we don't leave the working tree dirty for
# commit in next step
run: |
rm -rf REMOVE-ME-github-actions-templates
- name: Disable rendered workflows if required
shell: bash
env:
INPUT_DISABLE_WORKFLOWS_PREFIX: ${{ inputs.disable_workflows_prefix }}
run: |
if [ -z "$INPUT_DISABLE_WORKFLOWS_PREFIX" ]; then
echo "No workflows configured for disabling"
exit 0
fi
mapfile -t WORKFLOWS < <(find .github/workflows -type f -name "${INPUT_DISABLE_WORKFLOWS_PREFIX}*.yml")
for workflow in "${WORKFLOWS[@]}"; do
echo "Disabling $workflow"
yq e -i '.on = {"pull_request": {"types": ["locked"], "branches": ["workflow-renovate-canary-branch-should-never-exist"]}}' "$workflow"
done
- name: Push changes if required
id: collect-changes
env:
INPUT_GITHUB_REPOSITORY: ${{ inputs.github-repository }}
GITHUB_TOKEN: ${{ inputs.github-token }}
PR_LABEL: ${{ inputs.extra-pr-label }}
CREATE_PULL_REQUEST: ${{ inputs.create-pull-request }}
shell: bash
run: |
REPOSITORY_NAME="$(echo "$INPUT_GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//")"
BRANCH_NAME="${REPOSITORY_NAME}-github-actions-self-update"
function pr_exists() {
local repo="$1"
local branch="$2"
local PR_COUNT=
PR_COUNT="$(gh api "repos/${repo}/pulls?state=open&head=smartlyio:${branch}" | jq '. | length')"
[ "$PR_COUNT" -gt 0 ]
}
git status
# Check for changes in .github/workflows
git_changes="$(git status --porcelain -- .github/workflows || true)"
# If the output is not empty, there are changes. Commit them
if [ -n "$git_changes" ]; then
echo "has-changes=true" >> "$GITHUB_OUTPUT"
echo "Changes to workflows found!"
if [[ "$CREATE_PULL_REQUEST" == "false" ]]; then
echo "Skipping pull request creation"
exit 0
fi
echo "Using branch: $BRANCH_NAME"
git checkout -b "$BRANCH_NAME"
echo "Adding workflows directory and making commit"
git add .github/workflows/
git commit -m "Adding workflow changes at $(date)"
echo "Check if there is an existing branch and if the change set already exists in it."
if [ -z "$(git diff "origin/$BRANCH_NAME" || echo "no branch")" ]; then
echo "Seems to have no differences from existing branch!"
echo "Switching to the upstream branch"
git branch --set-upstream-to="origin/$BRANCH_NAME"
git reset --hard "origin/$BRANCH_NAME"
else
echo "Pushing the branch"
git push --force-with-lease -u origin HEAD
fi
echo "Check if the PR already exists"
if pr_exists "$INPUT_GITHUB_REPOSITORY" "$BRANCH_NAME"; then
echo "PR Exists already!"
exit 0
else
echo "Make sure the labels exist"
if [ -n "$PR_LABEL" ]; then
if ! gh api "/repos/smartlyio/${REPOSITORY_NAME}/labels/${PR_LABEL}"; then
gh api "/repos/smartlyio/${REPOSITORY_NAME}/labels" -X POST \
-f "name=${PR_LABEL}" \
-f "description=Do not create a release from this pull request"
fi
fi
if ! gh api "/repos/smartlyio/${REPOSITORY_NAME}/labels/workflow-updater"; then
gh api "/repos/smartlyio/${REPOSITORY_NAME}/labels" -X POST \
-f "name=workflow-updater" \
-f "color=ff2994" \
-f "description=Automatically generated by workflow updater"
fi
echo "Create the PR!"
gh pr create \
--title "GitHub Actions Self Update for ${REPOSITORY_NAME}" \
--body "These changes are generated by the GitHub Actions self updater. Remember to delete the branch!" \
--reviewer smartlyio/vulcan \
--label "${PR_LABEL:+${PR_LABEL},}workflow-updater"
exit 0
fi
else
if [[ "$CREATE_PULL_REQUEST" == "false" ]]; then
echo "Skipping pull request manipulation"
exit 0
fi
if pr_exists "$INPUT_GITHUB_REPOSITORY" "$BRANCH_NAME"; then
echo "Close the PR as there are no longer relevant changes"
gh pr close "$BRANCH_NAME"
git push origin --delete "$BRANCH_NAME"
fi
echo "has-changes=false" >> "$GITHUB_OUTPUT"
echo "No Changes Found."
exit 0
fi