CD #108
This file contains 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: CD | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: 'Deployment environment (dev|stage|prod)' | |
required: true | |
default: 'dev' | |
push: | |
branches: | |
- develop | |
- release | |
permissions: | |
contents: read | |
env: | |
NODE_VERSION: 18.x | |
DOCKER_REGISTRY: ghcr.io | |
DOCKER_IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
cd: | |
name: Build, test & package | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
cache: 'npm' | |
- name: Install dependencies | |
uses: borales/actions-yarn@v5 | |
with: | |
cmd: install --frozen-lockfile | |
- name: Build | |
uses: borales/actions-yarn@v5 | |
env: | |
CI: true | |
with: | |
cmd: build | |
- name: Package build output artifacts | |
run: | | |
tar czf build-artifacts.tgz .next/standalone .next/static | |
- name: Archive production artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: build-artifacts | |
path: build-artifacts.tgz | |
retention-days: 1 | |
docker-build: | |
name: Build and push Docker image | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
needs: cd | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Show GitHub context | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github) }} | |
run: echo "$GITHUB_CONTEXT" | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }} | |
tags: | | |
type=sha,prefix={{branch}}-,priority=750,enable=${{ startsWith(github.ref, 'refs/heads/') }} | |
type=ref,event=branch | |
type=ref,event=pr | |
type=pep440,pattern={{version}} | |
type=pep440,pattern={{major}}.{{minor}} | |
type=raw,value=latest,enable={{is_default_branch}} | |
labels: | | |
org.opencontainers.image.vendor=HathiTrust Research Center | |
- name: Retrieve saved build | |
uses: actions/download-artifact@v4 | |
with: | |
name: build-artifacts | |
path: build | |
- name: Unpack build artifacts | |
run: | | |
tar xzf build-artifacts.tgz && rm -f build-artifacts.tgz | |
working-directory: build | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.DOCKER_REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Docker images | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Repository Dispatch | |
if: github.event.inputs.environment == '' | |
uses: peter-evans/repository-dispatch@v3 | |
env: | |
HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} | |
with: | |
token: ${{ secrets.PAT }} | |
repository: htrc/torchlite-argocd | |
event-type: argocd | |
client-payload: >- | |
{ | |
"image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", | |
"commit_msg": ${{ env.HEAD_COMMIT_MESSAGE }}, | |
"repository": "${{ github.event.repository.name }}", | |
"environment": "${{ github.event.inputs.environment }}", | |
"ref": "${{ github.head_ref || github.ref_name }}", | |
"actor": "${{ github.triggering_actor }}" | |
} | |
- name: Manual Repository Dispatch | |
if: github.event.inputs.environment != '' | |
uses: peter-evans/repository-dispatch@v3 | |
env: | |
HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} | |
with: | |
token: ${{ secrets.PAT }} | |
repository: htrc/torchlite-argocd | |
event-type: argocd-manual | |
client-payload: >- | |
{ | |
"image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", | |
"commit_msg": ${{ env.HEAD_COMMIT_MESSAGE }}, | |
"repository": "${{ github.event.repository.name }}", | |
"environment": "${{ github.event.inputs.environment }}", | |
"ref": "${{ github.head_ref || github.ref_name }}", | |
"actor": "${{ github.triggering_actor }}" | |
} |