-
Notifications
You must be signed in to change notification settings - Fork 0
103 lines (99 loc) · 3.41 KB
/
build.yaml
File metadata and controls
103 lines (99 loc) · 3.41 KB
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: Create and publish Docker images
on:
push:
branches: ["main"]
paths:
- "images/**"
- ".github/workflows/build.yaml"
workflow_dispatch:
inputs:
imageName:
description: 'Name of the image to build (leave empty to build all changed images)'
required: false
default: ''
env:
REGISTRY: ghcr.io
jobs:
find-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Get changed files
id: getfile
uses: lots0logs/gh-action-get-changed-files@2.2.2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Determine changed images or rebuild all
id: set-matrix
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
# Special Case for workflow_dispatch
if [ -n "${{ github.event.inputs.imageName }}" ]; then
images=($(ls -d images/* | sed 's|images/||'))
matrix_value=$(echo "[\"${images[@]}\"]" | sed 's/ /","/g')
else
matrix_value="[\"${{ github.event.inputs.imageName }}\"]"
fi
else
all_files=$(echo '${{ steps.getfile.outputs.all }}' | jq -r '.[]')
if echo "$all_files" | grep -q ".github/workflows/build.yaml"; then
images=($(ls -d images/* | sed 's|images/||'))
matrix_value=$(echo "[\"${images[@]}\"]" | sed 's/ /","/g')
else
images=()
for file in $all_files
do
if [[ $file == images/* ]]; then
image_name=$(echo $file | cut -d'/' -f2)
images+=("$image_name")
fi
done
# Removing duplicate entries
images=($(echo "${images[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
matrix_value="[\"${images[*]}\"]"
fi
fi
echo "matrix=${matrix_value}" >> $GITHUB_OUTPUT
build-and-push-image:
needs: find-changes
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
image: ${{fromJson(needs.find-changes.outputs.matrix)}}
permissions:
contents: read
packages: write
env:
IMAGE_NAME: ${{ github.repository_owner }}/example-${{ matrix.image }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@4c1b68d83ad20cc1a09620ca477d5bbbb5fa14d0
with:
context: ./images/${{ matrix.image }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64