-
Notifications
You must be signed in to change notification settings - Fork 2
113 lines (99 loc) · 3.38 KB
/
update-gitops.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
name: Update GitOps repository
on:
workflow_call:
inputs:
packages:
description: 'Packages to update'
required: true
type: string
environment:
description: 'Environment e.g. beta, prod, main used to define path to kustomize overlay'
required: true
type: string
image-prefix:
description: 'Prefix of image e.g. thinc-org/cugetreg'
required: true
type: string
gitops-repository:
description: 'Target GitOps repository'
required: true
type: string
gitops-ref:
description: 'Target GitOps ref'
type: string
default: refs/heads/master
container-registry:
description: 'Container registry e.g. ghcr.io'
type: string
default: ghcr.io
mode:
description: 'Mode of the action, pr or commit'
type: string
default: pr
secrets:
GH_TOKEN:
description: 'GitHub token used to checkout GitOps repository and open PR'
required: true
jobs:
update-gitops:
name: Update GitOps and Open PR
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
repository: ${{ inputs.gitops-repository }}
ref: ${{ inputs.gitops-ref }}
token: ${{ secrets.GH_TOKEN }}
- name: Setup Kustomize
uses: multani/action-setup-kustomize@v1
with:
version: 5.0.0
- name: Update kustomize configuration
run: |
for row in $(echo "$PACKAGES" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
NAME=$(_jq '.name')
IMAGE_TAG=$(_jq '.imageTag')
PREFIX=${{ inputs.container-registry }}/${{ inputs.image-prefix }}
KUSTOMIZE_PATH=k8s/$NAME/overlays/${{ inputs.environment }}
[ -d "$KUSTOMIZE_PATH" ] && bash -c "cd $KUSTOMIZE_PATH && kustomize edit set image $NAME=$PREFIX/$NAME:$IMAGE_TAG"
echo "${NAME}:${IMAGE_TAG} is updated"
done
env:
PACKAGES: ${{ inputs.packages }}
- name: Show Git Status
run: git status
- name: Prepare Pull Request body
id: pr-body
if: ${{ inputs.mode == 'pr' }}
run: |
echo "DATE=$(date +'%d/%m/%Y')" >> $GITHUB_OUTPUT
UPDATED_PACKAGES=$(echo "$PACKAGES" | jq -r '.[] | "- \(.name): \(.imageTag)"')
echo "UPDATED_PACKAGES<<EOF" >> $GITHUB_ENV
echo "$UPDATED_PACKAGES" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
env:
PACKAGES: ${{ inputs.packages }}
- name: Create Pull Request to GitOps
uses: peter-evans/create-pull-request@v4
if: ${{ inputs.mode == 'pr' }}
with:
token: ${{ secrets.GH_TOKEN }}
commit-message: update gitops
branch: update-gitops
base: ${{ inputs.gitops-ref }}
title: Update GitOps
body: |
# Update GitOps
Date ${{ steps.pr-body.outputs.DATE }}
> This PR is automatically generated
## Updated Packages
${{ env.UPDATED_PACKAGES }}
- name: Add, commit and push to the repository
uses: stefanzweifel/git-auto-commit-action@v4
if: ${{ inputs.mode == 'commit' }}
with:
commit_message: Update GitOps