-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
241 lines (202 loc) · 8.77 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
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
name: "Update GitOps Image Tag"
description: "Update the image tag in the GitOps repository"
inputs:
owner:
description: "Owner of the repository if it is not the current one"
default: ${{ github.repository_owner }}
required: false
repo:
description: "Repository on which to update the image tag"
required: true
image-tag:
description: "The tag of the image to be updated"
required: true
app-id:
description: "The id of the GitHub App"
required: true
private-key:
description: "The private key of the GitHub App"
required: true
component-name:
description: "The directory the component manifests are in"
required: true
image-tag-path:
description: "The path to the image tag in the file to be updated"
default: global.image.tag
author: DIT KRG
runs:
using: "composite"
steps:
- name: Install yq
uses: jaxxstorm/action-install-gh-release@v1
with:
repo: mikefarah/yq
tag: v4.40.2
cache: enable
- name: Generate token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ inputs.app-id }}
private-key: ${{ inputs.private-key }}
owner: ${{ inputs.owner }}
repositories: ${{ inputs.repo }}
- name: Checkout target repository
uses: actions/checkout@v4
with:
repository: ${{ inputs.owner }}/${{ inputs.repo }} # Replace with the actual target repository
token: ${{ steps.generate-token.outputs.token }} # insert the token
- name: Get environment
id: get-environment
shell: bash
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
environment=production
elif [[ $GITHUB_REF == refs/heads/main ]]; then
environment=staging
else
environment=dev
fi
echo "environment=$environment" >> $GITHUB_OUTPUT
- name: Find target file
id: find-target-file
shell: bash
env:
TARGET_ENV: ${{ steps.get-environment.outputs.environment }}
run: |
if [ $TARGET_ENV == "production" ]; then
target_file="overlays/production/image.yaml"
elif [ $TARGET_ENV == "staging" ]; then
target_file="overlays/staging/image.yaml"
else
target_file="overlays/development/image.yaml"
fi
echo "target_file=$target_file" >> $GITHUB_OUTPUT
- name: Create label
id: create-label
shell: bash
env:
REPO: ${{ inputs.owner }}/${{ inputs.repo }}
LABEL_NAME: auto-deploy
LABEL_COLOR: 00FF00
LABEL_DESCRIPTION: "This label is used to automatically close a pr when the deployment is done"
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run: gh label create "$LABEL_NAME" --force --color "$LABEL_COLOR" --description "$LABEL_DESCRIPTION"
- name: Get branch name
id: get-branch-name
shell: bash
env:
TARGET_ENV: ${{ steps.get-environment.outputs.environment }}
run: |
COMPONENT_NAME=${{ inputs.component-name }}
# replace all / with - in the component name
COMPONENT_NAME=${COMPONENT_NAME//\//-}
if [ $TARGET_ENV == "production" ]; then
BRANCH_NAME="prod-release"
elif [ $TARGET_ENV == "staging" ]; then
BRANCH_NAME="staging-release"
else
BRANCH_NAME=update-${{ steps.get-environment.outputs.environment }}-$COMPONENT_NAME
fi
echo "TARGET_BRANCH=$BRANCH_NAME" >> $GITHUB_OUTPUT
- name: Create branch if not exists
shell: bash
env:
TARGET_FILE: ${{ inputs.component-name }}/${{ steps.find-target-file.outputs.target_file }}
TARGET_BRANCH: ${{ steps.get-branch-name.outputs.TARGET_BRANCH }}
run: |
# Do not create a new branch if the branch already exists
if [[ $(git ls-remote origin "$TARGET_BRANCH") ]]; then
git fetch
git checkout "$TARGET_BRANCH"
else
git checkout -b "$TARGET_BRANCH"
git push --set-upstream origin "$TARGET_BRANCH"
fi
# check if we have conflicts with main
git fetch
git rebase $TARGET_BRANCH
if [ $? -ne 0 ]; then
git checkout --theirs $TARGET_FILE
git add $TARGET_FILE
git rebase --continue
git push --force
fi
# Use the REST API to commit changes, so we get automatic commit signing
- name: Commit changes
id: commit
shell: bash
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
FULL_IMAGE_TAG: ${{ inputs.image-tag }}
TARGET_FILE: ${{ inputs.component-name }}/${{ steps.find-target-file.outputs.target_file }}
TARGET_BRANCH: ${{ steps.get-branch-name.outputs.TARGET_BRANCH }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_SERVER_URL: ${{ github.server_url }}
run: |
# Update the image.yaml file with the new image tag
echo "#########################################################################################" > $TARGET_FILE
echo "# This file was automatically generated by the auto-deploy action. Do not edit manually.#" >> $TARGET_FILE
echo "#########################################################################################" >> $TARGET_FILE
echo "# GITHUB_REPOSITORY: $GITHUB_REPOSITORY" >> $TARGET_FILE
export IMAGE_TAG="${FULL_IMAGE_TAG##*:}" # Removes everything up to and including the last colon
export GITHUB_REPO_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
# Initialize the YAML output
yq -i '
.${{ inputs.image-tag-path }} = strenv(IMAGE_TAG) |
.annotations.GITHUB_REPO_URL = strenv(GITHUB_REPO_URL) |
.annotations.GITHUB_COMMIT_HASH = strenv(GITHUB_SHA)
' "$TARGET_FILE"
# If the file has not changes then don't commit
if [[ -z $(git status --porcelain $TARGET_FILE) ]]; then
echo "commit=false" >> $GITHUB_OUTPUT
echo "No changes to commit"
exit 0
fi
echo "Committing changes"
echo "commit=true" >> $GITHUB_OUTPUT
export MESSAGE="chore: Update ${{ inputs.component-name }} image tag to $image_tag_only in ${{ steps.get-environment.outputs.environment }}"
export SHA=$( git rev-parse $TARGET_BRANCH:$TARGET_FILE )
export CONTENT=$( base64 -i $TARGET_FILE )
gh api --silent --method PUT /repos/${{ inputs.owner }}/${{ inputs.repo }}/contents/$TARGET_FILE \
--field message="$MESSAGE" \
--field content="$CONTENT" \
--field encoding="base64" \
--field branch="$TARGET_BRANCH" \
--field sha="$SHA"
- name: Create PR and try to merge
shell: bash
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
LABEL_NAME: auto-deploy
TARGET_ENV: ${{ steps.get-environment.outputs.environment }}
TARGET_FILE: ${{ steps.find-target-file.outputs.target_file }}
HEAD_BRANCH: ${{ steps.get-branch-name.outputs.TARGET_BRANCH }}
IS_COMMIT: ${{ steps.commit.outputs.commit }}
run: |
# reset the local changes and pull changes from the remote branch (Because we are using the REST API to commit changes and not the git cli)
git reset --hard
git pull
# If the file has not changes then don't commit
if [ $IS_COMMIT == "true" ]; then
# Do not create a PR if the PR already exists, just export the PR url else create a new PR if the TARGET_ENV it production or staging then do not add the auto-deploy label else add the auto-deploy label and export the PR url
if [[ $(gh pr list --state open --base main --head $HEAD_BRANCH) ]]; then
echo "PR already exists"
else
if [ $TARGET_ENV == "production" ]; then
export pr_url=$(gh pr create --title "Production release" --body "Populating PR description with release notes :clock130: ..." --base main --head $HEAD_BRANCH)
elif [ $TARGET_ENV == "staging" ]; then
export pr_url=$(gh pr create --title "Staging release" --body "Populating PR description with release notes :clock130: ..." --base main --head $HEAD_BRANCH)
else
export pr_url=$(gh pr create --title "chore: Update ${{ inputs.component-name }} image tag in ${{ steps.get-environment.outputs.environment }}" --body "This PR was automatically created by the auto-deploy action" --base main --head $HEAD_BRANCH --label $LABEL_NAME)
fi
fi
GREEN='\e[32m'
RESET='\e[0m'
echo -e "${GREEN} Pull Request: $pr_url${RESET}"
fi
# try to merge the PR if the TARGET_ENV is dev
if [ $TARGET_ENV == "dev" ]; then
echo "Merging PR"
gh pr merge --auto --merge --delete-branch
fi