add ability to login to docker registry

This commit is contained in:
2025-12-22 00:42:00 +01:00
parent 90f83b775e
commit 8e9640a4fc
2 changed files with 50 additions and 1 deletions

View File

@@ -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` | | `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` | | `copy_env_file` | Whether to copy `.env` file to remote host | ❌ No | `false` |
| `env_file_path` | Local path to `.env` file | ❌ No | `.env` | | `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 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 ## 🧑‍💻 How It Works
@@ -88,7 +107,8 @@ This action performs the following steps:
1. 🔑 **Securely creates a temporary SSH key file** for authentication. 1. 🔑 **Securely creates a temporary SSH key file** for authentication.
2. 📁 **Creates a secure temporary directory** on the remote host. 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. 4. 📂 **Optionally copies Docker build context** to the remote host.
5. 📄 **Optionally copies `.env` file** to the remote host. 5. 📄 **Optionally copies `.env` file** to the remote host.
6. 🔍 **Validates the Docker Compose file remotely** to ensure correctness. 6. 🔍 **Validates the Docker Compose file remotely** to ensure correctness.

View File

@@ -43,6 +43,18 @@ inputs:
description: 'Local path to .env file' description: 'Local path to .env file'
required: false required: false
default: '.env' 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: runs:
@@ -61,6 +73,9 @@ runs:
REMOTE_TEMP_DIR="${{ inputs.remote_temp_dir }}/$STACK_NAME" REMOTE_TEMP_DIR="${{ inputs.remote_temp_dir }}/$STACK_NAME"
COPY_BUILD_CONTEXT="${{ inputs.copy_build_context }}" COPY_BUILD_CONTEXT="${{ inputs.copy_build_context }}"
BUILD_CONTEXT_PATH="${{ inputs.build_context_path }}" 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'" echo "🚀 Starting deployment of '$STACK_NAME' to host '$SSH_HOST' using mode '$DEPLOY_MODE'"
@@ -88,6 +103,20 @@ runs:
"$SSH_USER@$SSH_HOST" \ "$SSH_USER@$SSH_HOST" \
"mkdir -p '$REMOTE_TEMP_DIR' && chmod 700 '$REMOTE_TEMP_DIR'" "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 # Copy deployment file to remote host
echo "📤 DEBUG: Copying deployment file '$DEPLOY_FILE' to remote host at '$REMOTE_TEMP_DIR/docker-compose.yml'" 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" \ scp -o StrictHostKeyChecking=no -i "$SSH_KEY_FILE" \