Build a Docker image (single-arch)
This workflows builds a Docker image for a single architecture, and deploys it to a server.
.github/workflows/build.yml
name: 'Build and publish'
on:
workflow_dispatch:
# Allows to manually run the workflow from GitHub's UI
schedule:
# Rebuilds the image automatically every Saturday,
# to ensure it stays up-to-date and secure
- cron: '0 4 * * 6'
push:
branches:
- 'main'
jobs:
build:
name: 'Build Docker image and push to GHCR'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Calculate short form of current git commit
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: true
context: .
tags: ghcr.io/${{ github.repository }}:latest,ghcr.io/${{ github.repository }}:git-${{ steps.vars.outputs.sha_short }}
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}-buildcache
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-buildcache,mode=max
- name: Replace variables in stack file
env:
IMAGE_TAG: git-${{ steps.vars.outputs.sha_short }}
run: >
sed -e "s/\${IMAGE_TAG}/$IMAGE_TAG/" \
-i .github/workflows/deploy/stack.yml
cat .github/workflows/deploy/stack.yml
- name: Trigger deployment
env:
API_KEY: ${{ secrets.StackSparkApiKey }}
STACKSPARK_HOST: ${{ secrets.StackSparkHost }}
STACK_NAME: notes
run: >
curl --fail -H "X-API-Key: $API_KEY" --data-binary "@.github/workflows/deploy/stack.yml" https://$STACKSPARK_HOST/deploy?stackName=$STACK_NAME