-
Notifications
You must be signed in to change notification settings - Fork 2
108 lines (97 loc) · 4.48 KB
/
docker-image-local.yml
File metadata and controls
108 lines (97 loc) · 4.48 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
name: Backend Docker build
on:
workflow_run:
workflows: ["Merge main into target"]
types:
- completed
workflow_dispatch:
inputs:
target_branch:
description: 'Target branch to compare against main (default: local)'
required: false
default: 'local'
permissions:
contents: read
jobs:
detect-and-build:
if: ${{ (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine target branch
if: ${{ github.event_name != 'workflow_dispatch' }}
run: |
set -euo pipefail
# Prefer a manually-dispatched input, then PR base (if present), then workflow_run.head_branch
if command -v jq >/dev/null 2>&1; then
TARGET=$(jq -r '.inputs.target_branch // .workflow_run.pull_requests[0].base.ref // .workflow_run.head_branch // empty' "$GITHUB_EVENT_PATH" || true)
else
# jq not available; leave TARGET empty (jq is available on ubuntu-latest)
TARGET=""
echo "jq not found; TARGET will be empty"
fi
if [ -z "${TARGET:-}" ]; then
echo "TRIGGER_BRANCH=local" >> $GITHUB_ENV
echo "Determined TRIGGER_BRANCH=local"
else
echo "TRIGGER_BRANCH=$TARGET" >> $GITHUB_ENV
echo "Determined TRIGGER_BRANCH=$TARGET"
fi
- name: Detect backend changes between target and main
if: ${{ github.event_name != 'workflow_dispatch' }}
id: detect
run: |
set -euo pipefail
TARGET=${TRIGGER_BRANCH:-local}
echo "Detecting changes related to backend/ for workflow run. Target branch: $TARGET"
# Always fetch main so we have the merged commit available
git fetch origin main
# Prefer to use the workflow_run.head_sha (the commit produced by the merge/run)
if command -v jq >/dev/null 2>&1; then
HEAD_SHA=$(jq -r '.workflow_run.head_sha // empty' "$GITHUB_EVENT_PATH" || true)
else
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
fi
CHANGED=""
if [ -n "${HEAD_SHA:-}" ]; then
echo "Found workflow head SHA: $HEAD_SHA — attempting to list files changed in that commit"
# Try to show files for that SHA. If it's not present locally, try fetching it (fetching main should normally fetch it).
if git rev-parse --verify "$HEAD_SHA" >/dev/null 2>&1; then
CHANGED=$(git show --name-only --pretty="" "$HEAD_SHA" || true)
else
git fetch origin "$HEAD_SHA" || true
CHANGED=$(git show --name-only --pretty="" "$HEAD_SHA" || true)
fi
fi
# If we didn't get any changed files from the head SHA, fall back to comparing branches
if [ -z "${CHANGED:-}" ]; then
echo "No files found for head SHA — falling back to comparing origin/$TARGET..origin/main"
git fetch origin "$TARGET" || true
if git ls-remote --exit-code --heads origin "$TARGET" >/dev/null 2>&1; then
# Use merge-base (three-dot) diff so we capture files introduced by the merge commit(s)
CHANGED=$(git diff --name-only origin/main...origin/$TARGET || true)
else
echo "origin/$TARGET not found — treating as changed"
CHANGED="backend/"
fi
fi
echo "Changed files:\n$CHANGED"
if printf "%s" "$CHANGED" | grep -E '^backend/' >/dev/null 2>&1; then
echo "backend_changed=true" >> $GITHUB_OUTPUT
else
echo "backend_changed=false" >> $GITHUB_OUTPUT
fi
- name: Build and push Docker image
if: ${{ github.event_name == 'workflow_dispatch' || steps.detect.outputs.backend_changed == 'true' }}
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
run: |
set -euo pipefail
echo "Backend changed — building image"
echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
docker build -t "$DOCKERHUB_USERNAME/transformer-tracker-local:latest" backend
docker push "$DOCKERHUB_USERNAME/transformer-tracker-local:latest"