-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (134 loc) · 4.6 KB
/
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
name: Container Images
on:
push:
branches:
- main
pull_request:
release:
types: [published] # Corrected 'type' to 'types'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
TARGET_DIR: ./.silverback-images
DOCKERFILE_PATTERN: Dockerfile.*
jobs:
generate_matrix:
name: Check and Setup Matrix
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
outputs:
matrix: ${{ steps.find_dockerfiles.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if Directory Exists
id: check_dir
run: |
if [ -d "${{ env.TARGET_DIR }}" ]; then
echo "Directory exists."
echo "directory_exists=true" >> $GITHUB_OUTPUT
else
echo "Directory does not exist. Creating..."
mkdir -p "${{ env.TARGET_DIR }}"
echo "directory_exists=false" >> $GITHUB_OUTPUT
fi
- name: Generate Files
id: gen_files
if: steps.check_dir.outputs.directory_exists == 'false'
run: |
python -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
silverback build --generate
- name: Verify Dockerfiles Exist
run: |
if [ ! -d "${{ env.TARGET_DIR }}" ]; then
echo "Directory '${{ env.TARGET_DIR }}' does not exist. Exiting."
exit 1
fi
- name: Find Dockerfiles
id: find_dockerfiles
run: |
# Find all Dockerfiles matching the pattern
dockerfiles=$(find "${{ env.TARGET_DIR }}" -type f -name "${{ env.DOCKERFILE_PATTERN }}" | sort)
echo "Found Dockerfiles:"
echo "${dockerfiles}"
dockerfile_array=()
for df in $dockerfiles; do
name=$(basename "$df" | sed 's/Dockerfile\.//')
tag=${name}
# Properly escape quotes and ensure JSON strings are correctly formatted
dockerfile_array+=("{\"file\":\"$df\",\"name\":\"$name\",\"tag\":\"$tag\"}")
done
# Check if any Dockerfiles were found
if [ "${#dockerfile_array[@]}" -eq 0 ]; then
echo "No Dockerfiles found in '${{ env.TARGET_DIR }}'. Exiting..."
exit 1
fi
# Generate a valid JSON matrix
matrix=$(printf '[%s]' "$(IFS=,; echo "${dockerfile_array[*]}")")
echo "Matrix JSON: $matrix"
# Optional: Validate JSON structure using jq (if available)
echo "$matrix" | jq empty
# Set the matrix output
echo "matrix=$matrix" >> $GITHUB_OUTPUT
- name: Upload Generated Files as Artifact
if: steps.check_dir.outputs.directory_exists == 'false'
uses: actions/upload-artifact@v4
with:
name: generated-files
path: ${{ env.TARGET_DIR }}/*
build-and-push:
name: Build and Push Docker Images
needs: generate_matrix
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
strategy:
matrix:
include: ${{ fromJson(needs.generate_matrix.outputs.matrix) }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Display Matrix Variables
run: |
echo "Building Dockerfile: ${{ matrix.file }}"
echo "Image Name: ${{ matrix.name }}"
echo "Image Tag: ${{ matrix.tag }}"
- name: Set Lowercase Repository Owner
id: lowercase_owner
run: |
LOWERCASE_OWNER=$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')
echo "LOWERCASE_OWNER=$LOWERCASE_OWNER" >> $GITHUB_ENV
- name: Download Generated Files Artifact
uses: actions/download-artifact@v4
with:
name: generated-files
path: ${{ env.TARGET_DIR }}
- name: Log into GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push ${{ matrix.name }} Image
uses: docker/build-push-action@v4
with:
context: .
file: ${{ matrix.file }}
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.REGISTRY }}/${{ env.LOWERCASE_OWNER }}/${{ matrix.tag }}:latest