|
| 1 | +# Docker Build & Deploy - Reusable Workflow Documentation 🚀 |
| 2 | + |
| 3 | +## Overview 🌟 |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +- **Determines the Tag**: Computes the tag to use for the Docker image. 🏷️ |
| 8 | +- **Builds the Docker Image**: Builds and pushes the Docker image to DockerHub. 🔨 |
| 9 | +- **Deploys to Remote Server**: Securely deploys the image to a remote server using SSH. 🚀 |
| 10 | + |
| 11 | +## Workflow Inputs 🛠️ |
| 12 | + |
| 13 | +| **Input** | **Description** | **Required** | **Default** | |
| 14 | +|------------------|-----------------------------------------------------|--------------|-----------------| |
| 15 | +| **dockerfile** | Path to Dockerfile. | No | `Dockerfile` | |
| 16 | +| **image_name** | Full image name (e.g. org/my-api). | Yes | - | |
| 17 | +| **image_tag** | Optional tag override (defaults to pushed Git tag). | No | - | |
| 18 | +| **remote_host** | SSH host (user@host). | Yes | - | |
| 19 | +| **remote_path** | Remote path where compose files live. | Yes | - | |
| 20 | +| **runner_group** | Runner group or label. | No | `ubuntu-latest` | |
| 21 | + |
| 22 | +### Secrets 🔐 |
| 23 | + |
| 24 | +| **Secret** | **Description** | **Required** | |
| 25 | +|------------------------|----------------------------------------|--------------| |
| 26 | +| **dockerhub_username** | DockerHub username for authentication. | Yes | |
| 27 | +| **dockerhub_password** | DockerHub password for authentication. | Yes | |
| 28 | +| **ssh_private_key** | SSH private key for remote deployment. | Yes | |
| 29 | + |
| 30 | +### Outputs 📤 |
| 31 | + |
| 32 | +| **Output** | **Description** | **Value** | |
| 33 | +|------------|--------------------------------|-----------------------------------| |
| 34 | +| **tag** | Tag effectively built/deployed | `${{ jobs.get-tag.outputs.tag }}` | |
| 35 | + |
| 36 | +## Jobs and Steps ⚙️ |
| 37 | + |
| 38 | +### Job: `get-tag` |
| 39 | +- **Purpose**: Determines the tag to use for the Docker image. |
| 40 | +- **Runs On**: The specified runner group (default: `ubuntu-latest`). |
| 41 | +- **Steps**: |
| 42 | + 1. Checkout the repository. |
| 43 | + 2. Compute the tag (uses the provided tag or extracts it from the Git reference). |
| 44 | + |
| 45 | +### Job: `build` |
| 46 | +- **Purpose**: Builds and pushes the Docker image. |
| 47 | +- **Depends On**: `get-tag` |
| 48 | +- **Uses**: The docker-build workflow from the same repository. |
| 49 | +- **Inputs**: |
| 50 | + - Dockerfile path |
| 51 | + - Image name and tag |
| 52 | + - Push configuration (set to true) |
| 53 | + |
| 54 | +### Job: `deploy` |
| 55 | +- **Purpose**: Deploys the Docker image to a remote server. |
| 56 | +- **Depends On**: `build` and `get-tag` |
| 57 | +- **Runs On**: The specified runner group. |
| 58 | +- **Steps**: |
| 59 | + 1. Checkout the repository. |
| 60 | + 2. Install SSH key for secure connection. |
| 61 | + 3. Add remote host to known_hosts. |
| 62 | + 4. Prepare .env file for Docker Compose. |
| 63 | + 5. Copy compose files to the remote server. |
| 64 | + 6. Pull and restart containers on the remote server. |
| 65 | + |
| 66 | +## How to Use This Reusable Workflow 🔄 |
| 67 | + |
| 68 | +1. **Save the Workflow File** |
| 69 | + This workflow is already saved as `.github/workflows/deploy-docker.yml` in the repository. 💾 |
| 70 | + |
| 71 | +2. **Call the Reusable Workflow** |
| 72 | + In another workflow file (e.g., triggered by a release), invoke this reusable workflow like so: |
| 73 | + |
| 74 | + ```yaml |
| 75 | + name: Deploy My Docker Application |
| 76 | + on: |
| 77 | + release: |
| 78 | + types: [published] |
| 79 | + |
| 80 | + jobs: |
| 81 | + deploy: |
| 82 | + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/deploy-docker.yml@main |
| 83 | + with: |
| 84 | + dockerfile: 'path/to/Dockerfile' |
| 85 | + image_name: 'your-org/your-app' |
| 86 | + |
| 87 | + remote_path: '/path/to/deployment' |
| 88 | + secrets: |
| 89 | + dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 90 | + dockerhub_password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 91 | + ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 92 | + ``` |
| 93 | +
|
| 94 | +3. **Configure Secrets** |
| 95 | + Ensure that the following secrets are added to your repository's settings: |
| 96 | + - `DOCKERHUB_USERNAME`: Your DockerHub username |
| 97 | + - `DOCKERHUB_PASSWORD`: Your DockerHub password or access token |
| 98 | + - `SSH_PRIVATE_KEY`: The SSH private key for connecting to the remote server |
| 99 | + |
| 100 | +## Prerequisites 📋 |
| 101 | + |
| 102 | +1. **Docker Compose File**: |
| 103 | + - You must have a `docker-compose.yml` file in the root of your repository. |
| 104 | + - This file should reference the environment variables `IMAGE_NAME` and `IMAGE_TAG`. |
| 105 | + |
| 106 | +2. **Remote Server**: |
| 107 | + - The remote server must have Docker and Docker Compose installed. |
| 108 | + - The user specified in `remote_host` must have permissions to run Docker commands. |
| 109 | + |
| 110 | +## Workflow Steps in Detail 🔍 |
| 111 | + |
| 112 | +1. **Get Tag**: |
| 113 | + - Checks out the repository. |
| 114 | + - Computes the tag to use (either from the input or from the Git reference). |
| 115 | + |
| 116 | +2. **Build Docker Image**: |
| 117 | + - Uses the docker-build workflow to build and push the Docker image. |
| 118 | + - Configures the image with the computed tag. |
| 119 | + |
| 120 | +3. **Deploy to Remote Server**: |
| 121 | + - Sets up SSH authentication. |
| 122 | + - Prepares the environment variables file. |
| 123 | + - Copies the necessary files to the remote server. |
| 124 | + - Pulls the latest image and restarts the containers using Docker Compose. |
0 commit comments