commit 9b5294bb7e119bf53c6972901f91ba9ac1eb1a32 Author: mrrpnya Date: Thu Mar 6 13:39:23 2025 -0800 . diff --git a/.forgejo/workflows/test.yml b/.forgejo/workflows/test.yml new file mode 100644 index 0000000..a07fab2 --- /dev/null +++ b/.forgejo/workflows/test.yml @@ -0,0 +1,23 @@ +name: Test Deploy to OCI Registry + +on: + push: + branches: [ main ] + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Deploy to OCI Registry + uses: SevenOfAces/DeployImage@main + with: + registry-url: 'https://git.smgames.club' + registry-token: ${{ secrets.OCI_REGISTRY_TOKEN }} + architectures: 'amd64,arm64' + tags: 'latest' + name: 'auto' + dockerfile: './Dockerfile' + owner: 'SevenOfAces' diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d17c1ea --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +# Dockerfile +FROM alpine:3.16 + +# A dummy command to show the container is working. +CMD ["echo", "Hello, world!"] diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..06a5012 --- /dev/null +++ b/action.yml @@ -0,0 +1,133 @@ +name: 'Deploy to OCI Registry' +description: 'Build and push a Docker image to an OCI Registry' +inputs: + registry-url: + description: 'OCI Registry URL' + required: true + registry-token: + description: 'OCI Registry Token' + required: true + architectures: + description: 'Architectures to build for' + required: true + default: 'amd64,arm64' + tags: + description: 'Tags to apply to the image' + required: true + default: 'latest' + name: + description: 'Name of the image' + required: true + default: "auto" + dockerfile: + description: 'Path to the Dockerfile' + required: true + default: './Dockerfile' + owner: + description: 'OCI Registry owner' + required: true + default: ${{ github.repository_owner }} +env: + VALID_ARCHITECTURES: 'amd64 arm64' +runs: + using: "composite" + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + id: setup-buildx + uses: docker/setup-buildx-action@v3 + + - name: Validating Inputs + shell: bash + id: validate-inputs + env: + VALID_ARCHITECTURES: ${{ env.VALID_ARCHITECTURES }} + TAGS: ${{ inputs.tags }} + ARCHITECTURES: ${{ inputs.architectures }} + NAME: ${{ inputs.name }} + REGISTRY_URL: ${{ inputs.registry-url }} + REGISTRY_OWNER: ${{ inputs.owner }} + DOCKERFILE: ${{ inputs.dockerfile }} + run: | + bash ./scripts/validate_input.sh + + - name: Summary of Inputs + shell: bash + id: summary + env: + NAME: ${{ inputs.name }} + REGISTRY_URL: ${{ inputs.registry-url }} + REGISTRY_OWNER: ${{ inputs.owner }} + ARCHITECTURES: ${{ inputs.architectures }} + TAGS: ${{ inputs.tags }} + DOCKERFILE: ${{ inputs.dockerfile }} + run: | + echo "đŸ“Ļ Building image for: $NAME" + echo "🔗 Registry URL: $REGISTRY_URL" + echo "🔑 Registry Owner: $REGISTRY_OWNER" + echo "đŸ—ī¸ Architectures: $ARCHITECTURES" + echo "đŸˇī¸ Tags: $TAGS" + echo "📄 Dockerfile: $DOCKERFILE" + + - name: Authenticate with OCI Registry + id: login + shell: bash + env: + REGISTRY_URL: ${{ inputs.registry-url }} + REGISTRY_OWNER: ${{ inputs.owner }} + REGISTRY_TOKEN: ${{ inputs.registry-token }} + run: | + echo "🐋 Logging on to OCI Registry..." + echo "$REGISTRY_TOKEN" | docker login "$REGISTRY_URL" -u "$REGISTRY_OWNER" --password-stdin + if [ $? -ne 0 ]; then + echo "❌ Failed to authenticate with OCI Registry" + exit 1 + fi + echo "✅ Authenticated with OCI Registry" + + - name: Build and push OCI Image + id: build + shell: bash + env: + ARCHITECTURES: ${{ inputs.architectures }} + TAGS: ${{ inputs.tags }} + NAME: ${{ inputs.name }} + REGISTRY_URL: ${{ inputs.registry-url }} + DOCKERFILE: ${{ inputs.dockerfile }} + REPOSITORY_NAME: ${{ github.repository }} + run: | + # Determine image name if set to auto + if [ "$NAME" == "auto" ]; then + echo "🔍 Auto-detecting image name..." + NAME=$(echo "$REPOSITORY_NAME" | tr '[:upper:]' '[:lower:]') + echo "🔍 Auto-detected image name: $NAME" + fi + + # Process tags into an array and build tag arguments + IFS=',' read -r -a TAG_ARRAY <<< "$TAGS" + TAG_ARGS="" + for TAG in "${TAG_ARRAY[@]}"; do + TAG_ARGS+=" --tag $REGISTRY_URL/$NAME:$TAG" + done + + echo "🔨 Building Docker image: $NAME" + docker buildx build \ + --platform "$ARCHITECTURES" \ + $TAG_ARGS \ + --file "$DOCKERFILE" \ + --push \ + . + echo "🔨 Docker image built and pushed." + + # Verify each tag + for TAG in "${TAG_ARRAY[@]}"; do + if ! docker manifest inspect "$REGISTRY_URL/$NAME:$TAG" > /dev/null; then + echo "❌ Verification failed for $REGISTRY_URL/$NAME:$TAG" + exit 1 + fi + echo "✅ Verified image: $REGISTRY_URL/$NAME:$TAG" + done + + echo "🚀 Successfully built and pushed image: $REGISTRY_URL/$NAME" diff --git a/scripts/ensure_lowercase.sh b/scripts/ensure_lowercase.sh new file mode 100644 index 0000000..92aec5b --- /dev/null +++ b/scripts/ensure_lowercase.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Ensure specified environment variable is lowercase +# specify the environment variable to validate as an argument (or array of arguments) +# reserve first argument for the environment variable name +ENV_VAR_NAME=$1 + +for var in "${@:2}"; do + # get the value of the specified environment variable + value=$(eval echo \$$var) + # check if the value is not lowercase + if [[ $value =~ [A-Z] ]]; then + echo "❌ Invalid $ENV_VAR_NAME: $value" + echo "â„šī¸ $ENV_VAR_NAME must be lowercase" + exit 1 + else + echo "✅ $ENV_VAR_NAME is lowercase: $value" + fi +done diff --git a/scripts/ensure_no_spaces.sh b/scripts/ensure_no_spaces.sh new file mode 100644 index 0000000..b74fe4c --- /dev/null +++ b/scripts/ensure_no_spaces.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Ensure no spaces are in the input environment variable(s) +# specify the environment variable(s) to validate as an argument (or array of arguments) +# reserve first argument for the environment variable name + +ENV_VAR_NAME=$1 + +for var in "${@:2}"; do + # get the value of the specified environment variable + value=$(eval echo \$$var) + # check if the value has spaces + if [[ $value =~ [[:space:]] ]]; then + echo "❌ Invalid $ENV_VAR_NAME: $value" + echo "â„šī¸ $ENV_VAR_NAME must not have spaces" + exit 1 + else + echo "✅ $ENV_VAR_NAME is valid: $value" + fi +done \ No newline at end of file diff --git a/scripts/validate_input.sh b/scripts/validate_input.sh new file mode 100644 index 0000000..6af185f --- /dev/null +++ b/scripts/validate_input.sh @@ -0,0 +1,127 @@ +#!/bin/bash +# Script: validate_input.sh +# Description: Validates prerequisites and input parameters for building and pushing a Docker image. +# This script ensures that Docker, Docker Buildx, and input variables (architectures, tags, image name, registry URL) +# meet the required criteria. If any check fails, the script provides informative error messages and exits. + +# Exit immediately if a command exits with a non-zero status. +set -e + +echo "============================================" +echo "Starting Validation Script" +echo "============================================" + +# ---------------------------- Docker Installation --------------------------- # +echo "🔍 Checking Docker installation..." +if docker --version >/dev/null 2>&1; then + DOCKER_VERSION=$(docker --version) + echo "✅ Docker is installed: $DOCKER_VERSION" +else + echo "❌ Docker is not installed." + echo "â„šī¸ Please install Docker from: https://docs.docker.com/get-docker/" + exit 1 +fi + +# ---------------------------- Buildx Installation --------------------------- # +echo "🔍 Checking Docker Buildx installation..." +if docker buildx version >/dev/null 2>&1; then + BUILDX_VERSION=$(docker buildx version) + echo "✅ Docker Buildx is installed: $BUILDX_VERSION" +else + echo "❌ Docker Buildx is not installed." + echo "â„šī¸ Please install Docker Buildx. See: https://docs.docker.com/buildx/working-with-buildx/" + exit 1 +fi + +# ---------------------------- Validate Architectures --------------------------- # +echo "🔍 Validating architectures..." +for arch in $ARCHITECTURES; do + echo "→ Checking architecture: $arch" + if [[ $VALID_ARCHITECTURES != *"$arch"* ]]; then + echo "❌ Invalid architecture detected: $arch" + echo "â„šī¸ Allowed architectures are: $VALID_ARCHITECTURES" + exit 1 + fi +done +echo "✅ All specified architectures are valid." + +# ---------------------------- Validate Tags --------------------------- # +echo "🔍 Validating tags..." + +# Ensure tags are lowercase. +if sh ./ensure_lowercase.sh "Tag" $TAGS; then + echo "✅ All tags are lowercase." +else + echo "❌ One or more tags are not lowercase. Please update the tags." + exit 1 +fi + +# Ensure tags do not contain spaces. +if sh ./ensure_no_spaces.sh "Tag" $TAGS; then + echo "✅ Tags do not contain spaces." +else + echo "❌ One or more tags contain spaces. Please update the tags." + exit 1 +fi + +# ---------------------------- Validate Image Name --------------------------- # +if [ "$NAME" != "auto" ]; then + echo "🔍 Validating image name..." + # Ensure image name is lowercase. + if sh ./ensure_lowercase.sh "Name" $NAME; then + echo "✅ Image name is lowercase." + else + echo "❌ Image name is not lowercase. Please update the name." + exit 1 + fi + + # Ensure image name does not contain spaces. + if sh ./ensure_no_spaces.sh "Name" $NAME; then + echo "✅ Image name does not contain spaces." + else + echo "❌ Image name contains spaces. Please update the name." + exit 1 + fi +else + echo "â„šī¸ Image name is set to auto-detect." +fi + +# ---------------------------- Validate Registry URL --------------------------- # +echo "🔍 Validating Registry URL..." +# Ensure Registry URL does not contain spaces. +if sh ./ensure_no_spaces.sh "Registry URL" $REGISTRY_URL; then + echo "✅ Registry URL does not contain spaces." +else + echo "❌ Registry URL contains spaces. Please update the URL." + exit 1 +fi + +# Ensure Registry URL is lowercase. +if sh ./ensure_lowercase.sh "Registry URL" $REGISTRY_URL; then + echo "✅ Registry URL is lowercase." +else + echo "❌ Registry URL is not lowercase. Please update the URL." + exit 1 +fi + +# Ensure Registry URL starts with "https://" +if [[ $REGISTRY_URL != https://* ]]; then + echo "❌ Invalid Registry URL: $REGISTRY_URL" + echo "â„šī¸ Registry URL must start with 'https://'" + exit 1 +fi +echo "✅ Registry URL starts with 'https://'" + +# Check if Registry URL is reachable. +echo "🔍 Checking if Registry URL is reachable..." +if curl -s --head "$REGISTRY_URL" | head -n 1 | grep "HTTP/[12]\.[0-9] [23].." >/dev/null; then + echo "✅ Registry URL is reachable: $REGISTRY_URL" +else + echo "❌ Registry URL is not reachable: $REGISTRY_URL" + echo "â„šī¸ Please verify the URL and check your network connection." + exit 1 +fi + +echo "============================================" +echo "All validations passed successfully!" +echo "============================================"