Skip to content

Commit

Permalink
Added docs and multi platform build
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kreuzberger <[email protected]>
  • Loading branch information
christian-kreuzberger-dtx committed Feb 3, 2021
1 parent d0d6abb commit 7798cac
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
```
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}"
20 changes: 15 additions & 5 deletions build_docker_image.sh
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 --platforms \"${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
Expand Down

0 comments on commit 7798cac

Please sign in to comment.