This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
Add preview deployments #3
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Deployment | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
branches: | |
- main | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build-push-docker: | |
runs-on: ubuntu-latest | |
if: github.actor != 'renovate[bot]' # Don't run this workflow for renovate bot | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set environment variables | |
run: | | |
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV | |
echo "SERVER_IMAGE=ghcr.io/wwi21seb-projekt/server-alpha:pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV | |
echo "TRAFFIC_RULE=Host(`pr-${{ github.event.pull_request.number }}.preview.server-alpha.tech`)" >> $GITHUB_ENV | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Get Docker metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
file: ./build/package/Dockerfile | |
- name: Deploy PR to Testing Subdomain | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.DEPLOY_HOST }} | |
username: ${{ secrets.DEPLOY_USER }} | |
key: ${{ secrets.DEPLOY_SSH_KEY }} | |
port: ${{ secrets.DEPLOY_PORT }} | |
script: | | |
docker pull ${{ steps.meta.outputs.tags }} | |
docker-compose -f deployments/docker-compose.prod.yml -p pr_${{ github.event.pull_request.number }} down || true | |
docker-compose -f deployments/docker-compose.prod.yml -p pr_${{ github.event.pull_request.number }} up -d | |
- name: Comment PR | |
uses: actions/github-script@v5 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const issue_number = context.issue.number; | |
const owner = context.repo.owner; | |
const repo = context.repo.repo; | |
const url = `http://pr-${issue_number}.preview.server-alpha.tech`; | |
const message = ` | |
:tada: Your changes are live! :rocket: | |
You can view them [here](${url}). | |
Happy reviewing! :eyeglasses: | |
`; | |
github.rest.issues.createComment({ | |
owner: owner, | |
repo: repo, | |
issue_number: issue_number, | |
body: message | |
}); |