Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
add base-updater
Browse files Browse the repository at this point in the history
  • Loading branch information
madebyTimo committed Mar 25, 2023
1 parent a7eb5ce commit 579d4fa
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/docker-update-base-and-push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "docker-update-base-and-push"

on:
schedule:
- cron: "00 01 * * *"

jobs:
docker-build-and-push:
runs-on: ubuntu-latest
steps:
- name: add builder
run: docker run --privileged --rm tonistiigi/binfmt --install all && docker buildx create --use
- name: checkout repository
uses: actions/checkout@v3
- name: login into repository
run: echo ${{ secrets.DOCKER_REGISTRY_PASSWORD }} | docker login --username ${{ secrets.DOCKER_REGISTRY_USERNAME }} --password-stdin
- name: update base and push
run: ./docker-build.sh --push --update-base
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# folders
.idea
.vscode
builds
data-local
node_modules
target

# files
package-lock.json
1 change: 1 addition & 0 deletions Version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.0.1
13 changes: 8 additions & 5 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
variable "BASE_IMAGE_DATE" {
default = "unknown"
}
variable "IMAGE" {
default = "unknown"
}
variable "VERSION" {
default = "v0.0.1"
}

target "default" {
tags = [
"madebytimo/nginx:latest",
"madebytimo/nginx:${VERSION}",
"madebytimo/nginx:${VERSION}-base-${BASE_IMAGE_DATE}"
"${IMAGE}:latest",
"${IMAGE}:${VERSION}",
"${IMAGE}:${VERSION}-base-${BASE_IMAGE_DATE}"
]
platforms = [
"amd64",
"arm64",
"arm",
"arm"
]
}
}
57 changes: 54 additions & 3 deletions docker-build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
#!/usr/bin/env bash
set -e

BUILD_ARGUMENTS=""
DEPENDENCIES=(docker)
UPDATE_BASE=false
REPOSIITORY_NAME="docker-nginx"
VERSION=""

# help message
for ARGUMENT in "$@"; do
if [ "$ARGUMENT" == "-h" ] || [ "$ARGUMENT" == "--help" ]; then
echo "usage: $(basename "$0") [ARGUMENT]"
echo "Builds the docker image from the Dockerfile."
echo "ARGUMENT can be"
echo "--push push the build"
echo "--update-base only build if newer base image available."
exit
fi
done

# check dependencies
for cmd in ${DEPENDENCIES[@]}; do
if [[ -z "$(command -v $cmd)" ]]; then
echo "$cmd is missing!"
exit 1
fi
done

# check arguments
while [[ -n "$1" ]]; do
if [[ "$1" = "--update-base" ]]; then
UPDATE_BASE=true
elif [[ "$1" = "--push" ]]; then
BUILD_ARGUMENTS+=" --push"
else
echo "Unknown argument: \"$1\""
exit 1
fi
shift
done

BASE_IMAGE="$(tac Dockerfile | grep --max-count=1 "FROM" | cut -d" " -f2)"
docker pull "$BASE_IMAGE"
BASE_IMAGE_DATE="$(docker image inspect --format="{{ .Created }}" "$BASE_IMAGE" | cut -d "T" -f1)"
echo "Base is $BASE_IMAGE from $BASE_IMAGE_DATE"
export BASE_IMAGE BASE_IMAGE_DATE
docker buildx bake -f docker-bake.hcl $@
echo "Base image is $BASE_IMAGE from $BASE_IMAGE_DATE"
IMAGE="madebytimo/${REPOSIITORY_NAME#docker-}"
if [[ "$UPDATE_BASE" == true ]]; then
docker pull "$IMAGE"
PUSHED_IMAGE_DATE="$(docker image inspect --format="{{ .Created }}" "$IMAGE" | cut -d "T" -f1)"
echo "Last pushed image is from $PUSHED_IMAGE_DATE"
if [[ "$BASE_IMAGE_DATE" < "$PUSHED_IMAGE_DATE" ]]; then
echo "Used base image is up to date"
exit;
fi
fi
VERSION="$(cat Version.txt)"

export BASE_IMAGE BASE_IMAGE_DATE IMAGE VERSION
docker buildx bake --file docker-bake.hcl $BUILD_ARGUMENTS

0 comments on commit 579d4fa

Please sign in to comment.