Files
docker-push-action/action.yml
2025-11-27 16:45:50 +01:00

103 lines
3.1 KiB
YAML

name: 'Push Docker Tags'
description: 'Tag and push Docker images based on git context'
inputs:
registry:
description: 'Docker registry URL'
required: true
registry-username:
description: 'Docker registry username'
required: true
registry-password:
description: 'Docker registry password'
required: true
image-name:
description: 'Full image name (e.g., phundrak/my-app)'
required: true
local-image:
description: 'Local image to tag and push'
required: true
event-name:
description: 'GitHub/Gitea event name (pass github.event_name)'
required: true
ref:
description: 'Git ref (pass github.ref)'
required: true
ref-type:
description: 'Git ref type (pass github.ref_type)'
required: true
ref-name:
description: 'Git ref name (pass github.ref_name)'
required: true
pr-number:
description: 'Pull request number (pass github.event.pull_request.number)'
required: false
default: ''
outputs:
pushed-tags:
description: 'Space-separated list of tags that were pushed'
value: ${{ steps.push.outputs.tags }}
runs:
using: composite
steps:
- name: Log in to Docker Registry
shell: bash
run: |
echo "${{ inputs.registry-password }}" | docker login ${{ inputs.registry }} -u ${{ inputs.registry-username }} --password-stdin
- name: Determine tags and push images
id: push
shell: bash
run: |
set -euo pipefail
REGISTRY="${{ inputs.registry }}"
IMAGE_NAME="${{ inputs.image-name }}"
LOCAL_IMAGE="${{ inputs.local-image }}"
PUSHED_TAGS=""
echo "Event: ${{ inputs.event-name }}"
echo "Ref: ${{ inputs.ref }}"
echo "Ref type: ${{ inputs.ref-type }}"
push_tag() {
local tag="$1"
echo "Tagging and pushing: ${REGISTRY}/${IMAGE_NAME}:${tag}"
docker tag "${LOCAL_IMAGE}" "${REGISTRY}/${IMAGE_NAME}:${tag}"
docker push "${REGISTRY}/${IMAGE_NAME}:${tag}"
PUSHED_TAGS="${PUSHED_TAGS}${tag} "
}
if [[ "${{ inputs.event-name }}" == "push" && "${{ inputs.ref-type }}" == "tag" ]]; then
TAG_VERSION="${{ inputs.ref-name }}"
TAG_VERSION="${TAG_VERSION#v}"
push_tag "latest"
push_tag "${TAG_VERSION}"
elif [[ "${{ inputs.event-name }}" == "push" && "${{ inputs.ref }}" == "refs/heads/develop" ]]; then
push_tag "develop"
elif [[ "${{ inputs.event-name }}" == "pull_request" ]]; then
if [[ -z "${{ inputs.pr-number }}" ]]; then
echo "::error::PR number is required for pull_request events"
exit 1
fi
push_tag "pr${{ inputs.pr-number }}"
elif [[ "${{ inputs.event-name }}" == "push" && "${{ inputs.ref }}" == "refs/heads/main" ]]; then
push_tag "latest"
else
echo "::error::Unknown event or ref combination: event=${{ inputs.event-name }}, ref=${{ inputs.ref }}"
exit 1
fi
echo "tags=${PUSHED_TAGS}" >> "$GITHUB_OUTPUT"
- name: Log out from Docker Registry
if: always()
shell: bash
run: docker logout ${{ inputs.registry }}