-
Notifications
You must be signed in to change notification settings - Fork 0
136 lines (117 loc) · 4 KB
/
Copy pathbuild-and-push.yml
File metadata and controls
136 lines (117 loc) · 4 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
name: Build and Push Container Images
on:
push:
tags:
- '*'
permissions:
contents: read
packages: write
env:
IMAGE_PREFIX: ghcr.io/cabotage/containers
jobs:
determine-targets:
runs-on: ubuntu-24.04
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Determine which containers to build
id: set-matrix
run: |
TAG="${GITHUB_REF#refs/tags/}"
# Build list of containers from directory names
ALL_CONTAINERS=$(ls -d containers/*/Dockerfile | xargs -n1 dirname | xargs -n1 basename | jq -R -s -c 'split("\n") | map(select(. != ""))')
# Check if tag matches "{container-name}-{version}"
MATCHED=""
for name in $(echo "$ALL_CONTAINERS" | jq -r '.[]'); do
if [[ "$TAG" == "$name-"* ]]; then
MATCHED="$name"
break
fi
done
if [ -n "$MATCHED" ]; then
echo "matrix=[\"$MATCHED\"]" >> "$GITHUB_OUTPUT"
else
echo "matrix=$ALL_CONTAINERS" >> "$GITHUB_OUTPUT"
fi
build-and-push:
needs: determine-targets
concurrency:
group: build-${{ matrix.container }}-${{ matrix.config.arch }}-${{ github.ref }}
cancel-in-progress: false
runs-on: ${{ matrix.config.os }}
strategy:
matrix:
container: ${{ fromJson(needs.determine-targets.outputs.matrix) }}
config:
- { os: ubuntu-24.04, arch: amd64 }
- { os: ubuntu-24.04-arm, arch: arm64 }
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Extract version from tag
id: version
run: |
TAG="${GITHUB_REF#refs/tags/}"
# Strip container name prefix if present
VERSION="${TAG#${{ matrix.container }}-}"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine tags
id: tags
run: |
IMAGE="${IMAGE_PREFIX}/${{ matrix.container }}"
VERSION="${{ steps.version.outputs.version }}"
ARCH="${{ matrix.config.arch }}"
TAGS="${IMAGE}:${VERSION}-${ARCH},${IMAGE}:latest-${ARCH}"
echo "tags=${TAGS}" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v7
with:
context: containers/${{ matrix.container }}
push: true
tags: ${{ steps.tags.outputs.tags }}
create-manifests:
needs: [determine-targets, build-and-push]
runs-on: ubuntu-24.04
strategy:
matrix:
container: ${{ fromJson(needs.determine-targets.outputs.matrix) }}
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract version from tag
id: version
run: |
TAG="${GITHUB_REF#refs/tags/}"
VERSION="${TAG#${{ matrix.container }}-}"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Create version manifest and push
run: |
IMAGE="${IMAGE_PREFIX}/${{ matrix.container }}"
VERSION="${{ steps.version.outputs.version }}"
docker buildx imagetools create \
--tag "${IMAGE}:${VERSION}" \
"${IMAGE}:${VERSION}-amd64" \
"${IMAGE}:${VERSION}-arm64"
- name: Create latest manifest and push
run: |
IMAGE="${IMAGE_PREFIX}/${{ matrix.container }}"
docker buildx imagetools create \
--tag "${IMAGE}:latest" \
"${IMAGE}:latest-amd64" \
"${IMAGE}:latest-arm64"