Fix silly mistake with CD and secrets #35
Workflow file for this run
This file contains hidden or 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: Build and Deploy | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- 'main' | |
- 'staging' | |
tags: | |
- 'v*' | |
pull_request: | |
branches: | |
- 'main' | |
- 'staging' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Log in to GitHub Container Registry | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin | |
- name: Extract metadata for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ghcr.io/${{ github.repository }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=raw,value=latest,enable={{is_default_branch}} | |
- name: Build and push | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./docker/Dockerfile | |
target: 'prod' | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: read | |
needs: build | |
strategy: | |
matrix: | |
include: | |
# - environment: staging | |
# branch: staging | |
# target_path: "~/staging.mapdb.cncnet.org" | |
# compose_file: "docker-compose.prod.yml" | |
# nginx_conf: "docker/nginx.prod.conf" | |
# host-s-name: "STAGING_SSH_HOST" | |
- environment: production | |
branch: main | |
target_path: "~/mapdb.cncnet.org" | |
compose_file: "docker-compose.prod.yml" | |
nginx_conf: "docker/nginx.prod.conf" | |
host-s-name: "PROD_SSH_HOST" | |
steps: | |
- name: "Exit if not matching branch" | |
if: github.ref != format('refs/heads/{0}', matrix.branch) | |
run: echo "Not target branch for this deployment. Skipping..." && exit 0 | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Copy docker-compose and nginx config over ssh | |
uses: appleboy/scp-action@v0.1.7 | |
with: | |
host: ${{ secrets[matrix.host-s-name] }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
source: "${{ matrix.compose_file }},${{ matrix.nginx_conf }}" | |
target: "${{ matrix.target_path }}" | |
- name: SSH into server and deploy | |
uses: appleboy/ssh-action@v1.2.1 | |
with: | |
host: ${{ secrets[matrix.host-s-name] }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
cd ${{ matrix.target_path }} | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
docker compose -f ${{ matrix.compose_file }} pull | |
docker compose -f ${{ matrix.compose_file }} down | |
docker compose -f ${{ matrix.compose_file }} up -d |