Skip to content

ci: update GitHub Actions workflow #2

ci: update GitHub Actions workflow

ci: update GitHub Actions workflow #2

Workflow file for this run

name: CD
on:
push:
branches: [main]
tags: ["v*"]
concurrency:
group: cd-production
cancel-in-progress: false
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
IMAGE_APP: ${{ github.repository }}-app
IMAGE_NGINX: ${{ github.repository }}-nginx
jobs:
build-and-push:
name: Build & Push Images
runs-on: ubuntu-latest
outputs:
app_image: ${{ steps.meta.outputs.app_image }}
nginx_image: ${{ steps.meta.outputs.nginx_image }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Compute image tags (lowercased)
id: meta
run: |
set -euo pipefail
repo="${{ github.repository }}"
repo_lc="$(echo "$repo" | tr '[:upper:]' '[:lower:]')"
sha="${GITHUB_SHA::12}"
echo "app_image=${REGISTRY}/${repo_lc}-app:${sha}" >> "$GITHUB_OUTPUT"
echo "nginx_image=${REGISTRY}/${repo_lc}-nginx:${sha}" >> "$GITHUB_OUTPUT"
echo "app_latest=${REGISTRY}/${repo_lc}-app:latest" >> "$GITHUB_OUTPUT"
echo "nginx_latest=${REGISTRY}/${repo_lc}-nginx:latest">> "$GITHUB_OUTPUT"
- name: Build & push application image
uses: docker/build-push-action@v6
with:
context: .
target: production
push: true
tags: |
${{ steps.meta.outputs.app_image }}
${{ steps.meta.outputs.app_latest }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build & push nginx image
uses: docker/build-push-action@v6
with:
context: .
file: docker/nginx/Dockerfile
push: true
build-args: |
APP_IMAGE=${{ steps.meta.outputs.app_image }}
tags: |
${{ steps.meta.outputs.nginx_image }}
${{ steps.meta.outputs.nginx_latest }}
deploy:
name: Deploy to Production
runs-on: ubuntu-latest
needs: build-and-push
environment: production
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure SSH
run: |
set -euo pipefail
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_deploy
chmod 600 ~/.ssh/id_deploy
ssh-keyscan -p "${{ secrets.SSH_PORT || 22 }}" -H "${{ secrets.SSH_HOST }}" >> ~/.ssh/known_hosts 2>/dev/null
- name: Deploy over SSH
env:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_USER: ${{ secrets.SSH_USER }}
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
APP_IMAGE: ${{ needs.build-and-push.outputs.app_image }}
NGINX_IMAGE: ${{ needs.build-and-push.outputs.nginx_image }}
REGISTRY_USERNAME: ${{ github.actor }}
REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
ssh -i ~/.ssh/id_deploy -p "${SSH_PORT:-22}" \
"${SSH_USER}@${SSH_HOST}" \
"DEPLOY_PATH='${DEPLOY_PATH}' \
APP_IMAGE='${APP_IMAGE}' NGINX_IMAGE='${NGINX_IMAGE}' \
REGISTRY='${{ env.REGISTRY }}' \
REGISTRY_USERNAME='${REGISTRY_USERNAME}' REGISTRY_TOKEN='${REGISTRY_TOKEN}' \
bash -s" < deploy.sh