Improve Docker build

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2020-11-05 14:11:59 +01:00
parent 6500fdbdd1
commit b971415f0c
No known key found for this signature in database
GPG key ID: 34CC109EB5ED1C2A

View file

@ -1,9 +1,13 @@
name: CI name: CI
on: on:
schedule:
- cron: '0 5 * * *' # everyday at 5 am UTC
push: push:
branches: branches:
- master - master
tags:
- v[0-9]+.[0-9]+.[0-9]+
pull_request: pull_request:
jobs: jobs:
@ -87,21 +91,40 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Get Version - name: Calculate Docker image tags
id: info id: tags
env:
DOCKER_IMAGES: "ghcr.io/dexidp/dex dexidp/dex"
run: | run: |
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') VERSION=noop
- name: Determine Docker Tag if [[ "${{ github.event_name }}" == "schedule" ]]; then
uses: haya14busa/action-cond@v1 VERSION=nightly
id: imagetag else
with: case $GITHUB_REF in
cond: ${{ github.event_name == 'pull_request' }} refs/tags/*) VERSION=${GITHUB_REF#refs/tags/};;
if_true: ${{ github.sha }} refs/heads/*) VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g');;
if_false: 'master' refs/pull/*) VERSION=pr-${{ github.event.number }};;
*) VERSION=sha-${GITHUB_SHA::8};;
esac
fi
TAGS=()
for image in $DOCKER_IMAGES; do
TAGS+=("${image}:${VERSION}")
if [[ "${{ github.event.repository.default_branch }}" == "$VERSION" ]]; then
TAGS+=("${image}:latest")
fi
done
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::$(IFS=,; echo "${TAGS[*]}")
echo ::set-output name=commit_hash::${GITHUB_SHA::8}
echo ::set-output name=build_date::$(git show -s --format=%cI)
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v1 uses: docker/setup-qemu-action@v1
@ -109,7 +132,6 @@ jobs:
platforms: all platforms: all
- name: Set up Docker Buildx - name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1 uses: docker/setup-buildx-action@v1
with: with:
install: true install: true
@ -117,6 +139,14 @@ jobs:
# TODO: Remove driver-opts once fix is released docker/buildx#386 # TODO: Remove driver-opts once fix is released docker/buildx#386
driver-opts: image=moby/buildkit:master driver-opts: image=moby/buildkit:master
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
if: github.event_name == 'push'
- name: Login to Docker Hub - name: Login to Docker Hub
uses: docker/login-action@v1 uses: docker/login-action@v1
with: with:
@ -124,20 +154,24 @@ jobs:
password: ${{ secrets.DOCKER_PASSWORD }} password: ${{ secrets.DOCKER_PASSWORD }}
if: github.event_name == 'push' if: github.event_name == 'push'
- name: Build and Push - name: Build and push
uses: docker/build-push-action@v2 uses: docker/build-push-action@v2
with: with:
context: . context: .
file: Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64 platforms: linux/amd64,linux/arm/v7,linux/arm64
push: ${{ github.event_name == 'push' }} push: ${{ github.event_name == 'push' }}
tags: dexidp/dex:${{ steps.imagetag.outputs.value }} tags: ${{ steps.tags.outputs.tags }}
build-args: |
VERSION=${{ steps.tags.outputs.version }}
COMMIT_HASH=${{ steps.tags.outputs.commit_hash }}
BUILD_DATE=${{ steps.tags.outputs.build_date }}
labels: | labels: |
org.opencontainers.image.title=${{ github.event.repository.name }} org.opencontainers.image.title=${{ github.event.repository.name }}
org.opencontainers.image.description=${{ github.event.repository.description }} org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.url=${{ github.event.repository.html_url }} org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.source=${{ github.event.repository.clone_url }} org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.version=${{ steps.imagetag.outputs.value }} org.opencontainers.image.version=${{ steps.tags.outputs.version }}
org.opencontainers.image.created=${{ steps.info.outputs.created }} org.opencontainers.image.created=${{ steps.tags.outputs.build_date }}
org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }} org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
org.opencontainers.image.documentation=https://dexidp.io/docs/