-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (101 loc) · 4.61 KB
/
build_and_deploy.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
---
name: Build and Deploy
on:
push:
branches:
- main
jobs:
define_images:
# Define a build matrix based on all changed *.hcl files so we can run packer only for image definitions that have actually changed
# So if e.g. ubuntu/focal/variables.auto.pkrvars.hcl or ubuntu/focal/src-ubuntu.pkr.hcl changes, add "ubuntu/focal" to the matrix
# Also ensure that if an image template (for instance, the general template for ubuntu images 'ubuntu/src-ubuntu.pkr.hcl') changes,
# an entry is added to the matrix for each image definition that relies on (i.e. symlinks) it.
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: "${{ github.repository }}"
- name: Get changed image definitions
# Find changed .hcl files in all <os>/<version> directories
id: changed-images-yaml
uses: tj-actions/changed-files@v39
with:
path: "${{ github.repository }}"
dir_names: true
dir_names_max_depth: 3
quotepath: false
files_yaml: |
images:
- '*/*/*.hcl'
- name: Get changed image definition templates
# Find all changed base templates in all <os>/ directories
id: changed-symlink-yaml
uses: tj-actions/changed-files@v39
with:
path: "${{ github.repository }}"
quotepath: false
files_yaml: |
symlinks:
- '*/*.hcl'
- name: Get image directories based on changed symlinks
# For every changed template in an <os> directory, collect all <os>/<version> directories that contain a symlink to the template.
# For instance, if ubuntu/src-ubunut.pkr.hcl changes, find all subdirectories of 'ubuntu' that contain links to that file
# -> the result would be e.g. 'ubuntu/focal ubuntu/jammy ubuntu/focal_desktop'
id: get-symlink-dirs
working-directory: "${{ github.repository }}"
run: |
# Loop over each changed <os>/*.hcl file
for file in ${{ steps.changed-symlink-yaml.outputs.symlinks_all_changed_files }}; do
dir=$(dirname $file)
links=$(find "$dir" -type l) # Find all symlinks under the current $file's directory
for link in $links; do
# If this link has $file as a target, add the link's directory to the array of changed template directories
if [[ $(readlink -f "$link") == $(realpath "$file") ]]; then
CHANGED="$CHANGED $(dirname $link)"
fi
done
done
# Store the results
echo changed_dirs="$CHANGED" >> "$GITHUB_OUTPUT"
- id: set-images-matrix
run: |
# First add together the directories found in the changed-images-yaml and changed-templates-yaml steps into a single array
ALL_DIRS="${{ steps.changed-images-yaml.outputs.images_all_changed_files }} ${{ steps.get-changed-templates.outputs.changed_dirs }}"
# Now construct a unique array out of ALL_DIRS
MATRIX=($(echo "$ALL_DIRS" | xargs -n 1 | sort -u))
# Next create a valid json array out of the bash array using jq
MATRIX=$(printf '%s\n' "${MATRIX[@]}" | jq -R . | jq -c -s .)
# Store the results
echo images=$MATRIX >> $GITHUB_OUTPUT
echo matrix={\"images\": $MATRIX} >> $GITHUB_OUTPUT
outputs:
images: ${{ steps.set-images-matrix.outputs.images }}
images_matrix: ${{ steps.set-images-matrix.outputs.matrix }}
build_and_deploy:
needs: define_images
runs-on: ubuntu-latest
if: ${{ needs.define_images.outputs.images != '[""]' && needs.define_images.outputs.images_matrix != '' }}
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.define_images.outputs.images_matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
id: build-image
run: IMG=${{ matrix.images }} ./pack.sh docker
- name: Get image name and tag # grep in the image directory's variables file to find the image name/tag to push to
id: get-image-name
run: |
echo "docker_repo=`grep docker_repo ${{ matrix.images }}/variables.auto.pkrvars.hcl | awk '{print $3}'`" >> "$GITHUB_OUTPUT"
- name: Push image
run: docker push ${{ steps.get-image-name.outputs.docker_repo }}