-
Notifications
You must be signed in to change notification settings - Fork 1
155 lines (153 loc) · 5.42 KB
/
build-image.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
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
name: Build Image On Release
on:
workflow_call:
inputs:
target-arch:
required: true
type: string
repo_owner:
required: true
type: string
app_name:
required: true
type: string
release_type:
required: true
type: string
release_url:
required: false
type: string
release_name:
required: false
type: string
secrets:
OP_SERVICE_ACCOUNT_TOKEN:
required: true
jobs:
bake:
runs-on: ubuntu-22.04
if: ${{ github.actor != 'dependabot[bot]' }}
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Docker meta
id: docker_meta
uses: docker/metadata-action@v5.6.1
with:
images: |
ghcr.io/${{ inputs.repo_owner }}/${{ inputs.app_name }}
labels: |
org.opencontainers.image.title=${{ inputs.app_name }}
org.opencontainers.image.description=${{ inputs.app_name }}
org.opencontainers.image.vendor=${{ inputs.repo_owner }}
tags: |
type=schedule
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.9.0
-
name: Install Cosign
uses: sigstore/cosign-installer@v3.8.1
-
name: Login to GitHub Container Registry
uses: docker/login-action@v3.3.0
with:
registry: ghcr.io
username: ${{ inputs.repo_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Generate build date
id: gen_date
run: |
BUILD_DATE=$(date '+%Y-%m-%dT%H:%M:%S%:z')
echo "**** Setting build date to $BUILD_DATE ****"
echo "build_date=${BUILD_DATE}" >> $GITHUB_OUTPUT
-
name: Generate release version
id: gen_release
run: |
if [ -z ${{ github.event.release.tag_name }} ]; then
IMAGE_VERSION=$(curl -s "https://api.github.com/repos/${{ inputs.repo_owner }}/docker-${{ inputs.app_name }}/releases" | jq -r '(sort_by(.published_at) | .[-1].tag_name)?')
if [ -z $IMAGE_VERSION ] || [ $IMAGE_VERSION == null ]; then
case ${{ inputs.release_type }} in
github)
IMAGE_VERSION=$(curl -sX GET "${{ inputs.release_url }}/releases/latest" | awk '/tag_name/{print $4;exit}' FS='[""]');
;;
github_tag)
IMAGE_VERSION=$(curl -sX GET "${{ inputs.release_url }}/tags" | jq -r 'first(.[] | select(.name | contains("${{ inputs.release_name }}") )) | .name');
;;
github_commit)
IMAGE_VERSION=$(curl -sL "${{ inputs.release_url }}" | jq -r 'first(.[])' | cut -c1-8);
;;
alpine)
IMAGE_VERSION=$(curl -sL "http://dl-cdn.alpinelinux.org/alpine/${{ inputs.release_url }}/x86_64/APKINDEX.tar.gz" | tar -xz -C /tmp && awk '/^P:'"${{ inputs.release_name }}"'$/,/V:/' /tmp/APKINDEX | sed -n 2p | sed 's/^V://');
;;
script)
if test -f "./get-version.sh"; then
IMAGE_VERSION=$(bash "./get-version.sh");
fi
;;
esac
fi
else
IMAGE_VERSION=${{ github.event.release.tag_name }}
fi
APP_VERSION=$(echo ${IMAGE_VERSION} | awk -F'-ls' '{print $1}')
echo "**** Setting release tag to $IMAGE_VERSION ****"
echo "tag_name=${IMAGE_VERSION}" >> $GITHUB_OUTPUT
echo "app_version=${APP_VERSION}" >> $GITHUB_OUTPUT
-
name: Build and push
id: build_push
uses: docker/bake-action@v6.5.0
with:
files: |
./docker-bake.hcl
${{ steps.docker_meta.outputs.bake-file }}
set: |
image.args.BUILD_DATE=${{ steps.gen_date.outputs.build_date }}
image.args.VERSION=${{ steps.gen_release.outputs.tag_name }}
image.args.APP_VERSION=${{ steps.gen_release.outputs.app_version }}
targets: ${{ inputs.target-arch }}
push: true
provenance: false
sbom: true
-
name: Get Digest
id: get_digest
env:
BAKE_METADATA: ${{ steps.build_push.outputs.metadata }}
run: |
DIGEST=$(jq -rc '.[]."containerimage.digest"' <<< "${BAKE_METADATA}")
echo "Image digest: ${DIGEST}"
echo "image_digest=${DIGEST}" >> $GITHUB_OUTPUT
-
name: Load Key
id: op-load-key
uses: 1password/load-secrets-action@v2
with:
export-env: true
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
COSIGN_PRIVATE_KEY: op://Labs/labs-sigstore-key/notesPlain
COSIGN_PASSWORD: op://Labs/labs-sigstore-pass/password
-
name: Sign image with a key
run: |
images=""
for tag in ${TAGS}; do
images+="${tag}@${DIGEST} "
done
cosign sign --yes --key env://COSIGN_PRIVATE_KEY ${images}
env:
TAGS: ${{ steps.docker_meta.outputs.tags }}
DIGEST: ${{ steps.get_digest.outputs.image_digest }}