-
Notifications
You must be signed in to change notification settings - Fork 4
152 lines (132 loc) · 4.56 KB
/
build-images.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
name: "build images"
on:
workflow_dispatch:
workflow_call:
push:
paths:
- '.github/workflows/build-images.yml'
- '**/Dockerfile'
- 'docker-compose.yml'
env:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
jobs:
build-info:
runs-on: ubuntu-latest
outputs:
repo-owner: ${{ steps.repo-owner.outputs.result }}
tags: ${{ steps.image-tags.outputs.image-tags }}
build-config: ${{ steps.build-info.outputs.result }}
steps:
#github forces lower case for the image name
- name: Get lowercase repo owner name
uses: actions/github-script@v7
id: repo-owner
with:
result-encoding: string
script: |
return context.repo.owner.toLowerCase()
- uses: actions/checkout@v4
- name: Set nightly tag if commit was on main
id: add-nightly-tag
if: startsWith(github.ref, 'refs/heads/main')
run: |
echo "nightly-tag=nightly" | tr -d "\n" >> $GITHUB_OUTPUT
- name: Set latest tag if its a tag
id: add-latest-tag
if: startsWith(github.ref, 'refs/tags/')
run: |
echo "latest-tag=latest" | tr -d "\n" >> $GITHUB_OUTPUT
- uses: actions/github-script@v7
id: get-tag
if: startsWith(github.ref, 'refs/tags/')
with:
result-encoding: string
script: |
return context.payload.ref.replace('refs/tags/', '')
- name: concat tags to list
id: image-tags
run: |
TAGS=$(cat <<-END
[
"${{ github.sha }}",
"${{ steps.add-nightly-tag.outputs.nightly-tag }}",
"${{ steps.add-latest-tag.outputs.latest-tag }}",
"${{ steps.get-tag.outputs.result }}"
]
END
)
TAGS=$(echo $TAGS | jq -c 'map(select(length > 0))')
echo "image-tags=$TAGS" | tr -d "\n" >> $GITHUB_OUTPUT
- name: Get build info
id: build-info
run: |
set -x
sudo snap install yq
export REPO_NAME=$(basename $(pwd))
echo "services:" > /tmp/docker-compose.yml
for i in $(find . -name docker-compose.yml); do
docker compose -f $i config | yq .services | sed 's/^/ /' >> /tmp/docker-compose.yml
done
yq_pipe='.services.[]'
yq_pipe=$yq_pipe'| select(.build != null)'
yq_pipe=$yq_pipe'| [.build * {"image": .image}]'
yq_pipe=$yq_pipe'| map(.dockerfile=.context + "/" + .dockerfile)'
BUILD_INFO=$(cat /tmp/docker-compose.yml | yq "$yq_pipe" -o=json)
BUILD_INFO=$(echo $BUILD_INFO | jq -c -s add | sed "s|:local||g")
echo "result=$BUILD_INFO" | tr -d "\n" >> $GITHUB_OUTPUT
env:
REPO_OWNER: ${{ steps.repo-owner.outputs.result }}
VERSION: local
build-image:
runs-on: ubuntu-latest
needs:
- build-info
strategy:
fail-fast: false
matrix:
build-config: ${{ fromJSON(needs.build-info.outputs.build-config) }}
env:
tags: ${{ needs.build-info.outputs.tags }}
repo-owner: ${{ needs.build-info.outputs.repo-owner }}
steps:
- uses: actions/checkout@v4
- run: |
echo "inputs:"
cat <<-HEREDOC
${{ toJSON(inputs) }}
HEREDOC
echo "build config:"
cat <<-HEREDOC
${{ toJSON(matrix.build-config) }}
HEREDOC
echo "build tags:"
cat <<-HEREDOC
${{ env.tags }}
HEREDOC
echo "build repo owner:"
cat <<-HEREDOC
${{ env.repo-owner }}
HEREDOC
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/github-script@v7
id: expand-tags
with:
script: |
return JSON.parse('${{ env.tags }}').map(tag => `${{ matrix.build-config.image }}:${ tag }`)
- name: Build and push image
uses: docker/build-push-action@v6
with:
push: true
file: ${{ matrix.build-config.dockerfile }}
tags: ${{ join(fromJSON(steps.expand-tags.outputs.result)) }}
context: ${{ matrix.build-config.context }}
cache-from: type=gha,scope=${{ matrix.build-config.image }}
cache-to: type=gha,scope=${{ matrix.build-config.image }},mode=max