Files
tryno10/.github/workflows/docker.yml
T
2022-04-08 03:24:21 +05:30

149 lines
4.2 KiB
YAML

---
name: Build and Push Image
on:
# we want pull requests so we can build(test) but not push to image registry
pull_request:
branches:
- 'main'
# only build when important files change
paths-ignore:
# - 'README.md'
# - '.github/workflows/linter.yml'
- '.github/**'
- 'nginx/**'
- 'docker-compose.yml'
- 'docker-compose.dev.yml'
- 'docker-compose.prod.yml'
push:
branches:
- 'main'
# only build when important files change
paths-ignore:
- '.github/**'
- 'nginx/**'
- 'docker-compose.yml'
- 'docker-compose.dev.yml'
- 'docker-compose.prod.yml'
# schedule:
# # re-run montly to keep image fesh with upstream base images
# - cron: '0 12 15 * *'
workflow_dispatch:
# run whenever we want!
workflow_call:
# allow reuse of this workflow in other repos
inputs:
dockerhub-enable:
description: Log in and push to Docker Hub
required: false
type: boolean
default: true
dockerhub-username:
description: Docker Hub username
required: false
type: string
default: buzzerburger
context:
description: Docker context (path) to start build from
required: false
type: string
default: .
target:
description: Build stage to target
required: false
type: string
file:
description: Name of Dockerfile (in relation to context path)
required: false
type: string
platforms:
description: Platforms to build for
required: false
type: string
default: linux/amd64,linux/arm64
image-names:
description: A list of the account/repo names for docker build
required: false
type: string
default: |
ghcr.io/buzzerburger/test
secrets:
dockerhub-token:
description: Docker Hub token
required: false
jobs:
build-and-push-images:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
# we need qemu and buildx so we can build multiple platforms later
name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v1.2.0
-
# BuildKit (used with `docker buildx`) is the best way to build images
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Docker meta
id: docker_meta
uses: docker/metadata-action@v3.6.2
with:
# list of Docker images to use as base name for tags
images: |
buzzerburger/test
ghcr.io/buzzerburger/test
flavor: |
latest=false
tags: |
type=raw,value=latest
-
# this will build the images, once per platform,
# then push to both Docker Hub and GHCR
name: Docker Build and Push
id: docker_build_and_push
uses: docker/build-push-action@v2
with:
platforms: linux/amd64
context: app
target: ${{ inputs.target }}
file: ${{ inputs.file }}
builder: ${{ steps.buildx.outputs.name }}
# it uses github cache API for faster builds:
# https://github.com/crazy-max/docker-build-push-action/blob/master/docs/advanced/cache.md#cache-backend-api
cache-from: type=gha
cache-to: type=gha,mode=max
# for an approved pull_request, only push pr-specific tags
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
# labels: ${{ steps.docker_meta.outputs.labels }}
-
name: Show image digest
run: echo ${{ steps.docker_build_and_push.outputs.digest }}