-
Notifications
You must be signed in to change notification settings - Fork 0
126 lines (111 loc) · 3.76 KB
/
Copy pathdocker-build.yml
File metadata and controls
126 lines (111 loc) · 3.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
name: Build and Push Docker Image
on:
push:
branches:
- main
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: read
packages: write
jobs:
pre-check:
runs-on: ubuntu-latest
outputs:
should_build: ${{ steps.check.outputs.should_build }}
steps:
- uses: actions/checkout@v4
if: github.event_name == 'push' && startsWith(github.ref, 'refs/heads/')
with:
fetch-depth: 0
- name: Check if commit has release tag
id: check
run: |
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == refs/heads/* ]] && git tag --points-at HEAD | grep -q '^v'; then
echo "should_build=false" >> $GITHUB_OUTPUT
echo "::notice::Skipping beta build: commit already has a release tag"
else
echo "should_build=true" >> $GITHUB_OUTPUT
fi
build:
needs: pre-check
if: needs.pre-check.outputs.should_build == 'true'
runs-on: ${{ matrix.runner }}
outputs:
version: ${{ steps.version.outputs.version }}
is_beta: ${{ steps.version.outputs.is_beta }}
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-22.04-arm
arch: arm64
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Determine version
id: version
run: |
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
echo "version=${{ github.ref_name }}" >> $GITHUB_OUTPUT
echo "is_beta=false" >> $GITHUB_OUTPUT
else
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-6)
echo "version=beta-${SHORT_SHA}" >> $GITHUB_OUTPUT
echo "is_beta=true" >> $GITHUB_OUTPUT
fi
- name: Write VERSION file
run: echo "${{ steps.version.outputs.version }}" > VERSION
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
push: true
provenance: false
cache-from: type=gha,scope=build-${{ matrix.arch }}
cache-to: type=gha,scope=build-${{ matrix.arch }},mode=max
tags: ghcr.io/${{ github.repository_owner }}/longjin-api:${{ steps.version.outputs.version }}-${{ matrix.arch }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
manifest:
needs: build
runs-on: ubuntu-latest
steps:
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- name: Create and push multi-arch manifest
run: |
VERSION="${{ needs.build.outputs.version }}"
IS_BETA="${{ needs.build.outputs.is_beta }}"
IMAGE="ghcr.io/${{ github.repository_owner }}/longjin-api"
docker manifest create ${IMAGE}:${VERSION} \
${IMAGE}:${VERSION}-amd64 \
${IMAGE}:${VERSION}-arm64
docker manifest push ${IMAGE}:${VERSION}
if [ "${IS_BETA}" = "true" ]; then
ALIAS="beta"
else
ALIAS="latest"
fi
docker manifest create ${IMAGE}:${ALIAS} \
${IMAGE}:${VERSION}-amd64 \
${IMAGE}:${VERSION}-arm64
docker manifest push ${IMAGE}:${ALIAS}