Skip to content
name: Build, Publish and Deploy Docker image
# Avoids having multiple instances running
concurrency:
group: build-image
cancel-in-progress: true
on:
push:
tags:
- "v*"
env:
registry: ghcr.io
image_name: ${{ github.repository }}
jobs:
push_to_registry:
name: Push image to Github Packages
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Find Dockerfile
id: dockerfile
run: |
if [ -f "Dockerfile" ]; then
echo "grace_exit=false" >> "$GITHUB_OUTPUT"
else
echo "grace_exit=true" >> "$GITHUB_OUTPUT"
exit 1
fi
- name: Set up environment
run: |
echo DATABASE_URL=${{ secrets.DATABASE_URL }} > .env
- name: Log in to Github Packages
uses: docker/login-action@v2.1.0
with:
registry: ${{ env.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: extract_metadata
uses: docker/metadata-action@v4.1.0
with:
images: ${{ env.registry }}/${{ env.image_name }}
- name: Setup Buildx for caching
uses: docker/setup-buildx-action@v2
- name: Build and push Docker image
uses: docker/build-push-action@v3.2.0
with:
context: .
push: true
tags: ${{ steps.extract_metadata.outputs.tags }}
labels: ${{ steps.extract_metadata.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy containers
runs-on: ubuntu-latest
needs: push_to_registry
steps:
- name: Connect and pull
run: echo "${{ secrets.SSH_KEY }}" > ~/ssh_key && chmod 400 ~/ssh_key && ${{ secrets.SSH_DEPLOY_COMMAND }}