deploy.yml
1,513 bytes
| 1 | name: deploy |
|---|---|
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: [main] |
| 6 | workflow_dispatch: {} # allow manual runs from the Actions tab |
| 7 | |
| 8 | concurrency: |
| 9 | group: deploy |
| 10 | cancel-in-progress: false |
| 11 | |
| 12 | env: |
| 13 | IMAGE: ghcr.io/rasmusjy/cswebtravel |
| 14 | |
| 15 | jobs: |
| 16 | build-and-push: |
| 17 | runs-on: ubuntu-latest |
| 18 | permissions: |
| 19 | contents: read |
| 20 | packages: write # push to GHCR via GITHUB_TOKEN |
| 21 | steps: |
| 22 | - uses: actions/checkout@v4 |
| 23 | |
| 24 | - uses: docker/setup-buildx-action@v3 |
| 25 | |
| 26 | - uses: docker/login-action@v3 |
| 27 | with: |
| 28 | registry: ghcr.io |
| 29 | username: ${{ github.actor }} |
| 30 | password: ${{ secrets.GITHUB_TOKEN }} |
| 31 | |
| 32 | - uses: docker/build-push-action@v6 |
| 33 | with: |
| 34 | context: . |
| 35 | push: true |
| 36 | tags: | |
| 37 | ${{ env.IMAGE }}:latest |
| 38 | ${{ env.IMAGE }}:${{ github.sha }} |
| 39 | cache-from: type=gha |
| 40 | cache-to: type=gha,mode=max |
| 41 | |
| 42 | deploy: |
| 43 | needs: build-and-push |
| 44 | runs-on: ubuntu-latest |
| 45 | steps: |
| 46 | - name: Pull & restart on the VPS |
| 47 | run: | |
| 48 | mkdir -p ~/.ssh |
| 49 | echo "${{ secrets.SSH_KEY }}" | base64 -d > ~/.ssh/id_deploy |
| 50 | chmod 600 ~/.ssh/id_deploy |
| 51 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ |
| 52 | -i ~/.ssh/id_deploy "${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}" \ |
| 53 | 'cd /opt/projects/csweb-travel && git pull --ff-only && docker compose -f docker-compose.prod.yml pull app && docker compose -f docker-compose.prod.yml up -d && docker image prune -f' |
| 54 | |