2025-04-11 02:48:20 +00:00

92 lines
2.5 KiB
YAML

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: docker-registry
spec:
params:
- name: subdirectory
type: string
description: Subdirectory within the repo where the source code is located
default: ""
- name: imageName
description: Base image name with registry (e.g. registry.unbox-x.net/unbox-x-aisi-cron-app)
type: string
- name: tag
description: Version tag to apply to the image (e.g. v0.2.0)
type: string
- name: dockerfile
description: Path to Dockerfile
type: string
default: ./Dockerfile
- name: context
description: Build context path
type: string
default: .
workspaces:
- name: source
description: Source code workspace
- name: docker-auth
description: Docker registry credentials (username + password)
results:
- name: imageUrl
description: Final pushed image URL with tag (e.g. registry/app:v0.2.0)
steps:
- name: build-and-push
image: alpine:3.18
workingDir: /workspace/source
env:
- name: DOCKER_CONFIG
value: /tekton/home/.docker/
script: |
#!/bin/sh
set -e
if [ -n "$(params.subdirectory)" ]; then
cd "$(params.subdirectory)"
fi
cd "$(params.context)"
IMAGE="$(params.imageName):$(params.tag)"
echo "📦 Using image: $IMAGE"
echo -n "$IMAGE" > /tekton/results/imageUrl
echo "🔐 Loading Docker credentials..."
USERNAME=$(cat /workspace/docker-auth/username)
PASSWORD=$(cat /workspace/docker-auth/password)
REGISTRY=$(echo "$IMAGE" | cut -d/ -f1)
echo "📝 Writing Docker config for $REGISTRY"
mkdir -p /tekton/home/.docker
cat > /tekton/home/.docker/config.json <<EOF
{
"auths": {
"$REGISTRY": {
"username": "$USERNAME",
"password": "$PASSWORD",
"auth": "$(echo -n "$USERNAME:$PASSWORD" | base64)"
}
}
}
EOF
echo "📥 Installing Kaniko executor..."
wget -q -O /kaniko.tar.gz https://github.com/GoogleContainerTools/kaniko/releases/download/v1.17.0/executor-linux-amd64.tar.gz
tar -xzf /kaniko.tar.gz -C /usr/local/bin
chmod +x /usr/local/bin/executor
echo "🚀 Building and pushing image with Kaniko..."
executor \
--dockerfile=$(params.dockerfile) \
--context="$(params.context)" \
--destination="$IMAGE" \
--skip-tls-verify