Skip to content

Commit

Permalink
Merge pull request #41 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Improve Docker image build
  • Loading branch information
andyone authored Dec 6, 2022
2 parents 8798514 + 2e75972 commit 033af5f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Dockerfile → .docker/alpine.docker
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## REGISTRY CONFIGURATION ######################################################

ARG REGISTRY="docker.io"

################################################################################

FROM essentialkaos/alpine:3.13
FROM ${REGISTRY}/essentialkaos/alpine:3.15

LABEL org.opencontainers.image.title="web-utils" \
org.opencontainers.image.description="Helpers for working with web server" \
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Check dockerfiles with Hadolint
uses: essentialkaos/hadolint-action@v1
with:
files: Dockerfile
files: .docker/*.docker

DockerBuild:
name: Docker Build Check
Expand All @@ -66,8 +66,11 @@ jobs:
needs: [Hadolint, Perfecto]

env:
DOCKER_FILE: Dockerfile
IMAGE_NAME: web-utils
REGISTRY: ghcr.io

strategy:
matrix:
image: [ 'alpine' ]

steps:
- name: Check event type
Expand Down Expand Up @@ -103,11 +106,11 @@ jobs:
- name: Build Docker image
if: ${{ github.event_name == 'pull_request' }}
run: |
docker build -f ${{ env.DOCKER_FILE }} -t ${{ env.IMAGE_NAME }} .
docker build --build-arg REGISTRY=${REGISTRY} -f .docker/${{matrix.image}}.docker -t ${{matrix.image}} .
- name: Show info about built Docker image
uses: essentialkaos/docker-info-action@v1
if: ${{ github.event_name == 'pull_request' }}
with:
image: ${{ env.IMAGE_NAME }}
image: ${{matrix.image}}
show-labels: true
49 changes: 33 additions & 16 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
name: Docker Build & Publish
runs-on: ubuntu-latest

strategy:
matrix:
image: [ 'alpine' ]

steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -63,17 +67,17 @@ jobs:
exit 1
fi
docker_file="Dockerfile"
base_image=$(grep 'FROM ' $docker_file | tail -1 | cut -f2 -d' ')
docker_file=".docker/${{matrix.image}}.docker"
base_image=$(grep 'FROM ' $docker_file | grep -v 'builder' | sed 's#${REGISTRY}/##' | tail -1 | cut -f2 -d' ')
if [[ -z "$base_image" ]] ; then
echo "::error::Can't extract base image info"
exit 1
fi
echo "::set-output name=version::$version"
echo "::set-output name=dockerfile::$docker_file"
echo "::set-output name=baseimage::$base_image"
echo "version=$version" >> $GITHUB_OUTPUT
echo "dockerfile=$docker_file" >> $GITHUB_OUTPUT
echo "baseimage=$base_image" >> $GITHUB_OUTPUT
echo -e "\033[34mVersion:\033[0m $version"
echo -e "\033[34mDockerfile:\033[0m $docker_file"
Expand All @@ -83,14 +87,14 @@ jobs:
id: build_check
run: |
if [[ "${{github.event_name}}" == "release" ]] ; then
echo "::set-output name=build::true"
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
echo -e "::group::\033[34mDownloading built image…\033[0m"
if ! docker pull ghcr.io/${{env.IMAGE_NAME}}:latest ; then
echo "::error::Can't download image ghcr.io/${{env.IMAGE_NAME}}:latest"
if ! docker pull ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}} ; then
echo "::error::Can't download image ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}}"
exit 1
fi
Expand All @@ -111,27 +115,40 @@ jobs:
exit 1
fi
if ! docker inspect "ghcr.io/${{env.IMAGE_NAME}}:latest" | jq -r '.[0].RootFS.Layers' | grep -q "$base_layer" ; then
if ! docker inspect "ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}}" | jq -r '.[0].RootFS.Layers' | grep -q "$base_layer" ; then
echo "::warning::Rebuild image (reason: base image rebuilt)"
echo "::set-output name=build::true"
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
- name: Build and push Docker image
- name: Build and push Docker images (Docker)
if: ${{ steps.build_check.outputs.build == 'true' }}
uses: docker/build-push-action@v3
with:
push: true
context: .
file: ${{steps.metadata.outputs.dockerfile}}
build-args: |
REGISTRY=docker.io
tags: |
${{env.IMAGE_NAME}}:${{matrix.image}}-${{steps.metadata.outputs.version}}
${{env.IMAGE_NAME}}:${{matrix.image}}
- name: Build and push Docker images (GHCR)
if: ${{ steps.build_check.outputs.build == 'true' }}
uses: docker/build-push-action@v3
with:
push: true
context: .
file: ${{steps.metadata.outputs.dockerfile}}
build-args: |
REGISTRY=ghcr.io
tags: |
ghcr.io/${{env.IMAGE_NAME}}:latest
ghcr.io/${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
${{env.IMAGE_NAME}}:latest
${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}}-${{steps.metadata.outputs.version}}
ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}}
- name: Show info about built Docker image
uses: essentialkaos/docker-info-action@v1
with:
image: ghcr.io/${{env.IMAGE_NAME}}:latest
image: ghcr.io/${{env.IMAGE_NAME}}:${{matrix.image}}
show-labels: true

0 comments on commit 033af5f

Please sign in to comment.