From 102dbe062efe96d34283e30b0ea68fc302b53e34 Mon Sep 17 00:00:00 2001 From: Christian Kreuzberger Date: Wed, 3 Feb 2021 10:38:31 +0100 Subject: [PATCH] Added docs and multi platform build Signed-off-by: Christian Kreuzberger --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++-- action.yml | 6 +++- build_docker_image.sh | 20 +++++++++--- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 47246a5..596bb54 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,77 @@ This repo provides a utility GitHub action for the [CI Workflow](https://github. ## Inputs -* Todo +* `IMAGE_NAME` - name of the image (including the docker organization) +* `DOCKER_FOLDER` (optional, defaults to './') - folder where Dockerfile is in; needs a trailing slash +* `VERSION` - semantic version used as the tag for the resulting image +* `DATETIME` (optional) - datetime string, e.g., YYYYMMDDHHmmss; (defaults to `DATETIME=$(date +'%Y%m%d%H%M')`) +* `PLATFORMS` (optional) - comma separated list of platforms that the docker image should be build for (e.g., `linux/amd64,linux/arm/v7,linux/arm64`); if set, requires `buildx` to be available (see example below) ## Outputs -* Todo +None at the moment ## Example usage -Todo +### Simple Example + +``` + env: + VERSION: "1.2.3" + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - id: docker_login + name: Docker Login + uses: docker/login-action@v1 + with: + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - id: docker_build + name: Docker Build + uses: keptn/gh-action-build-docker-image@master + with: + PLATFORMS: ${{ env.DOCKER_PLATFORMS }} + VERSION: ${{ env.VERSION }} + IMAGE_NAME: "yourdockerorg/yourimagename" +``` + +### Extended Example using buildx + +``` + env: + VERSION: "1.2.3" + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: Set up QEMU (Docker Multi-Arch Build/BuildX) + # needed for docker multi-architecture build + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx (Docker Multi-Arch Build/BuildX) + # needed for docker multi-architecture build + uses: docker/setup-buildx-action@v1 + + - id: docker_login + name: Docker Login + # only run docker login on pushes; also for PRs, but only if this is not a fork + if: (github.event_name == 'push') || (github.event.pull_request.head.repo.full_name == github.repository) + # note: GH does not allow to access secrets for PRs from a forked repositories due to security reasons + # that's fine, but it means we can't push images to dockerhub + uses: docker/login-action@v1 + with: + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - id: docker_build + name: Docker Build + uses: keptn/gh-action-build-docker-image@master + with: + PLATFORMS: "linux/amd64,linux/arm/v7,linux/arm64" + VERSION: ${{ env.VERSION }} + IMAGE_NAME: "yourdockerorg/yourimagename" + DOCKER_FOLDER: "your-sub-directory/" +``` \ No newline at end of file diff --git a/action.yml b/action.yml index b325daa..8b78165 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,9 @@ inputs: DATETIME: description: Optional datetime for the tag of the docker image (will be set automatically if left empty) default: unset + PLATFORMS: + description: Platform for the docker build (comma separated; requires buildx; leave empty for default) + default: "" runs: using: composite steps: @@ -71,4 +74,5 @@ runs: DOCKER_FOLDER: ${{ inputs.DOCKER_FOLDER }} VERSION: ${{ inputs.VERSION }} DATETIME: ${{ steps.check_datetime.outputs.DATETIME }} - run: ${{ github.action_path }}/build_docker_image.sh "${IMAGE_NAME}" "${DOCKER_FOLDER}" "${VERSION}" "${DATETIME}" + PLATFORMS: ${{ inputs.PLATFORMS }} + run: ${{ github.action_path }}/build_docker_image.sh "${IMAGE_NAME}" "${DOCKER_FOLDER}" "${VERSION}" "${DATETIME}" "${PLATFORMS}" diff --git a/build_docker_image.sh b/build_docker_image.sh index 52f1fa9..9df6cbf 100755 --- a/build_docker_image.sh +++ b/build_docker_image.sh @@ -1,18 +1,19 @@ #!/bin/bash -if [[ "$#" -ne 4 ]]; then +if [[ "$#" -le 4 ]]; then echo "Usage: $0 IMAGE FOLDER VERSION DATETIME" - echo " Example: $0 keptn/api api/ 1.2.3 20210101101210" + echo " Example: $0 keptn/api api/ 1.2.3 20210101101210 amd64" echo " Your command: $0 $*" exit 1 fi # todo: not sure if we can use parameters like this -# ${IMAGE}=$1 ${FOLDER}=$2 ${VERSION}=$3 ${DATETIME}=$4 +# ${IMAGE}=$1 ${FOLDER}=$2 ${VERSION}=$3 ${DATETIME}=$4 ${PLATFORMS}=$5 IMAGE=$1 FOLDER=$2 VERSION=$3 DATETIME=$4 +PLATFORMS=${5:-""} # store pwd pwd=$(pwd) @@ -33,10 +34,19 @@ cd ./${FOLDER} sed -i '/#travis-uncomment/s/^#travis-uncomment //g' Dockerfile sed -i '/#build-uncomment/s/^#build-uncomment //g' Dockerfile cat MANIFEST -docker build . -t "${IMAGE}:${VERSION}.${DATETIME}" -t "${IMAGE}:${VERSION}" --build-arg version="${VERSION}" +if [[ "$PLATFORMS" != "" ]]; then + # use buildx and specify platform + echo "PLATFORMS is set to $PLATFORMS, using buildx" + DOCKER_BUILD="docker buildx build --platform ${PLATFORMS}" +else + # default build without platforms + DOCKER_BUILD="docker build" +fi + +$DOCKER_BUILD . -t "${IMAGE}:${VERSION}.${DATETIME}" -t "${IMAGE}:${VERSION}" --build-arg version="${VERSION}" if [[ $? -ne 0 ]]; then - echo "Failed to build Docker Image ${IMAGE}:${VERSION}.${DATETIME}, exiting" + echo "Failed to build Docker Image ${IMAGE}:${VERSION}.${DATETIME} using ${DOCKER_BUILD}, exiting" echo "::error file=${FOLDER}/Dockerfile::Failed to build Docker Image" exit 1 fi