-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (142 loc) Β· 5.61 KB
/
dynamic-build.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
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
161
162
163
164
name: B&P Dynamic Docker Images
on:
push:
branches:
- main
paths:
- ".github/workflows/dynamic-build.yml"
- "deployment/images/**"
pull_request:
branches:
- main
paths:
- ".github/workflows/dynamic-build.yml"
- "deployment/images/**"
jobs:
detect_changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out the repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all history for comparing changes
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
with:
files: deployment/images/**
- name: Display all changed files
run: |
echo "π Changed files in this push/PR:"
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo " β $file"
done
- name: Set up build matrix
id: set-matrix
run: |
cd deployment/images
echo "π Scanning directory: $(pwd)"
# Initialize matrix
MATRIX="["
SEPARATOR=""
# Find and display all Dockerfiles
echo "π³ Found Dockerfiles:"
find . -type f -name "Dockerfile.*" | while read -r dockerfile; do
echo " β $dockerfile"
done
# Process each Dockerfile
for dockerfile in $(find . -type f -name "Dockerfile.*"); do
# Remove leading ./ if present
dockerfile_clean=$(echo "$dockerfile" | sed 's|^./||')
image_name=$(echo "$dockerfile_clean" | sed 's/.*Dockerfile\.//')
context_dir=$(dirname "$dockerfile_clean")
if [ "$context_dir" = "." ]; then
context_path="deployment/images"
else
context_path="deployment/images/$context_dir"
fi
echo "β‘ Processing $dockerfile_clean"
echo " β’ Image name: $image_name"
echo " β’ Context directory: $context_dir"
echo " β’ Full context path: $context_path"
# Check for changes
CHANGED=false
echo " β’ Checking for changes in context..."
# First, check if the Dockerfile itself changed
if [[ "${{ steps.changed-files.outputs.all_changed_files }}" == *"deployment/images/$dockerfile_clean"* ]]; then
CHANGED=true
echo " β Dockerfile changed"
fi
# Then check context directory
if [ ! "$CHANGED" = true ] && [ "$context_dir" != "." ]; then
for changed_file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ $changed_file == $context_path/* ]]; then
CHANGED=true
echo " β Found change in context: $changed_file"
break
fi
done
fi
# If changed, add to matrix
if [ "$CHANGED" = true ]; then
echo " β
Changes detected - adding to build matrix"
MATRIX="${MATRIX}${SEPARATOR}{\"image\": \"${image_name}\", \"dockerfile\": \"${dockerfile_clean}\", \"context\": \"${context_dir}\"}"
SEPARATOR=","
else
echo " βοΈ No changes detected - skipping"
fi
done
MATRIX="${MATRIX}]"
echo "π Final build matrix:"
echo "$MATRIX" | jq '.'
if [ "$MATRIX" = "[]" ]; then
echo "β οΈ No changes detected in any Docker contexts - skipping builds"
echo "matrix=[]" >> $GITHUB_OUTPUT
else
echo "π Changes detected - proceeding with builds"
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
fi
build_and_push:
needs: detect_changes
if: ${{ needs.detect_changes.outputs.matrix != '[]' }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
include: ${{ fromJson(needs.detect_changes.outputs.matrix) }}
steps:
- name: Check out the repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Define lowercase repository owner
id: repo
run: |
echo "Converting ${{ github.repository_owner }} to lowercase..."
echo "REPO_OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Display build information
run: |
echo "ποΈ Building image: ${{ matrix.image }}"
echo " β’ Dockerfile: ${{ matrix.dockerfile }}"
echo " β’ Context: ${{ matrix.context }}"
echo " β’ Full image name: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest"
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: "./deployment/images/${{ matrix.context }}"
file: "./deployment/images/${{ matrix.dockerfile }}"
push: true
tags: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest
- name: Build status
run: |
echo "β
Successfully built and pushed: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest"