Skip to content

Commit

Permalink
Add options to suffix a tag and pass docker build options
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Mar 30, 2022
1 parent 1c7e4a6 commit 3ec303d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ jobs:
# skip-login: <true|false>
# repository: ""
# repository-suffix: ""
# tag-suffix: ""
# build-directory: ""
# build-options: ""
```

## Functions
Expand Down Expand Up @@ -160,7 +162,9 @@ It can receive these optional arguments:

* `-r <repo>`: Override the repository where the image is pushed
* `-s <suffix>`: Override the repository by adding a suffix to the one specified by the environment variable
* `-t <suffix>`: Append a suffix to the image tags (e.g. `-next`)
* `-d <dir>`: Build from the specified directory
* `-o "<options>"`: Options to directly pass to the docker build command

This function can be called several times to build different images within the same build. For example:

Expand Down
5 changes: 3 additions & 2 deletions action.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/bash
# Build script for the GitHub Action
set -eo pipefail
set -xeo pipefail

# Collect action inputs to pass as command options
SETUP_OPTS=""
Expand All @@ -10,6 +10,7 @@ SETUP_OPTS=""
BUILD_OPTS=""
[ -z $INPUT_REPOSITORY ] || BUILD_OPTS="$BUILD_OPTS -r $INPUT_REPOSITORY"
[ -z $INPUT_REPOSITORY_SUFFIX ] || BUILD_OPTS="$BUILD_OPTS -s $INPUT_REPOSITORY_SUFFIX"
[ -z $INPUT_TAG_SUFFIX ] || BUILD_OPTS="$BUILD_OPTS -t $INPUT_TAG_SUFFIX"
[ -z $INPUT_BUILD_DIRECTORY ] || BUILD_OPTS="$BUILD_OPTS -d $INPUT_BUILD_DIRECTORY"

# Loads the script from this repository
Expand All @@ -22,4 +23,4 @@ dockerSetup $SETUP_OPTS
echo $VERSION > VERSION

# Build and push the Docker image
dockerBuildAndPush $BUILD_OPTS
dockerBuildAndPush $BUILD_OPTS -o "$INPUT_BUILD_OPTIONS"
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ inputs:
repository-suffix:
type: string

tag-suffix:
type: string

build-directory:
type: string

build-options:
type: string

runs:
using: composite

Expand All @@ -29,5 +35,7 @@ runs:
INPUT_LATEST: ${{ inputs.latest }}
INPUT_REPOSITORY: ${{ inputs.repository }}
INPUT_REPOSITORY_SUFFIX: ${{ inputs.repository-suffix }}
INPUT_TAG_SUFFIX: ${{ inputs.tag-suffix }}
INPUT_BUILD_DIRECTORY: ${{ inputs.build-directory }}
INPUT_BUILD_OPTIONS: ${{ inputs.build-options }}
shell: bash
17 changes: 13 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ dockerBuildAndPush() {
local REPO=$DOCKER_REPOSITORY
local DIR="."
local OPTIND
local BUILD_OPTS

while getopts ":r:d:s:" opt "$@"; do
while getopts ":r:d:s:t:o:" opt "$@"; do
case ${opt} in
r)
REPO=$OPTARG
Expand All @@ -117,21 +118,29 @@ dockerBuildAndPush() {
DIR=$OPTARG
;;

t)
TAG_SUFFIX=$OPTARG
;;

o)
BUILD_OPTS=$OPTARG
;;

*)
;;
esac
done

local IMAGE="${REPO}:${DOCKER_TAG}"
local IMAGE="${REPO}:${DOCKER_TAG}${TAG_SUFFIX}"

echo "Building image ${IMAGE} from ${DIR}"
docker build -t "${IMAGE}" "${DIR}"
docker build ${BUILD_OPTS} -t "${IMAGE}" "${DIR}"

echo "Pushing ${IMAGE}"
docker push "${IMAGE}"

if [[ -n "$EXTRA_DOCKER_TAG" ]]; then
local EXTRA_IMAGE="${REPO}:${EXTRA_DOCKER_TAG}"
local EXTRA_IMAGE="${REPO}:${EXTRA_DOCKER_TAG}${TAG_SUFFIX}"
echo "Tagging also as $EXTRA_IMAGE"
docker tag "${IMAGE}" "${EXTRA_IMAGE}"

Expand Down

0 comments on commit 3ec303d

Please sign in to comment.