|
| 1 | +name: GitOps Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + APP_ID: |
| 7 | + description: 'GitHub App client ID (use with APP_PRIVATE_KEY)' |
| 8 | + required: false |
| 9 | + APP_PRIVATE_KEY: |
| 10 | + description: 'GitHub App private key' |
| 11 | + required: false |
| 12 | + RELEASE_TOKEN: |
| 13 | + description: 'PAT fallback when not using a GitHub App' |
| 14 | + required: false |
| 15 | + inputs: |
| 16 | + version: |
| 17 | + description: 'Version tag to deploy' |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + repository: |
| 21 | + description: 'GitOps repository (e.g. org/repo)' |
| 22 | + required: true |
| 23 | + type: string |
| 24 | + ref: |
| 25 | + description: 'Branch to checkout and push to' |
| 26 | + required: false |
| 27 | + default: 'main' |
| 28 | + type: string |
| 29 | + file: |
| 30 | + description: 'Path to the application YAML file within the repository' |
| 31 | + required: true |
| 32 | + type: string |
| 33 | + package: |
| 34 | + description: 'Package name used in the renovate depName comment (e.g. ghcr.io/myorg/myimage)' |
| 35 | + required: true |
| 36 | + type: string |
| 37 | + runner: |
| 38 | + description: 'Runner label for the deploy job' |
| 39 | + required: false |
| 40 | + default: 'self-hosted' |
| 41 | + type: string |
| 42 | + |
| 43 | +jobs: |
| 44 | + deploy: |
| 45 | + runs-on: ${{ inputs.runner }} |
| 46 | + steps: |
| 47 | + - name: Validate auth configuration |
| 48 | + id: auth-check |
| 49 | + env: |
| 50 | + APP_ID: ${{ secrets.APP_ID }} |
| 51 | + APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} |
| 52 | + RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }} |
| 53 | + run: | |
| 54 | + if [ -n "${APP_ID}" ] && [ -n "${APP_PRIVATE_KEY}" ]; then |
| 55 | + echo "use_app_auth=true" >> $GITHUB_OUTPUT |
| 56 | + elif [ -n "${RELEASE_TOKEN}" ]; then |
| 57 | + echo "use_app_auth=false" >> $GITHUB_OUTPUT |
| 58 | + else |
| 59 | + echo "::error::Either RELEASE_TOKEN or both APP_ID and APP_PRIVATE_KEY must be set" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Generate app token |
| 64 | + id: generate-token |
| 65 | + if: steps.auth-check.outputs.use_app_auth == 'true' |
| 66 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 67 | + with: |
| 68 | + client-id: ${{ secrets.APP_ID }} |
| 69 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 70 | + repositories: ${{ inputs.repository }} |
| 71 | + |
| 72 | + - name: Checkout gitops repository |
| 73 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 74 | + with: |
| 75 | + repository: ${{ inputs.repository }} |
| 76 | + token: ${{ steps.generate-token.outputs.token || secrets.RELEASE_TOKEN }} |
| 77 | + ref: ${{ inputs.ref }} |
| 78 | + |
| 79 | + - name: Update image tag |
| 80 | + env: |
| 81 | + VERSION: ${{ inputs.version }} |
| 82 | + FILE: ${{ inputs.file }} |
| 83 | + PACKAGE: ${{ inputs.package }} |
| 84 | + run: | |
| 85 | + echo "::group::Configuration" |
| 86 | + echo "Repository : ${{ inputs.repository }}" |
| 87 | + echo "Branch : ${{ inputs.ref }}" |
| 88 | + echo "File : ${FILE}" |
| 89 | + echo "Package : ${PACKAGE}" |
| 90 | + echo "Version : ${VERSION}" |
| 91 | + echo "::endgroup::" |
| 92 | +
|
| 93 | + echo "::group::Current file state" |
| 94 | + grep -n "depName=${PACKAGE}" "${FILE}" || echo "Warning: no matching depName annotation found in ${FILE}" |
| 95 | + echo "::endgroup::" |
| 96 | +
|
| 97 | + git config user.name "github-actions[bot]" |
| 98 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 99 | +
|
| 100 | + sed -i -E "s/^([[:space:]]*)tag:[[:space:]]*.*(# datasource=docker depName=${PACKAGE})/\1tag: ${VERSION} \2/" "${FILE}" |
| 101 | +
|
| 102 | + echo "::group::Updated file state" |
| 103 | + grep -n "depName=${PACKAGE}" "${FILE}" |
| 104 | + echo "::endgroup::" |
| 105 | +
|
| 106 | + git add "${FILE}" |
| 107 | + if git diff --cached --quiet; then |
| 108 | + echo "::notice::Tag already up to date, nothing to commit" |
| 109 | + else |
| 110 | + echo "::group::Diff" |
| 111 | + git diff --cached |
| 112 | + echo "::endgroup::" |
| 113 | + git commit -m "chore(deps): update ${PACKAGE##*/} to ${VERSION} [skip ci]" |
| 114 | + git push origin HEAD |
| 115 | + echo "::notice::Deployed ${PACKAGE##*/} ${VERSION} to ${{ inputs.repository }}@${{ inputs.ref }}" |
| 116 | + fi |
| 117 | +
|
| 118 | + echo "## GitOps Deploy" >> $GITHUB_STEP_SUMMARY |
| 119 | + echo "| Key | Value |" >> $GITHUB_STEP_SUMMARY |
| 120 | + echo "|-----|-------|" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "| Repository | ${{ inputs.repository }} |" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "| Branch | ${{ inputs.ref }} |" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo "| File | ${FILE} |" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "| Package | ${PACKAGE} |" >> $GITHUB_STEP_SUMMARY |
| 125 | + echo "| Version | ${VERSION} |" >> $GITHUB_STEP_SUMMARY |
0 commit comments