commit ae3daac41559ffcdf4c10fdadca6220a30f660fd Author: Lucien Cartier-Tilet Date: Thu Nov 27 16:45:41 2025 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa468d8 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +--- +gitea: none +include_toc: true +--- + +# Docker Push Action + +A composite action to tag and push Docker images based on git context. + +## Tag Logic + +| Event | Condition | Tags Pushed | +|----------------|------------------|--------------------------------------------| +| `push` | Tag `v*.*.*` | `latest`, `` (without `v` prefix) | +| `push` | Branch `main` | `latest` | +| `push` | Branch `develop` | `develop` | +| `pull_request` | Any | `pr` | + +## Usage +```yaml +- name: Load Docker image + run: docker load < my-image.tar.gz + +- name: Push Docker tags + uses: https://labs.phundrak.com/phundrak/docker-push-action@v1 + with: + registry: labs.phundrak.com + registry-username: ${{ secrets.DOCKER_USERNAME }} + registry-password: ${{ secrets.DOCKER_PASSWORD }} + image-name: phundrak/my-app + local-image: my-app:latest + event-name: ${{ github.event_name }} + ref: ${{ github.ref }} + ref-type: ${{ github.ref_type }} + ref-name: ${{ github.ref_name }} + pr-number: ${{ github.event.pull_request.number }} +``` + +## Inputs + +| Input | Required | Description | +|---------------------|----------|------------------------------------------------| +| `registry` | Yes | Docker registry URL | +| `registry-username` | Yes | Docker registry username | +| `registry-password` | Yes | Docker registry password | +| `image-name` | Yes | Full image name (e.g., `phundrak/my-app`) | +| `local-image` | Yes | Local image to tag and push | +| `event-name` | Yes | Pass `${{ github.event_name }}` | +| `ref` | Yes | Pass `${{ github.ref }}` | +| `ref-type` | Yes | Pass `${{ github.ref_type }}` | +| `ref-name` | Yes | Pass `${{ github.ref_name }}` | +| `pr-number` | No | Pass `${{ github.event.pull_request.number }}` | + +## Outputs + +| Output | Description | +|---------------|-----------------------------------------------| +| `pushed-tags` | Space-separated list of tags that were pushed | diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..01ca9df --- /dev/null +++ b/action.yml @@ -0,0 +1,102 @@ +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 }}