Changes in workflow
This commit is contained in:
@@ -2,17 +2,20 @@ name: Build image when workload image tag changes
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
# Uncomment/adjust once it's working:
|
||||||
|
# branches: [ main ]
|
||||||
paths:
|
paths:
|
||||||
- "argocd/deployment.yaml"
|
- "argocd/deployment.yaml"
|
||||||
- "Dockerfile" # keep if you also want to rebuild when Dockerfile changes
|
- "Dockerfile"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-if-image-changed:
|
build-if-image-changed:
|
||||||
runs-on: ubuntu-latest
|
runs-on: self-hosted # or whatever label your runner uses
|
||||||
|
|
||||||
env:
|
env:
|
||||||
DEPLOY_FILE: "argocd/deployment.yaml"
|
DEPLOY_FILE: "argocd/deployment.yaml"
|
||||||
TARGET_REPO: "git.baumann.gr/adebaumann/vui" # repo (no tag)
|
CONTAINER_NAME: "web" # we only consider this container
|
||||||
|
EXPECTED_REPO: "git.baumann.gr/adebaumann/vui" # sanity-check the repo
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
@@ -31,7 +34,6 @@ jobs:
|
|||||||
echo "base=$(git hash-object -t tree /dev/null)" >> "$GITHUB_OUTPUT"
|
echo "base=$(git hash-object -t tree /dev/null)" >> "$GITHUB_OUTPUT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Install yq for robust YAML parsing
|
|
||||||
- name: Install yq
|
- name: Install yq
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
@@ -47,28 +49,41 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
file="$DEPLOY_FILE"
|
file="$DEPLOY_FILE"
|
||||||
repo="$TARGET_REPO"
|
cname="$CONTAINER_NAME"
|
||||||
|
expected_repo="$EXPECTED_REPO"
|
||||||
|
|
||||||
# Function: from a deployment yaml, read .spec.template.spec.containers[].image
|
# Extract ONLY from .spec.template.spec.containers[], selecting by .name == cname
|
||||||
# and select the one whose image starts with "$repo:"
|
# Works across multi-doc YAML (the Service doc will be ignored)
|
||||||
extract() {
|
extract() {
|
||||||
yq -r '
|
yq -r '
|
||||||
.spec.template.spec.containers // [] # only real containers, not initContainers
|
select(.kind == "Deployment") |
|
||||||
| map(.image) | .[]? # images as strings
|
.spec.template.spec.containers // [] |
|
||||||
| select(startswith(env(repo) + ":")) # match exact repo + ":"
|
map(select(.name == env(cname))) |
|
||||||
|
.[]?.image
|
||||||
' "$1" 2>/dev/null | tail -n 1
|
' "$1" 2>/dev/null | tail -n 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Old image from previous commit (if file existed)
|
debug_list() {
|
||||||
|
echo "== workload images in $1 ==" >&2
|
||||||
|
yq -r '
|
||||||
|
select(.kind == "Deployment") |
|
||||||
|
.spec.template.spec.containers // [] |
|
||||||
|
.[] | "\(.name): \(.image)"
|
||||||
|
' "$1" 2>/dev/null | nl -ba >&2 || true
|
||||||
|
}
|
||||||
|
|
||||||
|
# Old image (from previous commit)
|
||||||
if git cat-file -e "${{ steps.base.outputs.base }}":"$file" 2>/dev/null; then
|
if git cat-file -e "${{ steps.base.outputs.base }}":"$file" 2>/dev/null; then
|
||||||
git show "${{ steps.base.outputs.base }}:$file" > /tmp/old.yaml
|
git show "${{ steps.base.outputs.base }}:$file" > /tmp/old.yaml
|
||||||
|
debug_list /tmp/old.yaml
|
||||||
old_image="$(extract /tmp/old.yaml || true)"
|
old_image="$(extract /tmp/old.yaml || true)"
|
||||||
else
|
else
|
||||||
old_image=""
|
old_image=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# New image from workspace
|
# New image (from workspace)
|
||||||
if [ -f "$file" ]; then
|
if [ -f "$file" ]; then
|
||||||
|
debug_list "$file"
|
||||||
new_image="$(extract "$file" || true)"
|
new_image="$(extract "$file" || true)"
|
||||||
else
|
else
|
||||||
new_image=""
|
new_image=""
|
||||||
@@ -77,27 +92,26 @@ jobs:
|
|||||||
echo "Old workload image: $old_image"
|
echo "Old workload image: $old_image"
|
||||||
echo "New workload image: $new_image"
|
echo "New workload image: $new_image"
|
||||||
|
|
||||||
# Helpers to split repo and tag (handles registry with port)
|
|
||||||
parse_tag() {
|
|
||||||
local ref="$1"
|
|
||||||
local after_slash="${ref##*/}"
|
|
||||||
if [[ "$after_slash" == *:* ]]; then echo "${after_slash##*:}"; else echo ""; fi
|
|
||||||
}
|
|
||||||
parse_repo() {
|
|
||||||
local ref="$1"
|
|
||||||
local tag="$(parse_tag "$ref")"
|
|
||||||
if [ -n "$tag" ]; then echo "${ref%:$tag}"; else echo "$ref"; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
old_tag="$(parse_tag "$old_image")"
|
|
||||||
new_tag="$(parse_tag "$new_image")"
|
|
||||||
new_repo="$(parse_repo "$new_image")"
|
|
||||||
|
|
||||||
if [ -z "$new_image" ]; then
|
if [ -z "$new_image" ]; then
|
||||||
echo "ERROR: Could not find a containers[].image starting with ${repo}: in $file"
|
echo "ERROR: Could not find containers[].name == \"$cname\" in $file"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Sanity-check repo prefix (avoid accidentally building the init image)
|
||||||
|
new_repo="${new_image%:*}"
|
||||||
|
new_tag="${new_image##*:}"
|
||||||
|
if [[ "$new_repo" != "$expected_repo" ]]; then
|
||||||
|
echo "ERROR: Found container \"$cname\" image repo is \"$new_repo\" but expected \"$expected_repo\""
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Old tag (if any)
|
||||||
|
if [ -n "$old_image" ]; then
|
||||||
|
old_tag="${old_image##*:}"
|
||||||
|
else
|
||||||
|
old_tag=""
|
||||||
|
fi
|
||||||
|
|
||||||
registry="$(echo "$new_repo" | awk -F/ '{print $1}')"
|
registry="$(echo "$new_repo" | awk -F/ '{print $1}')"
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -110,7 +124,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Skip if tag unchanged
|
- name: Skip if tag unchanged
|
||||||
if: steps.img.outputs.changed != 'true'
|
if: steps.img.outputs.changed != 'true'
|
||||||
run: echo "Workload image tag unchanged in ${{ env.DEPLOY_FILE }}; skipping build."
|
run: echo "Workload image tag unchanged; skipping build."
|
||||||
|
|
||||||
- name: Set up Buildx
|
- name: Set up Buildx
|
||||||
if: steps.img.outputs.changed == 'true'
|
if: steps.img.outputs.changed == 'true'
|
||||||
|
|||||||
Reference in New Issue
Block a user