Skip to content

Commit 279e74f

Browse files
oseoinRahul Somasundaram
authored andcommitted
Forced SHA update workflow (nginx#5560)
1 parent 4cf0950 commit 279e74f

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

.github/scripts/docker-updater.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
set -o pipefail
4+
5+
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
6+
DOCKER_FILE=${SCRIPT_ROOT}/build/Dockerfile
7+
exclude_strings=""
8+
9+
# Parse command line arguments
10+
while [[ $# -gt 0 ]]; do
11+
key="$1"
12+
case $key in
13+
--exclude)
14+
exclude_strings="$2"
15+
shift
16+
shift
17+
;;
18+
*)
19+
DOCKER_FILE="$1"
20+
shift
21+
;;
22+
esac
23+
done
24+
25+
# Check if the file exists
26+
if [ ! -f "$DOCKER_FILE" ]; then
27+
echo "File $DOCKER_FILE does not exist."
28+
exit 1
29+
fi
30+
31+
function contains_excluded() {
32+
local line="$1"
33+
local exclude="$2"
34+
local IFS=','
35+
local excluded=($exclude)
36+
for word in "${excluded[@]}"; do
37+
if [[ "$line" == *"$word"* ]]; then
38+
return 0
39+
fi
40+
done
41+
return 1
42+
}
43+
44+
function check_sha() {
45+
image_sha="$1"
46+
image=$(echo "$image_sha" | cut -d '@' -f1)
47+
tag_sha=$(echo "$image_sha" | cut -d '@' -f2)
48+
49+
docker pull -q "$image" > /dev/null
50+
latest_digest=$(docker inspect --format='{{index .RepoDigests 0}}' "$image")
51+
latest_sha=$(echo "$latest_digest" | cut -d '@' -f2)
52+
53+
if [ "$tag_sha" = "$latest_sha" ]; then
54+
echo "The provided SHA256 hash is the latest for $image"
55+
else
56+
echo "> A newer version of $image is available:"
57+
echo "> - $image@$tag_sha"
58+
echo "> + $image@$latest_sha"
59+
echo "> updating $DOCKER_FILE"
60+
sed -i -e "s/$tag_sha/$latest_sha/g" "$DOCKER_FILE"
61+
fi
62+
}
63+
if [ -n "$exclude_strings" ]; then
64+
echo "excluding images containing one of: '$exclude_strings'"
65+
fi
66+
while IFS= read -r line; do
67+
if [[ $line =~ ^FROM\ (.+@.+) ]]; then
68+
image=$(echo "${BASH_REMATCH[1]}" | awk '{print $1}')
69+
if [ -n "$exclude_strings" ] && contains_excluded "$line" "$exclude_strings"; then
70+
echo "Skipping $image"
71+
continue
72+
fi
73+
check_sha "$image"
74+
fi
75+
done < "$DOCKER_FILE"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: "Update pinned container SHAs"
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
source_branch:
7+
required: true
8+
type: string
9+
default: 'main'
10+
excludes:
11+
description: Comma separated list of strings to exclude images from the update
12+
required: false
13+
type: string
14+
default: ''
15+
dry_run:
16+
type: boolean
17+
default: false
18+
19+
defaults:
20+
run:
21+
shell: bash
22+
23+
permissions:
24+
contents: read
25+
26+
jobs:
27+
update-docker-sha:
28+
permissions:
29+
contents: write
30+
runs-on: ubuntu-22.04
31+
steps:
32+
- name: Checkout Repository
33+
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
34+
with:
35+
ref: ${{ inputs.source_branch }}
36+
37+
- name: Update images
38+
id: update_images
39+
run: |
40+
docker_md5=$(find . -type f \( -wholename "build/Dockerfile" -o -wholename "tests/Dockerfile" \) -exec md5sum {} + | LC_ALL=C sort | md5sum | awk '{ print $1 }')
41+
echo "docker_md5=${docker_md5:0:8}" >> $GITHUB_OUTPUT
42+
ARGS=""
43+
if [ -n ${{ github.event.inputs.excludes }} ]; then
44+
ARGS="--exclude ${{ github.event.inputs.excludes }}"
45+
fi
46+
.github/scripts/docker-updater.sh ./build/Dockerfile $ARGS
47+
.github/scripts/docker-updater.sh ./tests/Dockerfile $ARGS
48+
files=$(git diff --name-only)
49+
if [[ $files == *"Dockerfile"* ]]; then
50+
echo "change_detected=true" >> $GITHUB_OUTPUT
51+
else
52+
echo "change_detected=false" >> $GITHUB_OUTPUT
53+
fi
54+
echo $GITHUB_OUTPUT
55+
56+
- name: Create Pull Request
57+
uses: peter-evans/create-pull-request@6d6857d36972b65feb161a90e484f2984215f83e # v6.0.5
58+
with:
59+
token: ${{ secrets.NGINX_PAT }}
60+
commit-message: Update docker images ${{ steps.update_images.outputs.docker_md5 }}
61+
title: Docker image update ${{ steps.update_images.outputs.docker_md5 }}
62+
branch: chore/image-update-${{ inputs.source_branch }}-${{ steps.update_images.outputs.docker_md5 }}
63+
author: nginx-bot <integrations@nginx.com>
64+
labels: |
65+
chore
66+
dependency
67+
body: |
68+
This automated PR updates pinned container image SHAs to latest.
69+
if: ${{ !inputs.dry_run && steps.update_images.outputs.change_detected == 'true' }}

0 commit comments

Comments
 (0)