Skip to content

Commit 15aec70

Browse files
ljones140elrayle
andauthored
Deploy image to docker optionally, as well as GHCR (#97)
To migrate deployment of Production Crawler to CD Azure from Microsoft owned Azure. Changes to workflow app-build-and-deploy Enables the optional docker image publish to Docker Hub, conditional on providing Input docker-hub-username secret DOCKERHUB_TOKEN Without these the action only deploys to ghcr --------- Co-authored-by: E. Lynette Rayle <elrayle@users.noreply.github.com>
1 parent bfb99d7 commit 15aec70

File tree

5 files changed

+60
-27
lines changed

5 files changed

+60
-27
lines changed

.github/workflows/app-build-and-deploy.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ on:
2323
PRODUCTION_DEPLOYERS:
2424
description: 'Name of the team that defines who can deploy to production - Defined in org action secrets'
2525
required: true
26+
DOCKERHUB_TOKEN:
27+
description: 'Token to be used if publishing image to dockerhub, input.docker-hub-username must also be provided'
28+
required: false
2629

2730
inputs:
2831
deploy-env:
@@ -49,8 +52,11 @@ on:
4952
description: 'optionally pass in build args to the Docker build command (e.g. "MY_VAR=my_value")'
5053
required: false
5154
type: string
55+
docker-hub-username:
56+
description: 'optionally pass to publish image to docker-hub'
57+
required: false
58+
type: string
5259

53-
5460
jobs:
5561
determine-trigger:
5662
name: Determine if this was triggered by a release or workflow_dispatch
@@ -79,7 +85,7 @@ jobs:
7985
uses: actions/checkout@v4.1.1
8086
with:
8187
repository: 'clearlydefined/operations'
82-
ref: 'v3.0.0'
88+
ref: 'v3.1.0'
8389
path: 'operations'
8490
- name: Get version from package-lock.json
8591
id: get_version
@@ -101,11 +107,13 @@ jobs:
101107
secrets:
102108
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
103109
PRODUCTION_DEPLOYERS: ${{ secrets.PRODUCTION_DEPLOYERS }}
110+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
104111
with:
105112
deploy-env: ${{ inputs.deploy-env }}
106113
application-type: ${{ inputs.application-type }}
107114
application-version: ${{ needs.get-version.outputs.version }}
108115
build-args: ${{ inputs.docker-build-args }}
116+
docker-hub-username: ${{ inputs.docker-hub-username }}
109117

110118
deploy-primary-app-to-azure:
111119
name: Deploy to primary Azure app

.github/workflows/app-build-docker-image.yml

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ on:
1010
PRODUCTION_DEPLOYERS:
1111
description: 'Name of the team that defines who can deploy to production - Defined in org action secrets'
1212
required: true
13+
DOCKERHUB_TOKEN:
14+
description: 'Token to be used if publishing image to dockerhub, input.docker-hub-username must also be provided'
15+
required: false
16+
1317

1418
inputs:
1519
deploy-env:
@@ -28,6 +32,10 @@ on:
2832
description: 'optionally pass in build args to the Docker build command (e.g. "MY_VAR=my_value")'
2933
required: false
3034
type: string
35+
docker-hub-username:
36+
description: 'optionally pass to publish image to docker-hub'
37+
required: false
38+
type: string
3139

3240
outputs:
3341
docker-image-name-with-tag:
@@ -42,36 +50,52 @@ jobs:
4250
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
4351
PRODUCTION_DEPLOYERS: ${{ secrets.PRODUCTION_DEPLOYERS }}
4452

45-
determine-image-name:
53+
build-image-names:
4654
name: Determine Image Name
4755
runs-on: ubuntu-latest
48-
outputs:
49-
docker-image-name-with-tag: "${{ env.DOCKER_IMAGE_NAME_WITH_TAG }}"
56+
outputs:
57+
names-with-tags: ${{ env.DOCKER_IMAGE_TAGS }}
5058
steps:
5159
- name: Checkout this repo
5260
uses: actions/checkout@v4.1.1
5361
with:
5462
repository: 'clearlydefined/operations'
55-
ref: 'v3.0.0'
63+
ref: '3.1.0'
5664
path: 'operations'
57-
- name: Determine Docker Image Name
58-
id: determine_image_name
65+
- name: Determine Image Name
66+
id: determine-image-name
5967
run: |
6068
echo "BUILD_ARGS=${{ inputs.build-args }}"
6169
script_log=$(./operations/scripts/app-workflows/determine-image-name.sh \
62-
"${{ github.repository }}" \
70+
"${{ github.event.repository.name }}" \
6371
"${{ inputs.deploy-env }}" \
6472
"${{ inputs.application-version }}") || (echo "$script_log" && exit 1)
6573
echo -e "---- script log\n$script_log\n----"; \
6674
image_name=$(echo "$script_log" | tail -n 1)
67-
echo "DOCKER_IMAGE_NAME_WITH_TAG=$image_name" >> $GITHUB_ENV
75+
echo "IMAGE_NAME=$image_name" >> $GITHUB_ENV
76+
- name: Add ghcr.io
77+
id: add-ghcr
78+
run: |
79+
echo "DOCKER_IMAGE_TAGS=ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}" >> $GITHUB_ENV
80+
- name: add-dockerhub
81+
if: ${{ inputs.docker-hub-username != '' }}
82+
id: add-dockerhub
83+
run: |
84+
echo "DOCKER_IMAGE_TAGS=${{ env.DOCKER_IMAGE_TAGS }},${{ inputs.docker-hub-username }}/${{ env.IMAGE_NAME }}" >> $GITHUB_ENV
85+
6886
6987
build-docker-image:
7088
name: Build Image
7189
runs-on: ubuntu-latest
72-
needs: [check-deployable, determine-image-name]
90+
needs: [check-deployable, build-image-names]
7391
steps:
7492
- uses: actions/checkout@v4.1.1
93+
- name: Log into Docker Hub
94+
if: ${{ inputs.docker-hub-username != '' }}
95+
uses: docker/login-action@v3.3.0
96+
with:
97+
username: ${{ inputs.docker-hub-username }}
98+
password: ${{ secrets.DOCKERHUB_TOKEN }}
7599

76100
- name: Log into ghcr registry
77101
uses: docker/login-action@v3.0.0
@@ -91,7 +115,8 @@ jobs:
91115
APP_VERSION=${{ inputs.application-version }}
92116
BUILD_SHA=${{ github.sha }}
93117
${{ inputs.build-args }}
94-
tags: ${{ needs.determine-image-name.outputs.docker-image-name-with-tag }}
118+
tags: |
119+
${{ needs.build-image-names.outputs.names-with-tags }}
95120
labels: |
96121
env=${{ inputs.deploy-env }}
97122
type=${{ inputs.application-type }}

.github/workflows/app-is-deployable.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: actions/checkout@v4.1.1
2727
with:
2828
repository: 'clearlydefined/operations'
29-
ref: 'v3.0.0'
29+
ref: 'v3.1.0'
3030
path: 'operations'
3131
- id: confirm-dev
3232
shell: bash
@@ -47,7 +47,7 @@ jobs:
4747
uses: actions/checkout@v4.1.1
4848
with:
4949
repository: 'clearlydefined/operations'
50-
ref: 'v3.0.0'
50+
ref: '3.1.0'
5151
path: 'operations'
5252

5353
- name: Get organization ID

scripts/app-workflows/determine-image-name.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#!/bin/bash
22

33
# Inputs
4-
# $1 - repo: the orgname/reponame where the image will be published (e.g. 'clearlydefined/service')
4+
# $1 - repo_name: the reponame where the image will be published (e.g. 'service')
55
# $2 - deploy_env: environment to deploy (i.e. dev | prod) - used as a label for the Docker image
66
# $3 - image-tag: the tag to use for the image (e.g. prod: v1.2.0, dev: v1.2.0+dev:1D3F567890)
77
#
88
# Outputs
9-
# image_name_with_tag: the full image name with tag (e.g. ghcr.io/clearlydefined/service:v1.2.0)
9+
# image_name_with_tag: the full image name with tag (e.g. service:v1.2.0, service-dev:v1.2.0+dev:1D3F567890)
1010

11-
repo="$1"
11+
repo_name="$1"
1212
deploy_env="$2"
1313
image_tag="$3"
1414

15-
image_base_name="ghcr.io/$repo" # e.g. ghcr.io/clearlydefined/service
15+
image_name_with_tag=""
1616
if [[ "$deploy_env" == 'prod' ]] ; then
17-
image_name_with_tag="$image_base_name:$image_tag"
17+
image_name_with_tag="$repo_name:$image_tag"
1818
elif [[ "$deploy_env" == 'dev' ]] ; then
19-
image_name_with_tag="$image_base_name-dev:$image_tag"
19+
image_name_with_tag="$repo_name-dev:$image_tag"
2020
else
2121
echo "ERROR: Invalid deploy environment: $deploy_env. Must be 'dev' or 'prod'"
2222
exit 1

tests/scripts/app-workflows/test-determine-image-name.bats

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
load 'test_helpers'
44

55
@test "deploy to dev environment" {
6-
run ./scripts/app-workflows/determine-image-name.sh test-org/test-repo dev test-tag
6+
run ./scripts/app-workflows/determine-image-name.sh test-repo dev test-tag
77
test_value 0 "$status"
8-
test_value "determine_image_name -> outputs -> image_name_with_tag: ghcr.io/test-org/test-repo-dev:test-tag" "${lines[0]}"
9-
test_value ghcr.io/test-org/test-repo-dev:test-tag "${lines[1]}"
8+
test_value "determine_image_name -> outputs -> image_name_with_tag: test-repo-dev:test-tag" "${lines[0]}"
9+
test_value test-repo-dev:test-tag "${lines[1]}"
1010
}
1111

1212
@test "deploy to prod environment" {
13-
run ./scripts/app-workflows/determine-image-name.sh test-org/test-repo prod test-tag
13+
run ./scripts/app-workflows/determine-image-name.sh test-repo prod test-tag
1414
test_value 0 "$status"
15-
test_value "determine_image_name -> outputs -> image_name_with_tag: ghcr.io/test-org/test-repo:test-tag" "${lines[0]}"
16-
test_value ghcr.io/test-org/test-repo:test-tag "${lines[1]}"
15+
test_value "determine_image_name -> outputs -> image_name_with_tag: test-repo:test-tag" "${lines[0]}"
16+
test_value test-repo:test-tag "${lines[1]}"
1717
}
1818

1919
@test "invalid deploy environment" {
20-
run ./scripts/app-workflows/determine-image-name.sh test-org/test-repo BAD_ENV test-tag
20+
run ./scripts/app-workflows/determine-image-name.sh test-repo BAD_ENV test-tag
2121
test_value 1 "$status"
2222
test_value "ERROR: Invalid deploy environment: BAD_ENV. Must be 'dev' or 'prod'" "${lines[0]}"
2323
}

0 commit comments

Comments
 (0)