Repository navigation
142 lines (129 loc) · 5.51 KB
/
Copy pathgitops-deploy.yml
File metadata and controls
142 lines (129 loc) · 5.51 KB
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
name: GitOps Deploy
on:
workflow_call:
secrets:
APP_ID:
description: 'GitHub App client ID (use with APP_PRIVATE_KEY)'
required: false
APP_PRIVATE_KEY:
description: 'GitHub App private key'
required: false
RELEASE_TOKEN:
description: 'PAT fallback when not using a GitHub App'
required: false
inputs:
version:
description: 'Version tag to deploy'
required: true
type: string
repository:
description: 'GitOps repository (e.g. org/repo)'
required: true
type: string
ref:
description: 'Branch to checkout and push to'
required: false
default: 'main'
type: string
file:
description: 'Path to the application YAML file within the repository'
required: true
type: string
package:
description: 'Package name used in the renovate depName comment (e.g. ghcr.io/myorg/myimage)'
required: true
type: string
runner:
description: 'Runner label for the deploy job'
required: false
default: 'self-hosted'
type: string
jobs:
deploy:
runs-on: ${{ inputs.runner }}
steps:
- name: Validate auth configuration
id: auth-check
env:
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
if [ -n "${APP_ID}" ] && [ -n "${APP_PRIVATE_KEY}" ]; then
echo "use_app_auth=true" >> $GITHUB_OUTPUT
elif [ -n "${RELEASE_TOKEN}" ]; then
echo "use_app_auth=false" >> $GITHUB_OUTPUT
else
echo "::error::Either RELEASE_TOKEN or both APP_ID and APP_PRIVATE_KEY must be set"
exit 1
fi
- name: Generate app token
id: generate-token
if: steps.auth-check.outputs.use_app_auth == 'true'
uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0
with:
client-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
repositories: ${{ inputs.repository }}
permission-contents: write
- name: Checkout gitops repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ inputs.repository }}
token: ${{ steps.generate-token.outputs.token || secrets.RELEASE_TOKEN }}
ref: ${{ inputs.ref }}
- name: Update image tag
# This step's `run` script is extracted and tested directly by
# tests/update-image-tag.bats (via yq) — no separate copy to keep in sync.
env:
VERSION: ${{ inputs.version }}
FILE: ${{ inputs.file }}
PACKAGE: ${{ inputs.package }}
run: |
echo "::group::Configuration"
echo "File : ${FILE}"
echo "Package : ${PACKAGE}"
echo "Version : ${VERSION}"
echo "::endgroup::"
PACKAGE_RE=$(printf '%s' "${PACKAGE}" | sed -E 's~[.[\*^$]~\\&~g')
echo "::group::Current file state"
grep -n -E "depName=${PACKAGE}|image:[[:space:]]*${PACKAGE_RE}:" "${FILE}" || echo "Warning: no matching depName annotation or image reference found for ${PACKAGE} in ${FILE}"
echo "::endgroup::"
# Form 1: tag/targetRevision split from the image, annotated with a Renovate datasource comment, e.g.
# tag: 1.1.0 # datasource=docker depName=ghcr.io/mogenius/mogenius-ai-config
sed -i -E "s~^([[:space:]]*)(tag|targetRevision):[[:space:]]*.*(# datasource=docker depName=${PACKAGE})~\1\2: ${VERSION} \3~" "${FILE}"
# Form 2: full image reference with inline tag, marked with a plain Renovate comment, e.g.
# image: ghcr.io/mogenius/mogenius-ai-config:1.1.0 # renovate
sed -i -E "s~^([[:space:]]*image:[[:space:]]*)${PACKAGE_RE}:[^[:space:]]+([[:space:]]*#[[:space:]]*renovate.*)?$~\1${PACKAGE}:${VERSION}\2~" "${FILE}"
echo "::group::Updated file state"
grep -n -E "depName=${PACKAGE}|image:[[:space:]]*${PACKAGE_RE}:" "${FILE}"
echo "::endgroup::"
- name: Commit and push
env:
VERSION: ${{ inputs.version }}
FILE: ${{ inputs.file }}
PACKAGE: ${{ inputs.package }}
REPOSITORY: ${{ inputs.repository }}
REF: ${{ inputs.ref }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add "${FILE}"
if git diff --cached --quiet; then
echo "::notice::Tag already up to date, nothing to commit"
else
echo "::group::Diff"
git diff --cached
echo "::endgroup::"
git commit -m "chore(deps): update ${PACKAGE##*/} to ${VERSION} [skip ci]"
git push origin HEAD
echo "::notice::Deployed ${PACKAGE##*/} ${VERSION} to ${REPOSITORY}@${REF}"
fi
echo "## GitOps Deploy" >> $GITHUB_STEP_SUMMARY
echo "| Key | Value |" >> $GITHUB_STEP_SUMMARY
echo "|-----|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Repository | ${REPOSITORY} |" >> $GITHUB_STEP_SUMMARY
echo "| Branch | ${REF} |" >> $GITHUB_STEP_SUMMARY
echo "| File | ${FILE} |" >> $GITHUB_STEP_SUMMARY
echo "| Package | ${PACKAGE} |" >> $GITHUB_STEP_SUMMARY
echo "| Version | ${VERSION} |" >> $GITHUB_STEP_SUMMARY