-
Notifications
You must be signed in to change notification settings - Fork 1
103 lines (92 loc) · 3.62 KB
/
build.yml
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
name: Build and push images
on:
workflow_dispatch:
inputs:
REGISTRY:
description: Target registry to push images
required: true
type: string
default: ghcr.io
NAMESPACE:
description: Target namespace to the given registry
required: true
type: string
default: this-is-tobi/tools
PLATFORMS:
description: Target platforms to build images
required: true
type: string
default: linux/amd64,linux/arm64
env:
REGISTRY: ${{ inputs.REGISTRY }}
NAMESPACE: ${{ inputs.NAMESPACE }}
PLATFORMS: ${{ inputs.PLATFORMS }}
jobs:
matrix:
name: Generate matrix for build
runs-on: ubuntu-latest
outputs:
build-matrix: ${{ steps.build-matrix.outputs.BUILD_MATRIX }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Generate matrix for build
id: build-matrix
run: |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix.json)" >> $GITHUB_OUTPUT
build:
name: Build images
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs:
- matrix
strategy:
matrix:
images: ${{ fromJSON(needs.matrix.outputs.build-matrix) }}
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.REGISTRY == 'ghcr.io' && github.actor || secrets.REGISTRY_USERNAME }}
password: ${{ env.REGISTRY == 'ghcr.io' && secrets.GITHUB_TOKEN || secrets.REGISTRY_PASSWORD }}
logout: true
- name: Get image status
id: image-status
run: |
IMAGE_STATUS=$(curl \
--head \
--silent \
--write-out '%{http_code}' \
--output /dev/null \
-H "Authorization: Bearer $(echo -n '${{ secrets.GITHUB_TOKEN }}' | base64)" \
https://${{ env.REGISTRY }}/v2/${{ env.NAMESPACE }}/${{ matrix.images.name }}/manifests/${{ matrix.images.build.tag }})
echo "IMAGE_STATUS=$IMAGE_STATUS" >> $GITHUB_OUTPUT
- name: Get image tags
id: image-tags
run: |
MAJOR_VERSION="$(echo '${{ matrix.images.build.tag }}' | cut -d '.' -f 1)"
MINOR_VERSION="$(echo '${{ matrix.images.build.tag }}' | cut -d '.' -f 2)"
echo "IMAGE_TAGS=${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:${{ matrix.images.build.tag }},${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:$MAJOR_VERSION.$MINOR_VERSION,${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:$MAJOR_VERSION,${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:latest" >> $GITHUB_OUTPUT
- name: Checks-out repository
uses: actions/checkout@v4
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }}
- name: Set up Docker buildx
uses: docker/setup-buildx-action@v3
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }}
- name: Set up QEMU (for multi platform build)
uses: docker/setup-qemu-action@v3
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }}
- name: Build docker image
uses: docker/build-push-action@v5
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }}
with:
context: ${{ matrix.images.build.context }}
file: ${{ matrix.images.build.dockerfile }}
tags: ${{ steps.image-tags.outputs.IMAGE_TAGS }}
target: ${{ matrix.images.build.target }}
platforms: ${{ env.PLATFORMS }}
push: true
provenance: false