-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (127 loc) · 4.55 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
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"
# 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")
# 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
if [[ $file == $service_dir/* ]]; then
CHANGED=true
echo "✨ Found changes in service: $service_name"
echo " Changed file: $file"
break
fi
done
# If changes detected, add to build matrix
if [ "$CHANGED" = true ]; then
echo "🔨 Adding $service_name to build matrix"
MATRIX="${MATRIX}${SEPARATOR}{\"service\": \"${service_name}\", \"context\": \"${service_dir#./}\"}"
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"