-
-
Notifications
You must be signed in to change notification settings - Fork 7
120 lines (102 loc) · 4.78 KB
/
buildx.yaml
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
name: buildx
on:
workflow_dispatch: # See https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch
inputs:
image_name:
description: Name of the image to build
required: true
image_tag:
description: Tag of the image to build
required: true
default: latest
pull_before_build:
description: Pull image before running build
required: true
default: "true"
jobs:
buildx:
name: buildx
runs-on: ubuntu-latest
env:
IMAGE_NAME: badouralix/${{ github.event.inputs.image_name }} # Not exactly the correct wording as per https://stackoverflow.com/questions/31115098/what-is-the-difference-between-an-image-and-a-repository
IMAGE_TAG: ${{ github.event.inputs.image_tag }}
WORKING_DIRECTORY: ${{ github.event.inputs.image_name }}/${{ github.event.inputs.image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v3 # See https://github.com/actions/checkout
- name: Validate event inputs
uses: actions/github-script@v6 # See https://github.com/actions/github-script
with:
script: |
for (const [key, value] of Object.entries(context.payload.inputs)) {
core.notice(value, {title: key})
}
if (context.ref !== 'refs/heads/main') {
core.setFailed(`Invalid git branch (expected 'refs/heads/main', got '${context.ref}').`)
return
}
const fs = require('fs')
const image_name = context.payload.inputs.image_name
const image_tag = context.payload.inputs.image_tag
if (!fs.existsSync(image_name) || !fs.statSync(image_name).isDirectory()) {
core.setFailed(`Invalid image name '${image_name}'.`)
return
}
if (!fs.existsSync(`${image_name}/${image_tag}`) || !fs.statSync(`${image_name}/${image_tag}`).isDirectory()) {
core.setFailed(`Invalid image tag '${image_tag}' with image name '${image_name}'.`)
return
}
- name: Set up qemu
uses: docker/setup-qemu-action@v2 # See https://github.com/docker/setup-qemu-action
- name: Set up buildx
uses: docker/setup-buildx-action@v2 # See https://github.com/docker/setup-buildx-action
- name: Login to dockerhub
uses: docker/login-action@v2 # See https://github.com/docker/login-action
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} # Get access token from https://hub.docker.com/settings/security
# Hack from https://stackoverflow.com/questions/58913512/how-to-give-github-action-the-content-of-a-file-as-input
- name: Read platforms
id: read_platforms
working-directory: ${{ env.WORKING_DIRECTORY }}
run: |
echo "platforms=$(cat platforms.txt)" >> $GITHUB_OUTPUT
# See also https://github.com/docker/metadata-action
- name: Generate labels
id: generate_labels
uses: actions/github-script@v6 # See https://github.com/actions/github-script
with:
script: |
const labels = []
labels.push(`com.github.actions.event_name=${context.eventName}`)
labels.push(`com.github.actions.job=${context.job}`)
labels.push(`com.github.actions.run_id=${context.runId}`)
labels.push(`com.github.actions.run_url=${context.payload.repository.html_url}/actions/runs/${context.runId}`)
labels.push(`org.opencontainers.image.created=${new Date().toISOString()}`)
labels.push(`org.opencontainers.image.documentation=${context.payload.repository.html_url}/tree/${context.sha.substr(0,7)}/${context.payload.inputs.image_name}/README.md`)
labels.push(`org.opencontainers.image.revision=${context.sha}`)
labels.push(`org.opencontainers.image.source=${context.payload.repository.html_url}`)
core.startGroup('Generated labels')
for (const label of labels) {
core.info(label)
}
core.endGroup()
core.setOutput('labels', labels.join('\n'))
- name: Run pre build hook
id: pre_build
working-directory: ${{ env.WORKING_DIRECTORY }}
run: |
if [[ -f hooks/pre_build ]]; then ./hooks/pre_build; fi
- name: Build and push
uses: docker/build-push-action@v4 # See https://github.com/docker/build-push-action
with:
context: ${{ env.WORKING_DIRECTORY }}
labels: |
${{ steps.generate_labels.outputs.labels }}
${{ steps.pre_build.outputs.extra_labels }}
platforms: ${{ steps.read_platforms.outputs.platforms }}
pull: ${{ github.event.inputs.pull_before_build }}
push: true
tags: |
${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
${{ steps.pre_build.outputs.extra_tags }}