This reusable GitHub Actions workflow automates the process of building a Docker image and deploying it to a remote server. It is configurable via inputs for the Dockerfile path, image name, tag, remote host, and other options. The workflow performs the following actions:
- Determines the Tag: Computes the tag to use for the Docker image. 🏷️
- Builds the Docker Image: Builds and pushes the Docker image to DockerHub. 🔨
- Deploys to Remote Server: Securely deploys the image to a remote server using SSH. 🚀
| Input | Description | Required | Default |
|---|---|---|---|
| build-args | Docker build arguments (multiline format: KEY1=value1\nKEY2=value2). |
No | "" |
| dockerfile | Path to Dockerfile. | No | Dockerfile |
| image_name | Full image name (e.g. org/my-api). | Yes | - |
| image_tag | Optional tag override (defaults to pushed Git tag). | No | - |
| remote_host | SSH host (user@host). | Yes | - |
| remote_path | Remote path where compose files live. | Yes | - |
| runner_group | Runner group or label. | No | ubuntu-latest |
| Secret | Description | Required |
|---|---|---|
| dockerhub_username | DockerHub username for authentication. | Yes |
| dockerhub_password | DockerHub password for authentication. | Yes |
| ssh_private_key | SSH private key for remote deployment. | Yes |
| Output | Description | Value |
|---|---|---|
| tag | Tag effectively built/deployed | ${{ jobs.get-tag.outputs.tag }} |
- Purpose: Determines the tag to use for the Docker image.
- Runs On: The specified runner group (default:
ubuntu-latest). - Steps:
- Checkout the repository.
- Compute the tag (uses the provided tag or extracts it from the Git reference).
- Purpose: Builds and pushes the Docker image.
- Depends On:
get-tag - Uses: The docker-build workflow from the same repository.
- Inputs:
- Build arguments (optional)
- Dockerfile path
- Image name and tag
- Push configuration (set to true)
- Purpose: Deploys the Docker image to a remote server.
- Depends On:
buildandget-tag - Runs On: The specified runner group.
- Steps:
- Checkout the repository.
- Install SSH key for secure connection.
- Add remote host to known_hosts.
- Prepare .env file for Docker Compose.
- Copy compose files to the remote server.
- Pull and restart containers on the remote server.
-
Save the Workflow File This workflow is already saved as
.github/workflows/deploy-docker.ymlin the repository. 💾 -
Call the Reusable Workflow In another workflow file (e.g., triggered by a release), invoke this reusable workflow like so:
name: Deploy My Docker Application on: release: types: [published] jobs: deploy: uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/deploy-docker.yml@main with: build-args: | BUILD_VERSION=1.0.0 NODE_ENV=production dockerfile: 'path/to/Dockerfile' image_name: 'your-org/your-app' remote_host: 'user@your-server.com' remote_path: '/path/to/deployment' secrets: dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} dockerhub_password: ${{ secrets.DOCKERHUB_PASSWORD }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
-
Configure Secrets Ensure that the following secrets are added to your repository's settings:
DOCKERHUB_USERNAME: Your DockerHub usernameDOCKERHUB_PASSWORD: Your DockerHub password or access tokenSSH_PRIVATE_KEY: The SSH private key for connecting to the remote server
-
Docker Compose File:
- You must have a
docker-compose.ymlfile in the root of your repository. - This file should reference the environment variables
IMAGE_NAMEandIMAGE_TAG.
- You must have a
-
Remote Server:
- The remote server must have Docker and Docker Compose installed.
- The user specified in
remote_hostmust have permissions to run Docker commands.
-
Get Tag:
- Checks out the repository.
- Computes the tag to use (either from the input or from the Git reference).
-
Build Docker Image:
- Uses the docker-build workflow to build and push the Docker image.
- Configures the image with the computed tag.
-
Deploy to Remote Server:
- Sets up SSH authentication.
- Prepares the environment variables file.
- Copies the necessary files to the remote server.
- Pulls the latest image and restarts the containers using Docker Compose.