Files
nix-builder-image/scripts/deploy.sh

107 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# Configuration (can be overridden by environment variables)
REGISTRY=${REGISTRY:-"ghcr.io"}
REGISTRY_USER=${REGISTRY_USER:-""}
REGISTRY_PASSWORD=${REGISTRY_PASSWORD:-""}
IMAGE_NAME=${IMAGE_NAME:-"nix-gitea-act-runner"}
REPOSITORY=${REPOSITORY:-"deepak/nix-gitea-act-runner"}
IMAGE_PATH="${REGISTRY}/${REPOSITORY}/${IMAGE_NAME}"
banner() {
echo "========================================================"
echo " $*"
echo "========================================================"
}
echo "List what we start with"
ls -alh
banner "Deploying Docker image to registry"
# Check if result file exists
if [[ ! -f "result" ]]; then
echo "Error: Docker archive 'result' not found. Build the image first."
exit 1
fi
# Login to registry if credentials are provided
if [[ -n ${REGISTRY_USER} && -n ${REGISTRY_PASSWORD} ]]; then
banner "Logging in to registry ${REGISTRY}"
echo "${REGISTRY_PASSWORD}" | skopeo login --username "${REGISTRY_USER}" --password-stdin "${REGISTRY}"
fi
# Inspect the image to ensure it's valid
banner "Inspecting Docker archive"
skopeo inspect docker-archive:result
# Initialize tags array
TAGS=()
# Determine tags based on git context
if [[ -n ${TAG:-} ]]; then
# For explicitly provided TAG (from workflow or command line)
VERSION="${TAG}"
if [[ ${VERSION} =~ ^v(.+)$ ]]; then
# Remove 'v' prefix if present
VERSION="${BASH_REMATCH[1]}"
fi
# Split version into components
IFS='.' read -r MAJOR MINOR _PATCH <<<"${VERSION}"
# Add version tags
TAGS+=("${VERSION}")
if [[ -n ${MAJOR} && -n ${MINOR} ]]; then
TAGS+=("${MAJOR}.${MINOR}")
fi
if [[ -n ${MAJOR} ]]; then
TAGS+=("${MAJOR}")
fi
banner "Deploying version: ${VERSION} (tags: ${TAGS[*]})"
else
# Check for git tag
if GIT_TAG=$(git describe --tags --exact-match 2>/dev/null); then
# We're on a tag, use it for versioning
VERSION="${GIT_TAG}"
if [[ ${VERSION} =~ ^v(.+)$ ]]; then
# Remove 'v' prefix if present
VERSION="${BASH_REMATCH[1]}"
fi
# Split version into components
IFS='.' read -r MAJOR MINOR _PATCH <<<"${VERSION}"
# Add version tags
TAGS+=("${VERSION}")
if [[ -n ${MAJOR} && -n ${MINOR} ]]; then
TAGS+=("${MAJOR}.${MINOR}")
fi
if [[ -n ${MAJOR} ]]; then
TAGS+=("${MAJOR}")
fi
banner "Deploying git tag version: ${VERSION} (tags: ${TAGS[*]})"
fi
fi
# Always include latest tag when on master branch
if [[ -z ${TAGS[*]} ]] || [[ ${BRANCH:-} == "master" ]] || [[ $(git rev-parse --abbrev-ref HEAD) == "master" ]]; then
TAGS+=("latest")
banner "Deploying with latest tag"
fi
# Copy the image to the registry with all relevant tags
for TAG in "${TAGS[@]}"; do
banner "Pushing image with tag: ${TAG}"
skopeo copy \
--insecure-policy \
--dest-tls-verify=true \
docker-archive:result \
"docker://${IMAGE_PATH}:${TAG}"
done
banner "Deployment complete"