Files
tryno10/.github/workflows/docker.yml
T
2022-04-08 04:15:12 +05:30

156 lines
4.3 KiB
YAML

---
name: ci__&__cd__pipeline
env:
TEST_TAG: user/myapp:test
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'
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:
built and test images:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker 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: Build docker image for test
uses: docker/build-push-action@v2
with:
context: app
load: true
tags: ${{ env.TEST_TAG }}
-
name: Test
run: |
- docker exec -it ${{ env.TEST_TAG }} php artisan test
- docker run --rm ${{ env.TEST_TAG }}
-
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: ${{ github.event_name != 'pull_request' }}
tags: buzzerburger/test:latest
# labels: ${{ steps.docker_meta.outputs.labels }}
-
name: Show image digest
run: echo ${{ steps.docker_build_and_push.outputs.digest }}