Generate a GitHub Actions workflow that builds Docker, pushes to GHCR, and deploys to a server with one click.
Overview
Automatically generate a complete Docker CI/CD workflow: build image → push to GHCR → deploy to server via SSH. Includes an optimized Dockerfile (multi-stage build, non-root, health check) and a GitHub Actions workflow template.
When to Use
- User wants to containerize a project and deploy it
- User needs GitHub Actions to automatically build Docker images
- User mentions Docker, GHCR, container deployment, or CI/CD
- User inputs
/docker-build-deploy - User wants to set up continuous deployment pipeline
- User wants to push images to a container registry
- User wants to automate deployment to a remote server via SSH
- User wants to optimize existing Dockerfile with multi-stage builds
- User wants to add health checks to container deployment
When NOT to Use:
- User only wants to write a Dockerfile
- User deploys with Kubernetes (different workflow - consider
kubectlor Helm) - User doesn't use GitHub Actions (consider GitLab CI, CircleCI, etc.)
- User wants to deploy to AWS ECS/EKS, Google Cloud Run, or Azure Container Instances (different workflows)
Core Pattern
Step 1: Collect information
Interactively ask for:port(container port, default 3000),host_port(externally exposed port, default same as port),env_file(server env path, optional).
Smart detection: if there is package.json → Node.js (currently the only supported project type). If a Dockerfile already exists, skip generation.
Step 2: Generate Dockerfile (if needed)
Generate an optimized Node.js Dockerfile: multi-stage build, non-root user, health check. See template at templates/Dockerfile.nodejs。
Step 3: Generate Workflow
From templates/docker-deploy.yml generate the workflow, replacing the {{PORT}}(container port),{{HOST_PORT}}(external port) and {{ENV_FILE}} variables.
build-and-push job: Log in to GHCR → Buildx build → push (tags: latest + sha) → GHA cache
deploy job: SSH connection → pull image → stop old container → start new container (supports env file) → clean up old images
Step 4: Prompt to configure Secrets
Tell the user to configure in GitHub Settings → Secrets:DEPLOY_HOST、DEPLOY_USER、DEPLOY_SSH_KEY(server private key).
Security: use an SSH key instead of a password (password login carries brute-force risk and is stored in plaintext in CI). If the server only supports passwords, you can switch to
password: ${{ secrets.DEPLOY_PASSWORD }}, but it is not recommended.
Quick Reference
/docker-build-deploy
/docker-build-deploy --port 8080
/docker-build-deploy --port 8080 --env-file /opt/app/.env
| Parameter | Description | Default value |
|---|---|---|
--port |
Container app port | 3000 |
--host-port |
Server externally exposed port | Same as --port |
--env-file |
Server env file path | Empty |
Common Mistakes
| Error | Correct approach | Reason |
|---|---|---|
Use latest single tag |
Also tag latest + ${{ github.sha }} |
for easy rollback |
Do not set packages: write permissions |
Declare permissions: packages: write |
required for GHCR push |
| deploy does not check whether the container exists | First docker stop + docker rm |
to avoid port conflicts |
| Do not clean up old images | After deployment docker image prune -f |
disk space |
| Dockerfile runs as root | Add USER node or a non-root user |
Container security best practice |
| Secrets hardcoded in workflow | Use ${{ secrets.XXX }} reference |
Risk of key leakage |
| Docker BuildKit cache not set | Configure cache-from / cache-to using GitHub Actions cache |
A full build every time is too slow |
| Build failure rollback not handled | Verify health check after deployment | A failed deployment may release the wrong version |
| Use password SSH deployment | Use an SSH key (DEPLOY_SSH_KEY) |
High password brute-force risk |
| Platform declaration does not match the server architecture | Declare according to the server architecture linux/amd64 or linux/arm64; when building for multiple platforms, the linux/amd64,linux/arm64 |
target architecture mismatch causes runtime failures |