Skip to content

Merge pull request #730 from open-horizon/issue_594 #43

Merge pull request #730 from open-horizon/issue_594

Merge pull request #730 from open-horizon/issue_594 #43

Workflow file for this run

# Name of the workflow
name: build-push
# This workflow will run when there is a push (including merge) to the listed branches
on:
workflow_dispatch:
push:
branches:
- master
- v2.122
- v2.110
- v2.87
# Variables available to all jobs
env:
IMAGE_REPO: ${{ vars.DOCKERHUB_REPO }}
GITHUB_CONTAINER_REGISTRY: ghcr.io/${{ github.repository_owner }}
GH_BRANCH: ${{ github.ref_name }}
RUN_NUMBER: ${{ github.run_number }}
RUN_NUMBER_OFFSET: ${{ vars.RUN_NUMBER_OFFSET }}
# Jobs that will run when the workflow is triggered
jobs:
# This job will build and then push to docker hub
build-push:
# The type of runner the job will run on
runs-on: ubuntu-latest
steps:
# 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
fi
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
fi
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: Configure Env Vars
run: |
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
run: |
sudo apt-get purge docker-ce docker-ce-cli containerd.io runc containerd moby-buildx moby-cli moby-compose moby-containerd moby-engine moby-runc
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
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
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}
# Authenticate GHCR to allow pushing to our alternate image registry
- name: Login to Github Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# Compile
- name: Compile
run: |
sbt compile
# Package
- name: Package
run: |
sbt package
# Docker Build and Publish Local
## When creating docker image sbt will ~for some reason~ mark the logs as [error] when it should be [info]
- name: Docker Build and Publish Local
run: |
sbt docker:publishLocal
# Push Docker images to Dockerhub
- name: Publish Image to Dockerhub
run: |
docker tag openhorizon/amd64_exchange-api:${IMAGE_VERSION} ${IMAGE_REPO}/amd64_exchange-api:testing
if [[ "$GITHUB_REF" == 'refs/heads/master' ]]; then
docker push ${IMAGE_REPO}/amd64_exchange-api:testing
docker tag ${IMAGE_REPO}/amd64_exchange-api:testing ${GITHUB_CONTAINER_REGISTRY}/amd64_exchange-api:testing
docker push ${GITHUB_CONTAINER_REGISTRY}/amd64_exchange-api:testing
else
docker tag ${IMAGE_REPO}/amd64_exchange-api:testing ${IMAGE_REPO}/amd64_exchange-api:testing_${GH_BRANCH}
docker push ${IMAGE_REPO}/amd64_exchange-api:testing_${GH_BRANCH}
fi