Skip to content

Commit 033e539

Browse files
feat(docker): add GitHub Actions workflow for deploying (#61)
1 parent ac52b7f commit 033e539

File tree

6 files changed

+250
-1
lines changed

6 files changed

+250
-1
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Build & Deploy Docker
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
dockerfile:
7+
description: "Path to Dockerfile"
8+
default: "Dockerfile"
9+
required: false
10+
type: string
11+
image_name:
12+
description: "Full image name (e.g. org/my-api)"
13+
required: true
14+
type: string
15+
image_tag:
16+
description: "Optional tag override (defaults to pushed Git tag)"
17+
required: false
18+
type: string
19+
remote_host:
20+
description: "SSH host (user@host)"
21+
required: true
22+
type: string
23+
remote_path:
24+
description: "Remote path where compose files live"
25+
required: true
26+
type: string
27+
runner_group:
28+
description: "Runner group or label"
29+
required: false
30+
default: "ubuntu-latest"
31+
type: string
32+
secrets:
33+
dockerhub_username:
34+
required: true
35+
dockerhub_password:
36+
required: true
37+
ssh_private_key:
38+
required: true
39+
outputs:
40+
tag:
41+
description: "Tag effectively built/deployed"
42+
value: ${{ jobs.get-tag.outputs.tag }}
43+
44+
permissions:
45+
id-token: write
46+
contents: read
47+
48+
jobs:
49+
get-tag:
50+
runs-on: ${{ inputs.runner_group }}
51+
outputs:
52+
tag: ${{ steps.out.outputs.tag }}
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v4
56+
with:
57+
fetch-depth: 0
58+
59+
- name: Compute tag
60+
id: out
61+
run: |
62+
TAG="${{ inputs.image_tag }}"
63+
if [ -z "$TAG" ]; then
64+
TAG="${GITHUB_REF##*/}" # refs/tags/v1.2.3 → v1.2.3
65+
fi
66+
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
67+
68+
build:
69+
needs: get-tag
70+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/[email protected]
71+
with:
72+
dockerfile: ${{ inputs.dockerfile }}
73+
image-name: ${{ inputs.image_name }}
74+
image-tag: ${{ needs.get-tag.outputs.tag }}
75+
hadolint: false
76+
security-scan: false
77+
push: true
78+
secrets:
79+
username: ${{ secrets.dockerhub_username }}
80+
password: ${{ secrets.dockerhub_password }}
81+
82+
deploy:
83+
needs: [build, get-tag]
84+
runs-on: ${{ inputs.runner_group }}
85+
86+
steps:
87+
- name: Checkout
88+
uses: actions/checkout@v4
89+
with:
90+
fetch-depth: 0
91+
92+
- name: Install SSH key
93+
uses: webfactory/[email protected]
94+
with:
95+
ssh-private-key: ${{ secrets.ssh_private_key }}
96+
97+
- name: Add remote host to known_hosts
98+
run: ssh-keyscan -H "${{ inputs.remote_host#*@ }}" >> ~/.ssh/known_hosts
99+
100+
- name: Prepare .env for Compose
101+
run: |
102+
cat <<EOF > .env
103+
IMAGE_NAME=${{ inputs.image_name }}
104+
IMAGE_TAG=${{ needs.get-tag.outputs.tag }}
105+
DOCKERHUB_USERNAME=${{ secrets.dockerhub_username }}
106+
DOCKERHUB_PASSWORD=${{ secrets.dockerhub_password }}
107+
EOF
108+
109+
- name: Copy compose files
110+
run: |
111+
scp docker-compose.yml .env "${{ inputs.remote_host }}":"${{ inputs.remote_path }}/"
112+
113+
- name: Pull & restart containers
114+
run: |
115+
ssh "${{ inputs.remote_host }}" bash -s <<'REMOTE'
116+
cd "${{ inputs.remote_path }}"
117+
set -e
118+
export \$(grep -v '^#' .env | xargs)
119+
echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
120+
docker compose pull
121+
docker compose up -d
122+
REMOTE

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This repository contains a comprehensive collection of reusable GitHub Actions w
77
### 🐳 [Build Docker Image](./docker-build)
88
Automates the process of building, tagging, and pushing Docker images to Docker Hub. Perfect for projects that require containerization with minimal configuration overhead.
99

10+
### 🚀 [Deploy Docker](./deploy-docker)
11+
Automates the process of building a Docker image and deploying it to a remote server. Configurable for different Dockerfile paths, image names, tags, and remote hosts. Streamlines the deployment process with secure SSH connections.
12+
1013
### 📦 [Release Please](./release-please)
1114
Uses the [release-please-action](https://github.com/googleapis/release-please-action) to automate versioning and changelog generation based on Conventional Commits. This workflow streamlines your release process and ensures consistent version management.
1215

deploy-docker/CHANGELOG.md

Whitespace-only changes.

deploy-docker/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
remote_host: '[email protected]'
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.

deploy-docker/version.txt

Whitespace-only changes.

publish-npm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ for the package scope, Node.js version, registry URL, and other options. The wor
7474

7575
jobs:
7676
publish:
77-
uses: your-org/your-repo/.github/workflows/publish-npm.yml@main
77+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/publish-npm.yml@main
7878
with:
7979
node-version: '22'
8080
build-command: 'npm run build:prod'

0 commit comments

Comments
 (0)