From 8e9640a4fcf20538ece0d50db46fae0fa404102e Mon Sep 17 00:00:00 2001 From: Radek Date: Mon, 22 Dec 2025 00:42:00 +0100 Subject: [PATCH] add ability to login to docker registry --- README.md | 22 +++++++++++++++++++++- action.yml | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c89ea4..919cd99 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,9 @@ Robustly deploy your Docker Compose files to Docker Swarm or regular Docker Comp | `build_context_path` | Local path to Docker build context | ❌ No | `build-context` | | `copy_env_file` | Whether to copy `.env` file to remote host | ❌ No | `false` | | `env_file_path` | Local path to `.env` file | ❌ No | `.env` | +| `docker_registry` | Docker registry URL (defaults to Docker Hub if empty) | ❌ No | - | +| `docker_username` | Docker registry username | ❌ No | - | +| `docker_password` | Docker registry password or token | ❌ No | - | --- @@ -80,6 +83,22 @@ This action supports two deployment modes: env_file_path: ./production.env ``` +### Deploying with Private Registry Authentication 🔐 + +```yaml +- name: Deploy with Private Registry 🔐 + uses: your-org/deploy-to-docker-action@main + with: + stack_name: my-private-stack + ssh_host: ${{ vars.SSH_HOST }} + ssh_user: ${{ secrets.SSH_USER }} + ssh_key: ${{ secrets.SSH_KEY }} + deploy_file: docker-compose.yml + docker_registry: ghcr.io + docker_username: ${{ github.actor }} + docker_password: ${{ secrets.GITHUB_TOKEN }} +``` + --- ## 🧑‍💻 How It Works @@ -88,7 +107,8 @@ This action performs the following steps: 1. 🔑 **Securely creates a temporary SSH key file** for authentication. 2. 📁 **Creates a secure temporary directory** on the remote host. -3. 📤 **Copies your processed Docker Compose file** to the remote host. +3. 🔐 **Optionally performs Docker login** on the remote host if credentials are provided. +4. 📤 **Copies your processed Docker Compose file** to the remote host. 4. 📂 **Optionally copies Docker build context** to the remote host. 5. 📄 **Optionally copies `.env` file** to the remote host. 6. 🔍 **Validates the Docker Compose file remotely** to ensure correctness. diff --git a/action.yml b/action.yml index 7a884be..44f89b9 100644 --- a/action.yml +++ b/action.yml @@ -43,6 +43,18 @@ inputs: description: 'Local path to .env file' required: false default: '.env' + docker_registry: + description: 'Docker registry URL (e.g., ghcr.io). Defaults to Docker Hub if empty.' + required: false + default: '' + docker_username: + description: 'Docker registry username' + required: false + default: '' + docker_password: + description: 'Docker registry password or token' + required: false + default: '' runs: @@ -61,6 +73,9 @@ runs: REMOTE_TEMP_DIR="${{ inputs.remote_temp_dir }}/$STACK_NAME" COPY_BUILD_CONTEXT="${{ inputs.copy_build_context }}" BUILD_CONTEXT_PATH="${{ inputs.build_context_path }}" + DOCKER_REGISTRY="${{ inputs.docker_registry }}" + DOCKER_USERNAME="${{ inputs.docker_username }}" + DOCKER_PASSWORD="${{ inputs.docker_password }}" echo "🚀 Starting deployment of '$STACK_NAME' to host '$SSH_HOST' using mode '$DEPLOY_MODE'" @@ -88,6 +103,20 @@ runs: "$SSH_USER@$SSH_HOST" \ "mkdir -p '$REMOTE_TEMP_DIR' && chmod 700 '$REMOTE_TEMP_DIR'" + # Docker Login (if credentials provided) + if [[ -n "$DOCKER_USERNAME" && -n "$DOCKER_PASSWORD" ]]; then + echo "🔐 DEBUG: Performing Docker login..." + LOGIN_CMD="echo '$DOCKER_PASSWORD' | docker login -u '$DOCKER_USERNAME' --password-stdin" + if [[ -n "$DOCKER_REGISTRY" ]]; then + LOGIN_CMD="$LOGIN_CMD $DOCKER_REGISTRY" + fi + + ssh -o StrictHostKeyChecking=no -i "$SSH_KEY_FILE" \ + "$SSH_USER@$SSH_HOST" \ + "$LOGIN_CMD" + echo "✅ DEBUG: Docker login successful" + fi + # Copy deployment file to remote host echo "📤 DEBUG: Copying deployment file '$DEPLOY_FILE' to remote host at '$REMOTE_TEMP_DIR/docker-compose.yml'" scp -o StrictHostKeyChecking=no -i "$SSH_KEY_FILE" \