Skip to content

Commit 01709db

Browse files
authored
Add PR validation to validate image github action (#598)
1 parent 614cd7b commit 01709db

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

.github/workflows/validate-image.yml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,68 @@ on:
66
inputs:
77
image-version:
88
required: true
9-
description: Image version to validate=
9+
description: Image version to validate
10+
pr-number:
11+
required: true
12+
description: PR number to validate
1013
# Call from other workflow
1114
workflow_call:
1215
inputs:
1316
image-version:
1417
type: string
1518
required: true
19+
pr-number:
20+
type: string
21+
required: true
1622
defaults:
1723
run:
1824
shell: bash -l {0}
1925
jobs:
20-
get-pr-number:
26+
validate-pr:
2127
runs-on: ubuntu-latest
22-
name: Get PR Number by title
28+
name: Validate PR
2329
outputs:
24-
pr_id: ${{ steps.get_pr_id.outputs.pr_id }}
30+
pr_id: ${{ steps.validate_pr.outputs.pr_id }}
2531
steps:
2632
- name: Checkout repository
2733
uses: actions/checkout@v4
28-
- name: Get PR number
29-
id: get_pr_id
34+
- name: Validate PR
35+
id: validate_pr
3036
env:
3137
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3238
run: |
33-
PR=$(gh pr list --search "in:title 'release: v${{ inputs.image-version }}'" --json number --jq '.[0].number')
34-
echo "pr_id=$PR" >> $GITHUB_OUTPUT
39+
PR_NUMBER=${{ inputs.pr-number }}
40+
PR_INFO=$(gh pr view $PR_NUMBER --json number,title,isCrossRepository)
41+
42+
# Check if PR exists
43+
if [ -z "$PR_INFO" ]; then
44+
echo "Error: PR #$PR_NUMBER does not exist"
45+
exit 1
46+
fi
47+
48+
# Check PR title
49+
PR_TITLE=$(echo $PR_INFO | jq -r '.title')
50+
EXPECTED_TITLE_PREFIX="release: v${{ inputs.image-version }}"
51+
if [[ "$PR_TITLE" != "$EXPECTED_TITLE_PREFIX"* ]]; then
52+
echo "Error: PR title does not start with the expected prefix"
53+
echo "Expected prefix: $EXPECTED_TITLE_PREFIX"
54+
echo "Actual title: $PR_TITLE"
55+
exit 1
56+
fi
57+
58+
# Check if PR is from a fork
59+
IS_CROSS_REPO=$(echo $PR_INFO | jq -r '.isCrossRepository')
60+
if [ "$IS_CROSS_REPO" = "true" ]; then
61+
echo "Error: PR is from a forked repository"
62+
exit 1
63+
fi
64+
65+
echo "PR validation successful"
66+
echo "pr_id=$PR_NUMBER" >> $GITHUB_OUTPUT
67+
3568
call-codebuild-project:
3669
runs-on: ubuntu-latest
37-
needs: get-pr-number
70+
needs: validate-pr
3871
permissions:
3972
pull-requests: write
4073
contents: write
@@ -51,7 +84,7 @@ jobs:
5184
- name: Run CodeBuild
5285
uses: dark-mechanicum/aws-codebuild@v1
5386
env:
54-
CODEBUILD__sourceVersion: 'pr/${{ needs.get-pr-number.outputs.pr_id }}'
87+
CODEBUILD__sourceVersion: 'pr/${{ needs.validate-pr.outputs.pr_id }}'
5588
with:
5689
projectName: ${{ secrets.CODEBUILD_VALIDATION_JOB_NAME }}
5790
buildspec: '{"imageOverride": "aws/codebuild/standard:7.0", "imagePullCredentialsTypeOverride": "CODEBUILD"}'

0 commit comments

Comments
 (0)