Skip to content

Commit

Permalink
feat: docker push extra tags (#136)
Browse files Browse the repository at this point in the history
* feat: adds docker push input parameter for extra docker tags

---------

Co-authored-by: hw-knowit <hw-knowit@users.noreply.github.com>
Co-authored-by: Bjørn Vestli <bjorn.vestli@entur.org>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent db90eb6 commit e95f1a2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 21 deletions.
54 changes: 51 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ jobs:
expected: "ci-test-tag"
actual: ${{ needs.test-push-gcr-ok-with-tag.outputs.image_tag }}

test-push-gcr-ok-with-multiple-tags:
needs: test-push-gcr-ok
uses: ./.github/workflows/push.yml
with:
context: .
dockerfile: fixture/Dockerfile.artifact
image_tag: ci-test-tag
extra_image_tags: ci-test-tag-2,ci-test-tag-3
git_tag: false

assert-multiple-tags-gcr-outputs-ok:
needs: test-push-gcr-ok-with-multiple-tags
runs-on: ubuntu-24.04
steps:
- uses: nick-fields/assert-action@v2
with:
expected: "ci-test-tag,ci-test-tag-2,ci-test-tag-3"
actual: ${{ needs.test-push-gcr-ok-with-multiple-tags.outputs.all_image_tags }}

test-build-ok-with-custom-image-name:
uses: ./.github/workflows/build.yml
with:
Expand Down Expand Up @@ -147,10 +166,33 @@ jobs:
expected: "ci-test-az-tag"
actual: ${{ needs.test-push-acr-ok-with-tag.outputs.image_tag }}

test-push-acr-ok-with-multiple-tags:
needs: test-build-ok-with-artifact
uses: ./.github/workflows/push.yml
with:
cloud_provider: az
context: .
dockerfile: fixture/Dockerfile.artifact
image_tag: ci-test-az-tag
extra_image_tags: ci-test-az-tag-2,ci-test-az-tag-3
git_tag: false

assert-multiple-tags-acr-outputs-ok:
runs-on: ubuntu-24.04
needs: test-push-acr-ok-with-multiple-tags
steps:
- uses: nick-fields/assert-action@v2
with:
expected: "ci-test-az-tag,ci-test-az-tag-2,ci-test-az-tag-3"
actual: ${{ needs.test-push-acr-ok-with-multiple-tags.outputs.all_image_tags }}

update-lint-doc:
needs:
[
test-push-acr-ok-with-tag,
assert-tag-gcr-outputs-ok,
assert-multiple-tags-gcr-outputs-ok,
assert-tag-acr-outputs-ok,
assert-multiple-tags-acr-outputs-ok,
assert-build-ok-with-custom-image-name-outputs,
]
uses: entur/gha-meta/.github/workflows/auto-doc.yml@v1
Expand All @@ -161,7 +203,10 @@ jobs:
update-build-doc:
needs:
[
test-push-acr-ok-with-tag,
assert-tag-gcr-outputs-ok,
assert-multiple-tags-gcr-outputs-ok,
assert-tag-acr-outputs-ok,
assert-multiple-tags-acr-outputs-ok,
assert-build-ok-with-custom-image-name-outputs,
update-lint-doc,
]
Expand All @@ -173,7 +218,10 @@ jobs:
update-push-doc:
needs:
[
test-push-acr-ok-with-tag,
assert-tag-gcr-outputs-ok,
assert-multiple-tags-gcr-outputs-ok,
assert-tag-acr-outputs-ok,
assert-multiple-tags-acr-outputs-ok,
assert-build-ok-with-custom-image-name-outputs,
update-build-doc,
]
Expand Down
25 changes: 21 additions & 4 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ on:
description: "Docker tag. If not set, it will be set to 'branch_name.date-SHA'"
type: string
default: "image_tag"
extra_image_tags:
description: "Extra Docker tags (comma separated) to apply and push - Requires Harness project-level setting 'Execute Triggers With All Collected Artifacts or Manifests'"
type: string
cloud_provider:
description: "Which cloud service provider to use - Google Cloud: 'gcp' or Azure: 'az'"
type: string
Expand All @@ -38,11 +41,14 @@ on:
value: ${{ jobs.push.outputs.image_tag }}
image_and_tag:
value: "${{ jobs.push.outputs.image_name }}:${{ jobs.push.outputs.image_tag }}"
all_image_tags:
value: ${{ jobs.push.outputs.all_image_tags }}
env:
GHA_DOCKER_PUSH_IMAGE_NAME: ${{ inputs.image_name }}
GHA_DOCKER_PUSH_CONTEXT: ${{ inputs.context }}
GHA_DOCKER_PUSH_DOCKERFILE: ${{ inputs.dockerfile }}
GHA_DOCKER_PUSH_IMAGE_TAG: ${{ inputs.image_tag }}
GHA_DOCKER_PUSH_EXTRA_IMAGE_TAGS: ${{ inputs.extra_image_tags }}
GHA_DOCKER_PUSH_CLOUD_PROVIDER: ${{ inputs.cloud_provider }}
GHA_DOCKER_PUSH_DATE: ""
GHA_DOCKER_PUSH_BRANCH_NAME: ""
Expand All @@ -61,6 +67,7 @@ jobs:
outputs:
image_name: ${{ env.GHA_DOCKER_PUSH_IMAGE_NAME }}
image_tag: ${{ env.GHA_DOCKER_PUSH_IMAGE_TAG }}
all_image_tags: ${{ env.GHA_ALL_IMAGE_TAGS }}
steps:
- name: Set variables
id: set-env
Expand Down Expand Up @@ -151,12 +158,22 @@ jobs:
azure_tenant_id: ${{ vars.CI_AZURE_TENANT_ID }}
azure_subscription_id: ${{ vars.CI_AZURE_SUBSCRIPTION_ID }}

- name: Docker push to registry
id: docker-push-single-tag
- name: Docker tag and push to registry
id: docker-push-tags
shell: bash
run: |
docker tag ${GHA_DOCKER_PUSH_IMAGE_NAME}:${GITHUB_SHA} ${GHA_DOCKER_PUSH_IMAGE}:${GHA_DOCKER_PUSH_IMAGE_TAG}
docker push ${GHA_DOCKER_PUSH_IMAGE}:${GHA_DOCKER_PUSH_IMAGE_TAG}
local_tag="${GHA_DOCKER_PUSH_IMAGE_NAME}:${GITHUB_SHA}"
tags="${GHA_DOCKER_PUSH_IMAGE_TAG},${GHA_DOCKER_PUSH_EXTRA_IMAGE_TAGS}"
echo "GHA_ALL_IMAGE_TAGS=${tags}" >> $GITHUB_ENV
echo "$tags" | while read -d ',' -r tag || [[ -n $tag ]]; do
remote_tag="${GHA_DOCKER_PUSH_IMAGE}:${tag}"
echo "Tagging and pushing '$local_tag' as '$remote_tag'"
docker tag "$local_tag" "$remote_tag"
docker push "$remote_tag"
done
# Tag the git commit with the image tag (used by Harness CD to deploy)
- if: inputs.git_tag
Expand Down
30 changes: 16 additions & 14 deletions README-push.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ jobs:
<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->
| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|-------------------------------------------------------------------------------|---------|----------|----------------|------------------------------------------------------------------------------------------------------------------------------------|
| <a name="input_cloud_provider"></a>[cloud_provider](#input_cloud_provider) | string | false | `"gcp"` | Which cloud service provider to <br>use - Google Cloud: 'gcp' <br>or Azure: 'az' |
| <a name="input_context"></a>[context](#input_context) | string | false | `"."` | Build context, default root of <br>repository |
| <a name="input_dockerfile"></a>[dockerfile](#input_dockerfile) | string | false | `"Dockerfile"` | Dockerfile name to use for <br>build |
| <a name="input_git_tag"></a>[git_tag](#input_git_tag) | boolean | false | `true` | Tag the repository default branch <br>with the image_tag? Used by <br>Harness CD to deploy the <br>exact same helm/terraform code |
| <a name="input_image_name"></a>[image_name](#input_image_name) | string | false | `"repo_name"` | GitHub artifact with Docker image |
| <a name="input_image_tag"></a>[image_tag](#input_image_tag) | string | false | `"image_tag"` | Docker tag. If not set, <br>it will be set to <br>'branch_name.date-SHA' |
| <a name="input_timeout_minutes"></a>[timeout_minutes](#input_timeout_minutes) | number | false | `10` | Timeout in minutes |
| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|----------------------------------------------------------------------------------|---------|----------|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <a name="input_cloud_provider"></a>[cloud_provider](#input_cloud_provider) | string | false | `"gcp"` | Which cloud service provider to <br>use - Google Cloud: 'gcp' <br>or Azure: 'az' |
| <a name="input_context"></a>[context](#input_context) | string | false | `"."` | Build context, default root of <br>repository |
| <a name="input_dockerfile"></a>[dockerfile](#input_dockerfile) | string | false | `"Dockerfile"` | Dockerfile name to use for <br>build |
| <a name="input_extra_image_tags"></a>[extra_image_tags](#input_extra_image_tags) | string | false | | Extra Docker tags (comma separated) to <br>apply and push - Requires <br>Harness project-level setting 'Execute Triggers <br>With All Collected Artifacts or <br>Manifests' |
| <a name="input_git_tag"></a>[git_tag](#input_git_tag) | boolean | false | `true` | Tag the repository default branch <br>with the image_tag? Used by <br>Harness CD to deploy the <br>exact same helm/terraform code |
| <a name="input_image_name"></a>[image_name](#input_image_name) | string | false | `"repo_name"` | GitHub artifact with Docker image |
| <a name="input_image_tag"></a>[image_tag](#input_image_tag) | string | false | `"image_tag"` | Docker tag. If not set, <br>it will be set to <br>'branch_name.date-SHA' |
| <a name="input_timeout_minutes"></a>[timeout_minutes](#input_timeout_minutes) | number | false | `10` | Timeout in minutes |

<!-- AUTO-DOC-INPUT:END -->

## Outputs

<!-- AUTO-DOC-OUTPUT:START - Do not remove or modify this section -->

| OUTPUT | VALUE | DESCRIPTION |
|---------------------------------------------------------------------------|----------------------------------------------------------------------------|-------------|
| <a name="output_image_and_tag"></a>[image_and_tag](#output_image_and_tag) | `"${{ jobs.push.outputs.image_name }}:${{ jobs.push.outputs.image_tag }}"` | |
| <a name="output_image_name"></a>[image_name](#output_image_name) | `"${{ jobs.push.outputs.image_name }}"` | |
| <a name="output_image_tag"></a>[image_tag](#output_image_tag) | `"${{ jobs.push.outputs.image_tag }}"` | |
| OUTPUT | VALUE | DESCRIPTION |
|------------------------------------------------------------------------------|----------------------------------------------------------------------------|-------------|
| <a name="output_all_image_tags"></a>[all_image_tags](#output_all_image_tags) | `"${{ jobs.push.outputs.all_image_tags }}"` | |
| <a name="output_image_and_tag"></a>[image_and_tag](#output_image_and_tag) | `"${{ jobs.push.outputs.image_name }}:${{ jobs.push.outputs.image_tag }}"` | |
| <a name="output_image_name"></a>[image_name](#output_image_name) | `"${{ jobs.push.outputs.image_name }}"` | |
| <a name="output_image_tag"></a>[image_tag](#output_image_tag) | `"${{ jobs.push.outputs.image_tag }}"` | |

<!-- AUTO-DOC-OUTPUT:END -->

0 comments on commit e95f1a2

Please sign in to comment.