Skip to content

Merge pull request #106 from DrexelTriangle/cicd #3

Merge pull request #106 from DrexelTriangle/cicd

Merge pull request #106 from DrexelTriangle/cicd #3

name: Build and Publish CMS Image
# Builds the CMS backend Docker image and pushes it to GitHub Container Registry
# (GHCR). Pushes are tagged with the full commit SHA (immutable, used for
# production deploys) and, on the default branch, a moving `latest` tag.
# Pull requests build the image to catch breakage but never push.
on:
push:
branches:
- main
tags:
- "v*"
pull_request:
branches:
- main
workflow_dispatch:
# Only the latest run per ref needs to finish; cancel superseded ones.
concurrency:
group: docker-publish-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# GHCR image names must be lowercase; the org (DrexelTriangle) is not.
- name: Compute lowercase image name
id: image
run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Skip login on pull_request builds: fork PRs have no push rights and
# must not receive registry credentials.
- name: Log in to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Derive image tags and labels
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ steps.image.outputs.name }}
tags: |
type=sha,format=long,prefix=
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=tag
labels: |
org.opencontainers.image.title=triangle-cms
org.opencontainers.image.source=https://github.com/${{ github.repository }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: ./server
file: ./server/Dockerfile
# Push on branch/tag builds and manual runs; PRs build only.
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max