You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kompass/.github/workflows/deploy-pr.yml

93 lines
2.9 KiB
YAML

name: Deploy PR to Remote Server
on:
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to deploy'
required: true
type: number
concurrency:
group: deploy-pr-${{ inputs.pr_number }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
APP_IMAGE_NAME: ${{ github.repository }}
NGINX_IMAGE_NAME: ${{ github.repository }}-nginx
jobs:
deploy:
runs-on: ubuntu-latest
environment: deploy-web
permissions:
pull-requests: write
steps:
- name: Get PR details
id: pr
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ inputs.pr_number }};
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
core.setOutput('head_ref', pr.head.ref);
core.setOutput('pr_number', prNumber);
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: Deploy to remote server
id: deploy
run: |
OUTPUT=$(ssh -i ~/.ssh/deploy_key -p ${{ secrets.SSH_PORT || 22 }} ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }} ${{ inputs.pr_number }})
echo "$OUTPUT"
DEPLOY_URL=$(echo "$OUTPUT" | tail -n 1)
echo "url=$DEPLOY_URL" >> $GITHUB_OUTPUT
- name: Cleanup SSH
if: always()
run: |
rm -f ~/.ssh/deploy_key
- name: Comment deployment and documentation links
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ steps.pr.outputs.pr_number }}
header: deployment
message: |
🚀 **PR deployed successfully!**
**Website:** ${{ steps.deploy.outputs.url }}
**Documentation:** https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${{ steps.pr.outputs.head_ref }}/
**Docker Images:**
- App: `${{ env.REGISTRY }}/${{ env.APP_IMAGE_NAME }}:pr-${{ steps.pr.outputs.pr_number }}`
- Nginx: `${{ env.REGISTRY }}/${{ env.NGINX_IMAGE_NAME }}:pr-${{ steps.pr.outputs.pr_number }}`
- name: Remove awaiting-deployment label
if: always()
uses: actions/github-script@v7
with:
script: |
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ steps.pr.outputs.pr_number }},
name: 'awaiting-deployment'
});
} catch (error) {
console.log('Label may have already been removed');
}