-
Notifications
You must be signed in to change notification settings - Fork 30
133 lines (117 loc) · 4.79 KB
/
build-push.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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