Skip to content

Commit

Permalink
Create reusable docker push action (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp94831 authored Aug 16, 2023
1 parent ecef042 commit 6019a8a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
30 changes: 11 additions & 19 deletions actions/docker-publish/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Publish Docker image"
description: "Publish Docker image from download artifact to any host"
description: "Publish Docker image from downloaded artifact to any host"

inputs:
docker-registry:
Expand Down Expand Up @@ -40,30 +40,22 @@ runs:
- name: Set up Docker Buildx
uses: docker/[email protected]

- name: Publish image
- name: Load image
run: |
if [[ $(ls -1 ${{ runner.temp }}/docker-build/*.tar 2>/dev/null | wc -l) != 1 ]]; then
>&2 echo "Error: A single image tar file is needed in the downloaded artifact. You can upload one before using this action: https://github.com/actions/upload-artifact."
exit 1
fi
fullImageName="${{ inputs.image-name }}"
if [[ -n "${{ inputs.image-namespace }}" ]]; then
fullImageName="${{ inputs.image-namespace }}/${fullImageName}"
fi
if [[ -n "${{ inputs.docker-registry }}" ]]; then
fullImageName="${{ inputs.docker-registry }}/${fullImageName}"
fi
docker load --input ${{ runner.temp }}/docker-build/*.tar
if [[ "$GITHUB_REF" =~ ^refs/tags/.* ]]; then
docker tag ${{ inputs.image-name }} ${fullImageName}:latest
docker tag ${{ inputs.image-name }} ${fullImageName}:${GITHUB_REF/refs\/tags\//}
docker push ${fullImageName}:latest
docker push ${fullImageName}:${GITHUB_REF/refs\/tags\//}
else
docker tag ${{ inputs.image-name }} ${fullImageName}:${{ inputs.image-tag }}
docker push ${fullImageName}:${{ inputs.image-tag }}
fi
shell: bash
working-directory: ${{ inputs.working-directory }}

- name: Publish image
uses: bakdata/ci-templates/actions/[email protected]
with:
docker-registry: ${{ inputs.docker-registry }}
image-namespace: ${{ inputs.image-namespace }}
image-name: ${{ inputs.image-name }}
image-tag: ${{ inputs.image-tag }}
working-directory: ${{ inputs.working-directory }}
32 changes: 32 additions & 0 deletions actions/docker-push/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# docker-push

This action publishes a Docker image to a Docker registry. When this action is used on a tag branch, the image is tagged with `latest` and the tag version of the branch (e.g. `1.2.3`). For all other branches, the tag `pipeline-${{ github.run_id }}-git-${GITHUB_SHA::8}` is used as an image tag.

## Prerequisites

Have a Docker image with name `${{ image-name }}` available in the Docker context.

## Input Parameters

| Name | Required | Default Value | Description |
| ----------------- | :------: | :------------------------------------------------: | ------------------------------------------- |
| docker-registry || "" | Host where the image should be pushed to |
| image-namespace || "" | Namespace of Docker image |
| image-name || github.event.repository.name | Name of Docker image |
| image-tag || pipeline-${{ github.run_id }}-git-${GITHUB_SHA::8} | Tag of Docker image |
| working-directory || "." | Working directory for your Docker artifacts |

## Usage

```yaml
steps:
- name: Push Docker image
uses: bakdata/ci-templates/actions/docker-push@main
with:
# publishing image registry.hub.docker.com/my-namespace/my-image:v1.1.0
docker-registry: "registry.hub.docker.com"
image-namespace: "my-namespace"
image-name: "my-image"
image-tag: "v1.1.0"
working-directory: "./tarball"
```
53 changes: 53 additions & 0 deletions actions/docker-push/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "Push Docker image"
description: "Push Docker image to any host"

inputs:
docker-registry:
description: "Host where the image should be pushed to."
required: false
default: ""
image-namespace:
description: "Namespace of Docker image."
required: false
default: ""
image-name:
description: "Name of Docker image."
required: false
default: "${{ github.event.repository.name }}"
image-tag:
description: "Tag of Docker image."
required: false
default: "pipeline-${{ github.run_id }}-git-${GITHUB_SHA::8}"
working-directory:
description: "Working directory for your Docker artifacts."
required: false
default: "."
runs:
using: "composite"
steps:
# setup-buildx action will create and boot a builder using by default the docker-container driver.
# This is not required but recommended using it to be able to build multi-platform images, export cache, etc.
- name: Set up Docker Buildx
uses: docker/[email protected]

- name: Publish image
run: |
fullImageName="${{ inputs.image-name }}"
if [[ -n "${{ inputs.image-namespace }}" ]]; then
fullImageName="${{ inputs.image-namespace }}/${fullImageName}"
fi
if [[ -n "${{ inputs.docker-registry }}" ]]; then
fullImageName="${{ inputs.docker-registry }}/${fullImageName}"
fi
if [[ "$GITHUB_REF" =~ ^refs/tags/.* ]]; then
docker tag ${{ inputs.image-name }} ${fullImageName}:latest
docker tag ${{ inputs.image-name }} ${fullImageName}:${GITHUB_REF/refs\/tags\//}
docker push ${fullImageName}:latest
docker push ${fullImageName}:${GITHUB_REF/refs\/tags\//}
else
docker tag ${{ inputs.image-name }} ${fullImageName}:${{ inputs.image-tag }}
docker push ${fullImageName}:${{ inputs.image-tag }}
fi
shell: bash
working-directory: ${{ inputs.working-directory }}

0 comments on commit 6019a8a

Please sign in to comment.