-
Notifications
You must be signed in to change notification settings - Fork 9
129 lines (118 loc) · 4.35 KB
/
container_build_child.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
---
# This workflow is triggered by the parent workflow and must not be triggered directly.
name: Reusable build container workflow
env:
# BUILDX_NO_DEFAULT_ATTESTATIONS must be set to build only arm64 and amd64 images.
# The devcontainers/[email protected] build will fail if this env variable is not set.
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
on:
workflow_call:
inputs:
container_name:
required: false
type: string
default: lab-base
image_tags:
required: false
type: string
platform:
required: false
type: string
default: linux/arm64/v8,linux/amd64
from_image:
required: false
type: string
default: ghcr.io/aristanetworks/avd/universal
from_variant:
required: false
type: string
default: latest
username:
required: false
type: string
default: avd
user_id:
required: false
type: string
default: 1000
group_id:
required: false
type: string
default: 1000
git_init:
required: false
type: boolean
default: true
clab_version:
required: false
type: string
container_revision:
required: false
type: string
default: latest
jobs:
build_image:
runs-on: ubuntu-22.04
steps:
- name: Starting container build
run: echo "Starting container build. Be patient. 🐢"
- name: Checkout code ✅
uses: actions/checkout@v4
- name: Build image tags 🏷️
id: build-tags
run: |
if [ -z "${{ inputs.image_tags }}" ]; then
echo "No image tags provided. Building tags."
echo "image_tags=${{ inputs.from_variant }}-clab${{ inputs.clab_version }}-rev${{ inputs.container_revision }}" >> $GITHUB_OUTPUT
else
echo "Using provided image tags."
echo "image_tags=${{ inputs.image_tags }}" >> $GITHUB_OUTPUT
fi
- name: Convert Github repository name to lowercase ⬇️
id: gh_repo
run: echo "name_lowcase=${GITHUB_REPOSITORY,,}" >> $GITHUB_OUTPUT
- name: Setup QEMU for multi-arch builds 🏗️
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ inputs.platform }}
- name: Setup Docker buildX for multi-arch builds 🏗️
uses: docker/setup-buildx-action@v3
- name: Login to the container registry 🗝️
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check if image already exists on container registry 🕵️
id: check-image
run: |
echo "exit_code=0" >> $GITHUB_OUTPUT
if [ "${{ inputs.container_name }}" == "dev" ]; then
# always rebuild dev image, but check if already exists for other images
echo "exit_code=1" >> $GITHUB_OUTPUT
else
IFS=',' read -ra image_tags <<< ${{ steps.build-tags.outputs.image_tags }}
for image_tag in "${image_tags[@]}"; do
echo "Checking if ghcr.io/${{ steps.gh_repo.outputs.name_lowcase }}/${{ inputs.container_name }}:$image_tag already exists."
docker manifest inspect ghcr.io/${{ steps.gh_repo.outputs.name_lowcase }}/${{ inputs.container_name }}:$image_tag || echo "exit_code=1" >> $GITHUB_OUTPUT
done
fi
- name: Pre-build dev container image 🔨
uses: devcontainers/[email protected]
# Only build and push the image if at least one of the image tags does not exist.
if: steps.check-image.outputs.exit_code != 0
env:
FROM_IMAGE: ${{ inputs.from_image }}
FROM_VARIANT: ${{ inputs.from_variant }}
USERNAME: ${{ inputs.username }}
UID: ${{ inputs.user_id }}
GID: ${{ inputs.group_id }}
CLAB_VERSION: ${{ inputs.clab_version }}
CEOS_LAB_VERSION_ARG: ${{ inputs.ceos_lab_version }}
GIT_INIT_ARG: ${{ inputs.git_init }}
with:
subFolder: containers/${{ inputs.container_name }}
imageName: ghcr.io/${{ steps.gh_repo.outputs.name_lowcase }}/${{ inputs.container_name }}
imageTag: ${{ steps.build-tags.outputs.image_tags }}
platform: ${{ inputs.platform }}
push: always