Skip to content

Commit 28d5012

Browse files
committed
chore: harden create-review-branch.yml — SHA-pin actions, fix script injection, add permissions
1 parent 478ae90 commit 28d5012

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

.github/workflows/create-review-branch.yml

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ on:
33
workflow_dispatch:
44
inputs:
55
branch_name:
6-
description: "Name of the branch to create (e.g., review)"
6+
description: 'Name of the branch to create (e.g., review)'
77
required: true
8-
default: "review"
8+
default: 'review'
99
push:
1010
branches:
11-
- main # This line triggers the workflow whenever anything is pushed to the 'main' branch
11+
- main
12+
13+
permissions:
14+
contents: write
1215

1316
jobs:
1417
create_branch:
1518
runs-on: ubuntu-latest
1619
steps:
1720
- name: Checkout main branch
18-
# For 'push' events, GITHUB_REF will already be 'refs/heads/main',
19-
# but explicitly setting 'ref: main' ensures we're always pulling from main
20-
# regardless of the specific commit that triggered the push.
21-
uses: actions/checkout@v4
21+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
2222
with:
23-
ref: "main"
23+
ref: 'main'
2424

2525
- name: Configure Git
2626
run: |
@@ -29,37 +29,36 @@ jobs:
2929
3030
- name: Determine Branch Name
3131
id: set_branch_name
32+
env:
33+
EVENT_NAME: ${{ github.event_name }}
34+
INPUT_BRANCH: ${{ github.event.inputs.branch_name }}
3235
run: |
33-
# If triggered by workflow_dispatch, use the input.
34-
# Otherwise (triggered by push), default to 'review'.
35-
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
36-
echo "BRANCH_NAME=${{ github.event.inputs.branch_name }}" >> "$GITHUB_OUTPUT"
36+
if [ "$EVENT_NAME" == "workflow_dispatch" ]; then
37+
echo "BRANCH_NAME=$INPUT_BRANCH" >> "$GITHUB_OUTPUT"
3738
else
3839
echo "BRANCH_NAME=review" >> "$GITHUB_OUTPUT"
3940
fi
4041
4142
- name: Delete existing review branch (if it exists)
43+
env:
44+
BRANCH_NAME: ${{ steps.set_branch_name.outputs.BRANCH_NAME }}
4245
run: |
43-
git push origin --delete ${{ steps.set_branch_name.outputs.BRANCH_NAME }} || true
44-
shell: bash
46+
git push origin --delete "$BRANCH_NAME" || true
4547
4648
- name: Create new review branch from main
49+
env:
50+
BRANCH_NAME: ${{ steps.set_branch_name.outputs.BRANCH_NAME }}
4751
run: |
48-
git checkout -b ${{ steps.set_branch_name.outputs.BRANCH_NAME }}
49-
git push origin ${{ steps.set_branch_name.outputs.BRANCH_NAME }}
50-
shell: bash
52+
git checkout -b "$BRANCH_NAME"
53+
git push origin "$BRANCH_NAME"
5154
52-
- name: Trigger Netlify Deploy (Optional, Netlify should auto-detect)
55+
- name: Trigger Netlify Deploy (Optional)
56+
env:
57+
NETLIFY_PREVIEW_BUILD_HOOK: ${{ secrets.NETLIFY_PREVIEW_BUILD_HOOK }}
5358
run: |
54-
# Only attempt to curl if the environment variable is set.
55-
# Use -s (silent) and -S (show errors) for cleaner output if it fails.
56-
# The 'test -n' checks if the string is non-empty.
57-
if test -n "${{ env.NETLIFY_PREVIEW_BUILD_HOOK }}"; then
59+
if test -n "$NETLIFY_PREVIEW_BUILD_HOOK"; then
5860
echo "Triggering Netlify deploy for preview branch..."
59-
curl -X POST -d '{}' "${{ env.NETLIFY_PREVIEW_BUILD_HOOK }}"
61+
curl -X POST -d '{}' "$NETLIFY_PREVIEW_BUILD_HOOK"
6062
else
6163
echo "NETLIFY_PREVIEW_BUILD_HOOK secret is not set or empty. Skipping Netlify deploy trigger."
6264
fi
63-
env:
64-
NETLIFY_PREVIEW_BUILD_HOOK: ${{ secrets.NETLIFY_PREVIEW_BUILD_HOOK }}
65-
# Removed the 'if' condition on the step itself, handling it inside 'run'

0 commit comments

Comments
 (0)