Skip to content

Docker Build and Publish #780

Docker Build and Publish

Docker Build and Publish #780

name: Docker Build and Publish
on:
schedule:
- cron: '37 7 * * *'
push:
branches: [ "main" ]
paths-ignore:
- '**/README.md'
pull_request:
branches: [ "main" ]
paths-ignore:
- '**/README.md'
workflow_dispatch:
env:
## github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
jobs:
## define job to build and publish docker image
build-and-push-docker-image:
name: Build Docker image and push to DockerHub and Github Container Registry
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
## This is used to complete the identity challenge
## with sigstore/fulcio when running outside of PRs.
id-token: write
steps:
## https://github.com/actions/checkout
- name: Checkout repository
uses: actions/checkout@v4
## https://github.com/docker/build-push-action
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
## https://github.com/docker/login-action
- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
## https://github.com/docker/login-action
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# ## https://github.com/docker/metadata-action
# - name: Extract Docker metadata
# id: meta
# uses: docker/metadata-action@v5
# with:
# images: docker.io/caddy
# tags: |
# type=semver,pattern={{version}}
# type=semver,pattern={{major}}.{{minor}}
# type=semver,pattern={{major}}
# flavor: |
# latest=true
## https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
provenance: false
context: .
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8
push: ${{ github.event_name != 'pull_request' }}
# tags: ${{ steps.meta.outputs.tags }}
# labels: ${{ steps.meta.outputs.labels }}
tags: |
docker.io/${{ env.IMAGE_NAME }}:latest
ghcr.io/${{ env.IMAGE_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Image digest
run: echo ${{ steps.build-and-push.outputs.digest }}