Refactor Docker login process in action.yml to enhance image pulling logic. Added handling for successful and failed pulls with credentials and anonymous access. Mask sensitive values during login.

This commit is contained in:
Tommy Malmqvist 2025-09-18 07:31:47 +02:00
parent f20f287d5b
commit 9ce5c8afee

View File

@ -26,26 +26,55 @@ inputs:
runs: runs:
using: "composite" using: "composite"
steps: steps:
- id: login - id: pull-with-credentials
name: Login to registry (if credentials provided) name: Pull image using provided credentials
if: ${{ inputs.registry_username && inputs.registry_password && inputs.registry }} if: ${{ inputs.registry_username && inputs.registry_password && inputs.registry }}
shell: bash shell: bash
env: env:
IMAGE_INPUT: ${{ inputs.image }}
REGISTRY: ${{ inputs.registry }} REGISTRY: ${{ inputs.registry }}
REG_USER: ${{ inputs.registry_username }} REG_USER: ${{ inputs.registry_username }}
REG_PASS: ${{ inputs.registry_password }} REG_PASS: ${{ inputs.registry_password }}
run: | run: |
set -euo pipefail set -euo pipefail
if [ -z "$REGISTRY" ] || [ -z "$REG_USER" ] || [ -z "$REG_PASS" ]; then IMAGE="$IMAGE_INPUT"
echo "Missing registry or credentials; skipping login" if [ -n "$REGISTRY" ]; then
exit 0 REG="${REGISTRY%/}"
IMAGE="$REG/$IMAGE"
fi fi
# Mask sensitive values echo "Using image: $IMAGE"
echo "Credentials provided; attempting docker login to $REGISTRY"
if [ -n "$REG_PASS" ]; then if [ -n "$REG_PASS" ]; then
echo "::add-mask::$REG_PASS" echo "::add-mask::$REG_PASS"
fi fi
echo "Logging into registry: $REGISTRY"
echo "$REG_PASS" | docker login "$REGISTRY" --username "$REG_USER" --password-stdin echo "$REG_PASS" | docker login "$REGISTRY" --username "$REG_USER" --password-stdin
if docker pull "$IMAGE" >/dev/null 2>&1; then
echo "Image pulled successfully after login."
else
echo "Failed to pull image after login; proceeding to run (docker run may fail)."
fi
- id: pull-anonymous
name: Pull image anonymously
if: ${{ !(inputs.registry_username && inputs.registry_password && inputs.registry) }}
shell: bash
env:
IMAGE_INPUT: ${{ inputs.image }}
REGISTRY: ${{ inputs.registry }}
run: |
set -euo pipefail
IMAGE="$IMAGE_INPUT"
if [ -n "$REGISTRY" ]; then
REG="${REGISTRY%/}"
IMAGE="$REG/$IMAGE"
fi
echo "Using image: $IMAGE"
echo "No credentials provided (or registry not set); attempting anonymous pull"
if docker pull "$IMAGE" >/dev/null 2>&1; then
echo "Anonymous pull succeeded."
else
echo "Anonymous pull failed; proceeding to run (docker run may fail if auth required)."
fi
- id: run - id: run
name: Run yq container name: Run yq container