-
Notifications
You must be signed in to change notification settings - Fork 58
178 lines (156 loc) · 5.76 KB
/
Copy pathdocker-build-publish.yml
File metadata and controls
178 lines (156 loc) · 5.76 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
168
169
170
171
172
173
174
175
176
177
178
name: Docker Build & Publish
on:
push:
branches: ["main"]
# Publish semver tags as releases.
tags: ["v*.*.*"]
pull_request:
branches: ["main"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
OCI_IMAGE_NAME: ""
OCI_PUSH: ${{ vars.OCI_PUSH }}
jobs:
build:
name: Build OCI Images
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-24.04
arch: amd64
platform: linux/amd64
- os: ubuntu-24.04-arm
arch: arm64
platform: linux/arm64
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Set correct OCI image name
id: ociImageName
run: echo "::set-output name=lowerCaseValue::${OCI_IMAGE_NAME,,}"
env:
OCI_IMAGE_NAME: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Checkout the repo
- name: Checkout repository
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Show git version info
run: |
echo "git describe (dirty): $(git describe --dirty --always --tags)"
echo "git describe --tags: $(git describe --tags `git rev-list --tags --max-count=1`)"
echo "git tag: $(git tag --sort=-committerdate | head -n 1)"
echo "github_ref: $GITHUB_REF"
echo "github_head_sha: ${{ github.event.pull_request.head.sha }}"
git tag -l
- name: Determine git current SHA and latest tag
id: git-version
run: |
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
GIT_TAG=${GITHUB_REF#refs/tags/}
elif [[ "$GITHUB_REF" == refs/pull/* ]]; then
PR_NUMBER=$(echo "$GITHUB_REF" | awk -F'/' '{print $3}')
GIT_TAG="pr-${PR_NUMBER}"
else
GIT_TAG="dev"
fi
echo "GIT_TAG=$GIT_TAG" >> "$GITHUB_OUTPUT"
GIT_SHA=$(git rev-parse --short HEAD)
echo "GIT_SHA=$GIT_SHA" >> $GITHUB_OUTPUT
echo "GIT_TAG=$GIT_TAG"
echo "GIT_SHA=$GIT_SHA"
# Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
# Extract metadata (tags, labels) for Docker
- name: Extract Docker metadata (release)
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Login into GitHub Container Registry (only if OCI_PUSH is true and not run by Dependabot)
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v4
if: env.OCI_PUSH == 'true' && github.actor != 'dependabot[bot]'
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v7
with:
push: ${{ env.OCI_PUSH == 'true' && github.actor != 'dependabot[bot]' }}
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
outputs: type=image,"name=${{ steps.ociImageName.outputs.lowerCaseValue }}",push-by-digest=true,name-canonical=true
build-args: |
GIT_TAG=${{ steps.git-version.outputs.GIT_TAG }}
GIT_SHA=${{ steps.git-version.outputs.GIT_SHA }}
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v7
if: env.OCI_PUSH == 'true' && github.actor != 'dependabot[bot]'
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
if: vars.OCI_PUSH == 'true' && github.actor != 'dependabot[bot]'
needs:
- build
concurrency:
group: ${{ github.ref }}
cancel-in-progress: false
steps:
- name: Set correct OCI image name
id: ociImageName
run: |
echo "lowerCaseValue=${OCI_IMAGE_NAME,,}" >> $GITHUB_OUTPUT
env:
OCI_IMAGE_NAME: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Download digests
uses: actions/download-artifact@v8
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v4
if: github.actor != 'dependabot[bot]'
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: |
${{ steps.ociImageName.outputs.lowerCaseValue }}
tags: |
type=ref,event=pr
type=semver,pattern={{version}}
type=raw,value=dev,enable={{is_default_branch}}
- name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ steps.ociImageName.outputs.lowerCaseValue }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ steps.ociImageName.outputs.lowerCaseValue }}:${{ steps.meta.outputs.version }}