Skip to content

Commit 407381d

Browse files
committed
ci(cd): enable PR deployments using pull_request_target with safe guards; checkout PR head for builds
1 parent 845ba79 commit 407381d

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

.github/workflows/cd.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CD
33
on:
44
push:
55
branches: [ main ]
6-
pull_request:
6+
pull_request_target:
77
branches: [ main ]
88
workflow_dispatch:
99

@@ -20,8 +20,19 @@ jobs:
2020
outputs:
2121
backend_image: ${{ steps.tags.outputs.backend }}
2222
frontend_image: ${{ steps.tags.outputs.frontend }}
23+
# Only run on PRs originating from this repo to avoid exposing secrets to forks
24+
if: ${{ github.event_name != 'pull_request_target' || github.event.pull_request.head.repo.full_name == github.repository }}
2325
steps:
26+
- name: Select ref
27+
run: |
28+
if [ "${{ github.event_name }}" = "pull_request_target" ]; then
29+
echo "REF=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
30+
else
31+
echo "REF=${GITHUB_SHA}" >> $GITHUB_ENV
32+
fi
2433
- uses: actions/checkout@v4
34+
with:
35+
ref: ${{ env.REF }}
2536

2637
- name: Derive lowercase image repo
2738
run: |
@@ -73,10 +84,22 @@ jobs:
7384
deploy-compose:
7485
runs-on: ubuntu-latest
7586
needs: build-and-push
76-
if: ${{ secrets.SSH_HOST != '' && secrets.SSH_USER != '' && secrets.SSH_PRIVATE_KEY != '' }}
87+
if: ${{ secrets.SSH_HOST != '' && secrets.SSH_USER != '' && secrets.SSH_PRIVATE_KEY != '' && (github.event_name != 'pull_request_target' || github.event.pull_request.head.repo.full_name == github.repository) }}
7788
environment: production
89+
concurrency:
90+
group: prod-deploy
91+
cancel-in-progress: false
7892
steps:
93+
- name: Select ref
94+
run: |
95+
if [ "${{ github.event_name }}" = "pull_request_target" ]; then
96+
echo "REF=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
97+
else
98+
echo "REF=${GITHUB_SHA}" >> $GITHUB_ENV
99+
fi
79100
- uses: actions/checkout@v4
101+
with:
102+
ref: ${{ env.REF }}
80103

81104
- name: Upload docker-compose file
82105
uses: appleboy/scp-action@v0.1.7

0 commit comments

Comments
 (0)