-
Notifications
You must be signed in to change notification settings - Fork 0
162 lines (140 loc) Β· 5.3 KB
/
dynamic-service-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
name: Dynamic Service Docker Builds
on:
push:
branches:
- main
paths-ignore:
- "deployment/**"
- "gef-portal-scraper/**"
- "**.md"
pull_request:
branches:
- main
paths-ignore:
- "deployment/**"
- "gef-portal-scraper/**"
- "**.md"
jobs:
detect_changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
- 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: |
# Initialize variables
MATRIX="["
SEPARATOR=""
# List of directories to exclude (space-separated)
EXCLUDED_DIRS="deployment resources gef-portal-scraper"
echo "π Changed files in this push/PR:"
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo " β $file"
done
# Find all Dockerfiles in service directories
for dockerfile in $(find . -name "Dockerfile" -not -path "*/\.*"); do
# Get service directory (parent of Dockerfile)
service_dir=$(dirname "$dockerfile")
service_name=$(basename "$service_dir")
echo "π¦ Checking service: $service_name"
echo " β’ Directory: $service_dir"
# Skip if directory is in excluded list
skip=false
for excluded in $EXCLUDED_DIRS; do
if [[ "$service_dir" == *"$excluded"* ]]; then
skip=true
echo "βοΈ Skipping excluded directory: $service_dir"
break
fi
done
[ "$skip" = true ] && continue
# Check for changes in service directory
CHANGED=false
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
# Remove leading ./ from paths for consistent comparison
clean_file=$(echo "$file" | sed 's|^\./||')
clean_dir=$(echo "$service_dir" | sed 's|^\./||')
echo " β’ Comparing changed file: $clean_file"
echo " β’ With service dir: $clean_dir"
if [[ "$clean_file" == "$clean_dir"* ]]; then
CHANGED=true
echo "β¨ Found changes in service: $service_name"
echo " Changed file: $clean_file"
break
fi
done
# If changes detected, add to build matrix
if [ "$CHANGED" = true ]; then
echo "π¨ Adding $service_name to build matrix"
# Remove leading ./ from context path
context_path=$(echo "$service_dir" | sed 's|^\./||')
MATRIX="${MATRIX}${SEPARATOR}{\"service\": \"${service_name}\", \"context\": \"${context_path}\"}"
SEPARATOR=","
else
echo "βοΈ No changes detected for $service_name - skipping"
fi
done
MATRIX="${MATRIX}]"
echo "π Final build matrix:"
echo "$MATRIX" | jq '.'
if [ "$MATRIX" = "[]" ]; then
echo "β οΈ No services to build"
echo "matrix=[]" >> $GITHUB_OUTPUT
else
echo "π Services ready for build"
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 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 "REPO_OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Display build information
run: |
echo "ποΈ Building service: ${{ matrix.service }}"
echo " β’ Context: ${{ matrix.context }}"
echo " β’ Image name: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest"
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: ./${{ matrix.context }}
file: ./${{ matrix.context }}/Dockerfile
push: true
tags: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest
- name: Build status
run: |
echo "β
Successfully built and pushed: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest"