From 73595f86d0cd105967cd8daa38593670778f41c1 Mon Sep 17 00:00:00 2001 From: nugaon <50576770+nugaon@users.noreply.github.com> Date: Tue, 4 May 2021 15:05:47 +0200 Subject: [PATCH] feat: tag with commit hash (#10) * feat: docker image tag setter and getter utility * chore: add new environment variable commit_version_tag (true or false) * feat: handle commit_version_tag in build and bee runner scripts * ci: add ci and introduce the new environment variable set on workflow manual run --- .github/workflows/publish.yaml | 10 +++++++++- scripts/.env | 1 + scripts/bee-cleanup.sh | 2 +- scripts/bee-docker-build.sh | 9 +++++++++ scripts/bee.sh | 2 +- scripts/utils/.gitignore | 2 ++ scripts/utils/build-image-tag.sh | 28 ++++++++++++++++++++++++++++ 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 scripts/utils/.gitignore create mode 100755 scripts/utils/build-image-tag.sh diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 5e6dcf7..d049e63 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -4,6 +4,12 @@ on: buildImage: description: 'Build Docker Image according to the environment' default: 'false' + commitVersionTag: + description: 'The image tag will be retrieved from the bee version command' + default: 'false' + beeVersion: + description: 'The official bee image tag that the image will be built on' + default: 'latest' push: branches: - 'master' @@ -38,6 +44,8 @@ jobs: run: | npm run build:env - name: Publish if it was clicked manually - if: ${{ github.event.inputs.buildImage }} == 'true' && success() + if: "${{ github.event.inputs.buildImage }}" == 'true' && success() run: | + COMMIT_VERSION_TAG="${{ github.event.inputs.commitVersionTag }}" + BEE_VERSION="${{ github.event.inputs.beeVersion }}" npm run publish:env diff --git a/scripts/.env b/scripts/.env index 7ead315..2441122 100755 --- a/scripts/.env +++ b/scripts/.env @@ -2,3 +2,4 @@ BEE_VERSION="0.5.3" BLOCKCHAIN_VERSION="1.0.0" BEE_ENV_PREFIX="swarm-test" BEE_IMAGE_PREFIX="docker.pkg.github.com/ethersphere/bee-factory" +COMMIT_VERSION_TAG="true" diff --git a/scripts/bee-cleanup.sh b/scripts/bee-cleanup.sh index 2b865bf..ef4e9f3 100755 --- a/scripts/bee-cleanup.sh +++ b/scripts/bee-cleanup.sh @@ -15,8 +15,8 @@ done echo "Removing built Bee Docker images..." +BEE_VERSION=$("$MY_PATH/utils/build-image-tag.sh" get) BEE_ENV_PREFIX=$("$MY_PATH/utils/env-variable-value.sh" BEE_ENV_PREFIX) -BEE_VERSION=$("$MY_PATH/utils/env-variable-value.sh" BEE_VERSION) BEE_IMAGE_PREFIX=$("$MY_PATH/utils/env-variable-value.sh" BEE_IMAGE_PREFIX) DOCKER_IMAGES=$(docker image ls -qaf reference="$BEE_IMAGE_PREFIX/$BEE_ENV_PREFIX*:$BEE_VERSION") for DOCKER_IMAGE in $DOCKER_IMAGES diff --git a/scripts/bee-docker-build.sh b/scripts/bee-docker-build.sh index 466aadb..9b50fdf 100755 --- a/scripts/bee-docker-build.sh +++ b/scripts/bee-docker-build.sh @@ -29,6 +29,15 @@ sudo chmod 777 -R "$MY_PATH/bee-data-dirs" echo "Update common dockerfile" dockerfile "$MY_PATH/bee-data-dirs/Dockerfile" "$BEE_VERSION" +# If the user has been set the COMMIT_VERSION_TAG env variable +# The image will be built with the tag that is the bee version string +COMMIT_VERSION_TAG="$("$MY_PATH/utils/env-variable-value.sh" COMMIT_VERSION_TAG)" +if [ "$COMMIT_VERSION_TAG" == "true" ] ; then + # somehow the version command's output goes to the stderr + BEE_VERSION=$(docker run --rm ethersphere/bee:$BEE_VERSION version 2>&1) + "$MY_PATH/utils/build-image-tag.sh" set "$BEE_VERSION" +fi + echo "Build Dockerfiles" for BEE_DIR in $BEE_DIRS do diff --git a/scripts/bee.sh b/scripts/bee.sh index edbd652..20a7ab9 100755 --- a/scripts/bee.sh +++ b/scripts/bee.sh @@ -59,8 +59,8 @@ log_queen() { MY_PATH=$(dirname "$0") # relative MY_PATH=$( cd "$MY_PATH" && pwd ) # absolutized and normalized # Check used system variable set +BEE_VERSION=$("$MY_PATH/utils/build-image-tag.sh" get) BEE_IMAGE_PREFIX=$("$MY_PATH/utils/env-variable-value.sh" BEE_IMAGE_PREFIX) -BEE_VERSION=$("$MY_PATH/utils/env-variable-value.sh" BEE_VERSION) BEE_ENV_PREFIX=$("$MY_PATH/utils/env-variable-value.sh" BEE_ENV_PREFIX) # Init variables diff --git a/scripts/utils/.gitignore b/scripts/utils/.gitignore new file mode 100644 index 0000000..07117b5 --- /dev/null +++ b/scripts/utils/.gitignore @@ -0,0 +1,2 @@ +# ignore tmp file for custom image tag +.commit-version-tag diff --git a/scripts/utils/build-image-tag.sh b/scripts/utils/build-image-tag.sh new file mode 100755 index 0000000..8d9c011 --- /dev/null +++ b/scripts/utils/build-image-tag.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# Store/get the tag of the Docker image that will be built + +store_custom_tag() { + echo "$1" > "$MY_PATH/$COMMIT_VERSION_TAG_FILENAME" +} + +# Echos the image tag which is defined by the environment or has been extracted from the Bee version command +get_tag() { + COMMIT_VERSION_TAG=$("$MY_PATH/env-variable-value.sh" COMMIT_VERSION_TAG) + if [ "$COMMIT_VERSION_TAG" == 'true' ] ; then + # retrieve from the output of previous store action + cat "$MY_PATH/$COMMIT_VERSION_TAG_FILENAME" + else + BEE_VERSION=$("$MY_PATH/env-variable-value.sh" BEE_VERSION) + echo "$BEE_VERSION" + fi +} + +MY_PATH=$(dirname "$0") +MY_PATH=$( cd "$MY_PATH" && pwd ) +COMMIT_VERSION_TAG_FILENAME=".commit-version-tag" + +if [ "$1" == "set" ] ; then + store_custom_tag "$2" +elif [ "$1" == "get" ] ; then + get_tag +fi