From e9d976b2ae4190effc93c61f19523be6134aa44d Mon Sep 17 00:00:00 2001 From: Ben Courliss Date: Wed, 23 Aug 2023 10:56:33 -0400 Subject: [PATCH] Updated to use git SHA for release label on Docker image Signed-off-by: Ben Courliss --- .github/workflows/build-push.yml | 67 ++++++++++++++++++-------------- build.sbt | 2 +- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml index 897a08db..d2e7900d 100644 --- a/.github/workflows/build-push.yml +++ b/.github/workflows/build-push.yml @@ -28,31 +28,48 @@ jobs: # Ensure that the repo variables and secrets are set before running any other steps - name: Check User Set Variables run: | - if [[ -z "$DOCKER_USER" ]]; then \ - echo "::error::Secret DOCKER_USER was not set"; \ - exit 1; \ + if [[ -z "$DOCKER_USER" ]]; then + echo "::error::Secret DOCKER_USER was not set" + exit 1 fi - if [[ -z "$DOCKER_TOKEN" ]]; then \ - echo "::error::Secret DOCKER_TOKEN was not set"; \ - exit 1; \ + if [[ -z "$DOCKER_TOKEN" ]]; then + echo "::error::Secret DOCKER_TOKEN was not set" + exit 1 fi - if [[ -z "$IMAGE_REPO" ]]; then \ - echo "::error::Variable DOCKERHUB_REPO was not set"; \ - exit 1; \ + if [[ -z "$IMAGE_REPO" ]]; then + echo "::error::Variable DOCKERHUB_REPO was not set" + exit 1 fi - if [[ -z "$RUN_NUMBER_OFFSET" ]]; then \ - echo "::error::Variable RUN_NUMBER_OFFSET was not set"; \ - exit 1; \ + if [[ -z "$RUN_NUMBER_OFFSET" ]]; then + echo "::error::Variable RUN_NUMBER_OFFSET was not set" + exit 1 fi env: DOCKER_USER: ${{ secrets.DOCKER_USER }} DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + # Checkout our Github repo + - name: Checkout Github Repo + uses: actions/checkout@v3 + # Offset our version build number to prevent collisions - - name: Offset Build Number - id: offset + - name: Configure Env Vars run: | - echo "BUILD_NUMBER=$(($RUN_NUMBER + $RUN_NUMBER_OFFSET))" >> $GITHUB_ENV + BUILD_NUMBER=$(($RUN_NUMBER + $RUN_NUMBER_OFFSET)) + echo "IMAGE_VERSION=$(head -n 1 src/main/resources/version.txt)-$BUILD_NUMBER" >> $GITHUB_ENV + echo "GIT_SHORT_SHA=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV + + - name: Validate Env Vars + run: | + echo "Using SHORT SHA: $GIT_SHORT_SHA" + if [[ -z "$GIT_SHORT_SHA" ]]; then + echo "::error::unable to get short sha from $GITHUB_SHA" + exit 1 + fi + if [[ -z "$IMAGE_VERSION" ]]; then + echo "::error::unable to get IMAGE_VERSION" + exit 1 + fi # Upgrade Docker engine version, needed for building images. - name: Install Latest Docker Version @@ -64,6 +81,11 @@ jobs: sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + # Setup Scala + # Comes from open source action: https://github.com/coursier/setup-action + - name: Setup Scala + uses: coursier/setup-action@v1 + # Authenticate Dockerhub to allow pushing to our image repo - name: Login to Dockerhub uses: docker/login-action@v2 @@ -78,21 +100,6 @@ jobs: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - # Checkout our Github repo - - name: Checkout Github Repo - uses: actions/checkout@v3 - - # Setup Scala - # Comes from open source action: https://github.com/coursier/setup-action - - name: Setup Scala - uses: coursier/setup-action@v1 - - # Configure version variables - - name: Config Version Variables - id: config-version - run: | - echo "IMAGE_VERSION=$(head -n 1 src/main/resources/version.txt)-$BUILD_NUMBER" >> $GITHUB_ENV # Compile - name: Compile diff --git a/build.sbt b/build.sbt index 8866d3e0..bced17b4 100644 --- a/build.sbt +++ b/build.sbt @@ -33,7 +33,7 @@ lazy val root = (project in file(".")) description := "'Containerized exchange-api'", name := "amd64_exchange-api", organization := "org.openhorizon", - release := versionFunc(), + release := sys.env.getOrElse("GIT_SHORT_SHA", versionFunc()), resolvers += Classpaths.typesafeReleases, scalaVersion := "2.13.10", summary := "'Open Horizon exchange-api image'",