forked from sartography/spiff-arena
-
Notifications
You must be signed in to change notification settings - Fork 7
167 lines (148 loc) · 5.77 KB
/
deploy-docker.yml
File metadata and controls
167 lines (148 loc) · 5.77 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
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
name: Deploy to Docker
on:
workflow_dispatch:
inputs:
rc_tag:
description: "RC tag name (e.g. 1.2.3-rc)"
required: true
type: string
workflow_run:
workflows: ["Create Release Tag"]
types: [completed]
branches: [main]
permissions:
contents: read
concurrency:
group: deploy-dev
cancel-in-progress: true
env:
REGISTRY: docker.io
IMAGE_REPOSITORY: m8flow
PROJECT: m8flow
COMPONENTS: '["backend", "frontend", "keycloak", "connector-proxy"]'
jobs:
prepare:
name: Prepare (resolve ref & tag)
if: |
(vars.AUTO_BUILD == 'true' || github.event_name == 'workflow_dispatch') &&
(github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success')
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.meta.outputs.ref }}
tag: ${{ steps.meta.outputs.tag }}
components: ${{ env.COMPONENTS }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.rc_tag || github.event.workflow_run.head_sha || github.ref }}
fetch-depth: 0 # Required to find tags at HEAD
- name: Extract metadata
id: meta
run: |
if [[ -n "${{ github.event.inputs.rc_tag }}" ]]; then
TAG="${{ github.event.inputs.rc_tag }}"
elif [[ "${{ github.event_name }}" == "workflow_run" ]]; then
git fetch origin --tags
TAG=$(git tag --points-at HEAD | sort -V | tail -1)
[[ -z "$TAG" ]] && { echo "::error::No tag found at HEAD"; exit 1; }
else
TAG="${{ github.ref_name }}"
fi
echo "tag=${TAG}" >> $GITHUB_OUTPUT
echo "ref=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
backup-existing-tags:
name: Backup Tags
needs: prepare
runs-on: ubuntu-latest
strategy:
matrix:
component: ${{ fromJson(needs.prepare.outputs.components) }}
steps:
- name: Setup regctl
uses: regclient/actions/regctl-installer@v0.1.0
- name: Login to Docker Hub
env:
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
echo "$DOCKERHUB_TOKEN" | regctl registry login ${{ env.REGISTRY }} -u "$DOCKERHUB_USERNAME" --pass-stdin
- name: Manage Backup Tags
env:
FULL_REPO: "${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/${{ env.PROJECT }}-${{ matrix.component }}"
TAG: "${{ needs.prepare.outputs.tag }}"
run: |
# 1. Check if the current tag exists.
if ! regctl image inspect "${FULL_REPO}:${TAG}" > /dev/null 2>&1; then
echo "Tag ${TAG} not found on remote. Nothing to backup."
exit 0
fi
# 2. If a backup already exists, delete it so we can overwrite it.
# Some registries don't allow overwriting a tag via 'copy' if it exists.
if regctl image inspect "${FULL_REPO}:${TAG}-backup" > /dev/null 2>&1; then
echo "Found existing backup. Deleting..."
regctl tag rm "${FULL_REPO}:${TAG}-backup" || true
fi
# 3. Server-side retag (No layer download)
echo "Backing up ${TAG} to ${TAG}-backup..."
regctl image copy "${FULL_REPO}:${TAG}" "${FULL_REPO}:${TAG}-backup"
build:
name: Build & Push
needs: [prepare, backup-existing-tags]
runs-on: ubuntu-latest
strategy:
matrix:
component: ${{ fromJson(needs.prepare.outputs.components) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.ref }}
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build & Push
id: build
uses: docker/build-push-action@v5
with:
context: ${{ matrix.component == 'connector-proxy' && 'm8flow-connector-proxy' || '.' }}
file: ${{ matrix.component == 'connector-proxy' && 'm8flow-connector-proxy/Dockerfile' || format('docker/m8flow.{0}.Dockerfile', matrix.component) }}
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/${{ env.PROJECT }}-${{ matrix.component }}:${{ needs.prepare.outputs.tag }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/${{ env.PROJECT }}-${{ matrix.component }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/${{ env.PROJECT }}-${{ matrix.component }}:buildcache,mode=max
provenance: true
sbom: true
- name: Save Digest
run: echo "${{ steps.build.outputs.digest }}" > digest.txt
- name: Upload Digest Artifact
uses: actions/upload-artifact@v4
with:
name: digest-${{ matrix.component }}
path: digest.txt
retention-days: 1
combine:
name: Finalize Artifacts
needs: [prepare, build]
runs-on: ubuntu-latest
steps:
- name: Download all digests
uses: actions/download-artifact@v4
with:
path: digests
pattern: digest-*
merge-multiple: false
- name: Create Summary
run: |
echo "### Deployment Summary - ${{ needs.prepare.outputs.tag }}" >> $GITHUB_STEP_SUMMARY
echo "| Component | Digest |" >> $GITHUB_STEP_SUMMARY
echo "|-----------|--------|" >> $GITHUB_STEP_SUMMARY
for f in digests/digest-*/digest.txt; do
COMPONENT=$(basename $(dirname $f) | sed 's/digest-//')
DIGEST=$(cat $f)
echo "| $COMPONENT | \`$DIGEST\` |" >> $GITHUB_STEP_SUMMARY
done