-
Notifications
You must be signed in to change notification settings - Fork 6
173 lines (147 loc) · 6 KB
/
Copy pathdocker-publish.yml
File metadata and controls
173 lines (147 loc) · 6 KB
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
165
166
167
168
169
170
171
172
173
name: Docker
on:
push:
branches: ["dev", "main"]
tags: ["v*.*.*"]
pull_request:
branches: ["dev", "main"]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
has-changes: ${{ steps.set-matrix.outputs.has-changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive
- name: Detect changed paths
id: set-matrix
run: |
# Initialize empty matrix
MATRIX_INCLUDE="[]"
HAS_CHANGES=false
# Function to add to matrix
add_to_matrix() {
local context=$1
local suffix=$2
local dockerfile=$3
local component=$4
# Use jq to robustly add the object to the array
MATRIX_INCLUDE=$(echo "$MATRIX_INCLUDE" | jq -c ". + [{\"context\":\"$context\",\"image-suffix\":\"$suffix\",\"dockerfile\":\"$dockerfile\",\"component\":\"$component\"}]")
HAS_CHANGES=true
}
# Check if we should build everything (tags, PRs, or workflow change)
# Check if we should build everything
FORCE_BUILD=false
BASE_REF="HEAD~1"
# Intelligent Tag Handling
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
# Try to find the previous tag (abbrev=0 to get clean tag name)
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
if [ -z "$PREV_TAG" ]; then
# No previous tag found (first release), force full build
echo "No previous tag found. Forcing full build."
FORCE_BUILD=true
else
# Diff against the previous tag
BASE_REF="$PREV_TAG"
echo "Comparing this tag (${{ github.ref_name }}) against previous tag: $PREV_TAG"
fi
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
# For PRs, we currently force build to ensure stability,
# though this could be optimized to compare against target branch in future.
FORCE_BUILD=true
fi
# Check for workflow changes (always rebuild if pipeline changes)
# We use BASE_REF here to catch workflow changes in the range
if git diff --name-only "$BASE_REF" HEAD | grep -q "\.github/workflows/docker-publish\.yml"; then
echo "Workflow file changed. Forcing full build."
FORCE_BUILD=true
fi
if [ "$FORCE_BUILD" = true ]; then
add_to_matrix "." "" "Dockerfile" "core"
add_to_matrix "./rf-engine" "-rf-engine" "./rf-engine/Dockerfile" "rf-engine"
add_to_matrix "opentopodata" "-opentopodata" "opentopodata/docker/Dockerfile" "opentopodata"
else
echo "Checking for changes relative to: $BASE_REF"
# Core/root image changes
if git diff --name-only "$BASE_REF" HEAD | grep -E '(Dockerfile|\.go$|\.py$|requirements|package\.json|\.ts$|\.js$)' | grep -qv "rf-engine\|opentopodata"; then
add_to_matrix "." "" "Dockerfile" "core"
fi
# rf-engine changes
if git diff --name-only "$BASE_REF" HEAD | grep -q "^rf-engine/"; then
add_to_matrix "./rf-engine" "-rf-engine" "./rf-engine/Dockerfile" "rf-engine"
fi
# opentopodata changes
if git diff --name-only "$BASE_REF" HEAD | grep -E -q "^opentopodata(/|$)"; then
add_to_matrix "opentopodata" "-opentopodata" "opentopodata/docker/Dockerfile" "opentopodata"
fi
fi
# finalize matrix JSON
MATRIX=$(echo "{}" | jq -c ".include = $MATRIX_INCLUDE")
echo "Detected changes: $HAS_CHANGES"
echo "Matrix: $MATRIX"
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
echo "has-changes=$HAS_CHANGES" >> $GITHUB_OUTPUT
build:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.has-changes == 'true' }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
- name: Verify Submodules
run: |
git submodule update --init --recursive
ls -R opentopodata
- name: Lowercase Image Name
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image-suffix }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image-suffix }}:buildcache
type=gha
cache-to: type=gha,mode=max