Skip to content

Commit 88329bc

Browse files
authored
Fix race condition in scheduled workflow by locking to commit hash (#293)
The scheduled workflow had a race condition where: 1. Check job fetches commit hash for a branch 2. Verifies no image exists for that hash 3. Triggers build with branch name (not hash) 4. Deploy checks out branch, which may have moved 5. Image tagged with old hash contains new code Solution: - Pass commit hash from check job through to deploy action - Use commit hash for checkout when provided (scheduled builds) - Fallback to branch name when not provided (manual builds) - Fully backward compatible with existing workflows Changes: - scheduled.yml: Include source_commit in config output - deploy.yml: Add optional source_commit input parameter - deploy/action.yml: Use source_commit || source_ref for checkout
1 parent 7109c89 commit 88329bc

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

.github/actions/deploy/action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ inputs:
1010
description: The branch, tag or SHA to checkout and build from
1111
type: string
1212
required: true
13+
source_commit:
14+
description: The specific commit SHA to checkout (takes precedence over source_ref)
15+
type: string
16+
required: false
1317
build_script:
1418
description: The bash script path in this repository to run instead of the Docker build & push script. You must push the image yourself.
1519
type: string
@@ -77,7 +81,7 @@ runs:
7781
with:
7882
repository: ${{ inputs.source_repository }}
7983
path: source
80-
ref: ${{ inputs.source_ref }}
84+
ref: ${{ inputs.source_commit || inputs.source_ref }}
8185
fetch-depth: 0
8286
- name: get short git commit hash
8387
id: git_commit_hash

.github/workflows/deploy.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
description: The branch, tag or SHA to checkout and build from
1010
type: string
1111
required: true
12+
source_commit:
13+
description: The specific commit SHA to checkout (takes precedence over source_ref)
14+
type: string
15+
required: false
1216
build_script:
1317
description: The bash script path in this repository to run instead of the Docker build & push script. You must push the image yourself.
1418
type: string
@@ -82,6 +86,7 @@ jobs:
8286
with:
8387
source_repository: ${{ inputs.source_repository }}
8488
source_ref: ${{ inputs.source_ref }}
89+
source_commit: ${{ inputs.source_commit }}
8590
build_script: ${{ inputs.build_script }}
8691
build_args: ${{ inputs.build_args }}
8792
target_tag: ${{ inputs.target_tag }}-${{ matrix.config.slug }}

.github/workflows/scheduled.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ jobs:
6464
local RESPONSE=$(curl -s -H "Accept: application/vnd.github+json" \
6565
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
6666
"https://api.github.com/repos/${SOURCE_REPOSITORY}/commits/${SOURCE_REF}?per_page=1")
67-
local COMMIT_HASH=$(echo "$RESPONSE" | jq -r '.sha' | cut -c1-7)
67+
local COMMIT_HASH_FULL=$(echo "$RESPONSE" | jq -r '.sha')
68+
local COMMIT_HASH=$(echo "$COMMIT_HASH_FULL" | cut -c1-7)
6869
6970
if [[ -z "$COMMIT_HASH" || "$COMMIT_HASH" == "null" ]]; then
7071
# Log error but don't exit; just skip this configuration
@@ -75,7 +76,7 @@ jobs:
7576
local configOutput="${TEMP_DIR}/${LINE}_commits.json"
7677
touch $configOutput
7778
78-
echo "{\"line\": \"$LINE\", \"commit_hash\": \"$COMMIT_HASH\"}," >> $configOutput
79+
echo "{\"line\": \"$LINE\", \"commit_hash\": \"$COMMIT_HASH\", \"commit_hash_full\": \"$COMMIT_HASH_FULL\"}," >> $configOutput
7980
}
8081
8182
process_image() {
@@ -147,6 +148,7 @@ jobs:
147148
while IFS=$'\t' read -r LINE SOURCE_REPOSITORY SOURCE_REF TARGET_REPOSITORY TARGET_TAG; do
148149
# get the image commit hash from LINE
149150
COMMIT_HASH=$(echo "$COMMITS" | jq -r --arg LINE "$LINE" '.[] | select(.line == $LINE) | .commit_hash')
151+
COMMIT_HASH_FULL=$(echo "$COMMITS" | jq -r --arg LINE "$LINE" '.[] | select(.line == $LINE) | .commit_hash_full')
150152
IMAGE_TAG="${TARGET_TAG}-${COMMIT_HASH}"
151153
IMAGE="${TARGET_REPOSITORY}:${IMAGE_TAG}"
152154
CLIENT="${TARGET_REPOSITORY#*/}"
@@ -165,7 +167,7 @@ jobs:
165167
# convert to string
166168
platformsOutput="{\"platforms\": \"[$platformsArr]\"}"
167169
168-
CONFIGS+=$(echo "$(yq -r -o=json ".[${LINE}]" "$CONFIG_FILE" | jq --argjson plat "$platformsOutput" '. + $plat'),")
170+
CONFIGS+=$(echo "$(yq -r -o=json ".[${LINE}]" "$CONFIG_FILE" | jq --argjson plat "$platformsOutput" --arg commit "$COMMIT_HASH_FULL" '. + $plat + {source_commit: $commit}'),")
169171
fi
170172
done < <(yq -r 'to_entries | map_values({"value":.value, "index":.key}) | .[] | [.index, .value.source.repository, .value.source.ref, .value.target.repository, .value.target.tag] | @tsv' "$CONFIG_FILE")
171173
@@ -187,6 +189,7 @@ jobs:
187189
with:
188190
source_repository: ${{ matrix.config.source.repository }}
189191
source_ref: ${{ matrix.config.source.ref }}
192+
source_commit: ${{ matrix.config.source_commit }}
190193
build_script: ${{ matrix.config.build_script }}
191194
build_args: "${{ matrix.config.build_args }}"
192195
target_tag: ${{ matrix.config.target.tag }}

0 commit comments

Comments
 (0)