-
Notifications
You must be signed in to change notification settings - Fork 0
160 lines (148 loc) · 5.99 KB
/
containers.build.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
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
156
157
158
159
160
name: "Containers: Test-and-Build"
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "apps/**"
- "dev/**"
- "base/**"
- ".github/workflows/containers.build.yaml"
pull_request:
paths:
- "apps/**"
- "dev/**"
- "base/**"
- ".github/workflows/containers.build.yaml"
# Detect which folders in project-root (which contain the containers) contain changes
jobs:
changes:
name: Get changes
runs-on: ubuntu-latest
outputs:
matrix: '{"container": ${{ env.containers }} }'
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- uses: dorny/paths-filter@v3
id: filter
with:
list-files: json
filters: |
changed:
- 'apps/**'
- 'dev/**'
- 'base/**'
- run: echo '${{ toJson(steps.filter.outputs) }}' > changes.json
- id: reduce
run: |
CONTAINERS=$(jq --raw-output '.changed_files | fromjson | .[] |= sub("(?<filepath>(?<first_directory>(?<root1>[/]?)[^/]+/)(?<second_directory>(?<root2>[/]?)[^/]+)(?<extra_paths>.+))"; "\(.second_directory)") | unique' changes.json)
echo "containers<<EOF" >> $GITHUB_ENV
echo "${CONTAINERS}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
hadolint:
name: Run hadolint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: hadolint
uses: reviewdog/action-hadolint@2d90b15b2b969e507fc2a31af6e29ca19a0f9516 # v1.45.0
if: github.event_name == 'pull_request'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-review
filter_mode: diff_context
fail_on_error: true
build:
permissions:
actions: read # for detecting the Github Actions environment.
packages: write # for uploading attestations.
name: Build
runs-on: ubuntu-latest
needs:
- hadolint
- changes
strategy:
matrix: ${{ fromJson(needs.changes.outputs.matrix) }}
fail-fast: false
if: "!contains(github.event.head_commit.message, '[ci-skip]') && needs.changes.outputs.matrix != '' && toJson(fromJson(needs.changes.outputs.matrix)) != '[]'"
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
# Define if tests and push should be run against which versions/platforms
- name: Prepare
id: prep
run: |
if test -f "./apps/${{ matrix.container }}/Dockerfile"; then
CATEGORY="apps"
VERSION=$(cat ./${CATEGORY}/${{ matrix.container }}/VERSION || echo "undef")
PLATFORM=$(cat ./${CATEGORY}/${{ matrix.container }}/PLATFORM || echo "linux/amd64")
fi
echo "category=${CATEGORY}" >> $GITHUB_OUTPUT
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "platform=${PLATFORM}" >> $GITHUB_OUTPUT
if test -f "./${CATEGORY}/${{ matrix.container }}/goss.yaml"; then
echo 'goss=true' >> $GITHUB_OUTPUT
else
echo 'goss=false' >> $GITHUB_OUTPUT
fi
if [ "${{github.event_name}}" == "pull_request" ]; then
echo 'push=false' >> $GITHUB_OUTPUT
echo 'cache_from="type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ matrix.container }}:buildcache"' >> $GITHUB_OUTPUT || echo 'cache_from=""' >> $GITHUB_OUTPUT
echo 'cache_to=""' >> $GITHUB_OUTPUT
else
echo 'push=true' >> $GITHUB_OUTPUT
echo 'cache_from="type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ matrix.container }}:buildcache"' >> $GITHUB_OUTPUT
echo 'cache_to="type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ matrix.container }}:buildcache,mode=max"' >> $GITHUB_OUTPUT
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3
- name: Log into registry GHCR
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
if: github.event_name != 'pull_request'
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: meta
uses: docker/metadata-action@359e915ab3220d88514ed693793a5cc795192aa6 # v5
with:
images: |
ghcr.io/${{ github.repository_owner }}/${{ matrix.container }}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3
with:
install: true
version: latest
driver-opts: image=moby/buildkit:latest
# Push if not a PR, otherwise just test the build process for all requested platforms
- name: Build and Push
id: push
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6
with:
build-args: |
VERSION=${{ steps.prep.outputs.version }}
CONTAINER_NAME=${{ matrix.container }}
CONTAINER_VER=${{ steps.prep.outputs.version }}
context: .
platforms: ${{ steps.prep.outputs.platform }}
file: ./${{ steps.prep.outputs.category }}/${{ matrix.container }}/Dockerfile
push: ${{ steps.prep.outputs.push }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: ${{ steps.prep.outputs.cache_from }}
cache-to: ${{ steps.prep.outputs.cache_to }}
tags: ${{ steps.meta.outputs.tags }}
container-build-complete:
needs: [build]
name: Container Build Completed
runs-on: ubuntu-latest
steps:
- name: complete message
run: echo "Container Build and Tests Completed Successfully"
# NOTE
# many of this code was copied from the great workflow of truechats/containers
# https://github.com/truecharts/containers/blob/master/.github/workflows/containers.build.yaml