Files
cloud-configuration/.github/workflows/deploy.yaml
T

109 lines
3.7 KiB
YAML

name: Deploy to Swarm
on:
workflow_dispatch:
inputs:
deploy:
description: 'Should we deploy?'
required: true
type: boolean
push:
paths:
- 'docker/**'
env:
REGISTRY: ghcr.io
IMAGE_NAME: 2martens/docker-nginx
jobs:
fact-finding:
name: Detect changed environments
runs-on: ubuntu-latest
outputs:
changed_envs: ${{ steps.detect.outputs.changed_envs }}
steps:
- uses: actions/checkout@v4
- name: Determine which environments changed
id: detect
run: |
echo "changed_envs=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '^docker/' | cut -d/ -f2 | sort -u | tr '\n' ' ')" >> "$GITHUB_OUTPUT"
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: fact-finding
strategy:
matrix:
env: [ test ]
environment: ${{ matrix.env }}
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Exit if this environment wasn't changed
id: check
run: |
echo "Changed envs: '${{ needs.fact-finding.outputs.changed_envs }}'"
if [[ ${{ github.event_name }} == 'workflow_dispatch' && "${{ github.event.inputs.deploy }}" == 'true' || "${{ needs.fact-finding.outputs.changed_envs }}" =~ "${{ matrix.env }}" ]]; then
echo "should_deploy=true" >> $GITHUB_OUTPUT
else
echo "should_deploy=false" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Perform docker login
if: steps.check.outputs.should_deploy == 'true'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
if: steps.check.outputs.should_deploy == 'true'
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ matrix.env }}
- name: Build & push Nginx image to GHCR
id: push
if: steps.check.outputs.should_deploy == 'true'
uses: docker/build-push-action@v6
with:
context: docker/${{ matrix.env }}/nginx
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Attest
uses: actions/attest-build-provenance@v2
id: attest
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
subject-digest: ${{ steps.push.outputs.digest }}
- name: Copy files to remote
if: steps.check.outputs.should_deploy == 'true'
uses: burnett01/rsync-deployments@7.0.2
with:
switches: -az -r --del
path: docker/${{ matrix.env }}/stack-config/.
remote_path: ~/stack-config
remote_host: ${{ secrets.DEPLOY_HOST }}
remote_port: ${{ secrets.DEPLOY_PORT }}
remote_user: ${{ secrets.DEPLOY_USER }}
remote_key: ${{ secrets.DEPLOY_KEY }}
- name: Deploy applications
if: steps.check.outputs.should_deploy == 'true'
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_KEY }}
script: |
docker pull ghcr.io/2martens/docker-nginx:${{ matrix.env }}
docker service update --image ghcr.io/2martens/docker-nginx:${{ matrix.env }} nginx-proxy
for f in ~/stack-config/*.yaml; do
NAME=$(basename "$f" .yaml)
docker stack deploy -c "$f" "$NAME"
done