Skip to content

Commit 4afe25b

Browse files
ci: migrate API build and deployment to gha
1 parent fc582a6 commit 4afe25b

File tree

8 files changed

+197
-68
lines changed

8 files changed

+197
-68
lines changed

.drone-consider

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: API deploy release
2+
description: Build and deploy the API when a release is published
3+
4+
on:
5+
release:
6+
types: [published]
7+
8+
jobs:
9+
get-version:
10+
# only run for releases with tag 'iapp-api-v*' as created by release-please for API
11+
if: startsWith(github.ref_name,'iapp-api-v')
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
- name: Set up Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '20'
20+
- name: Set publish version
21+
id: set-publish-version
22+
working-directory: api
23+
run: |
24+
VERSION=$(npm pkg get version | tr -d '"')
25+
echo "VERSION=${VERSION}" | tee -a $GITHUB_OUTPUT
26+
outputs:
27+
version: ${{ steps.set-publish-version.outputs.VERSION }}
28+
29+
docker-publish:
30+
if: startsWith(github.ref_name,'iapp-api-v')
31+
needs: get-version
32+
uses: ./.github/workflows/reusable-api-docker.yml
33+
with:
34+
tag: ${{ needs.get-version.outputs.version }}
35+
secrets:
36+
docker-username: ${{ secrets.DOCKERHUB_USERNAME }}
37+
docker-password: ${{ secrets.DOCKERHUB_PAT }}
38+
39+
deploy:
40+
if: startsWith(github.ref_name,'iapp-api-v')
41+
needs: get-version
42+
uses: ./.github/workflows/reusable-api-deploy.yml
43+
with:
44+
tag: ${{ needs.get-version.outputs.version }}
45+
secrets:
46+
host: ${{ secrets.API_HOST }}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: API deploy rollback
2+
description: Rollback the API deployment to a previous version
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
version:
8+
description: 'Version Tag to rollback to'
9+
required: true
10+
type: string
11+
12+
jobs:
13+
deploy:
14+
if: ${{ github.ref_name == 'main' }}
15+
uses: ./.github/workflows/reusable-api-deploy.yml
16+
with:
17+
tag: ${{ inputs.version }}
18+
secrets:
19+
host: ${{ secrets.API_HOST }}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: API docker publish staging
2+
description: Publish a staging version of the API on Docker Hub
3+
4+
on:
5+
workflow_dispatch:
6+
7+
jobs:
8+
compute-staging-version:
9+
uses: ./.github/workflows/reusable-compute-staging-version.yml
10+
with:
11+
working-directory: api
12+
13+
docker-publish:
14+
needs: compute-staging-version
15+
uses: ./.github/workflows/reusable-api-docker.yml
16+
with:
17+
tag: ${{ needs.compute-staging-version.outputs.version }}
18+
secrets:
19+
docker-username: ${{ secrets.DOCKERHUB_USERNAME }}
20+
docker-password: ${{ secrets.DOCKERHUB_PAT }}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: deploy API
2+
description: reusable docker compose deployment workflow for this project
3+
4+
on:
5+
workflow_call:
6+
inputs:
7+
tag:
8+
description: 'Tag of Docker Image to deploy'
9+
required: true
10+
type: string
11+
host:
12+
description: 'Remote host to deploy to'
13+
required: true
14+
type: string
15+
16+
env:
17+
IMAGE_NAME: 'iexechub/iexec-iapp-api'
18+
19+
jobs:
20+
deploy:
21+
runs-on:
22+
group: Azure_runners
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Verify SSH key exists on runner
30+
run: |
31+
if [ ! -f ~/.ssh/ghrunnerci ]; then
32+
echo "SSH key not found at ~/.ssh/ghrunnerci on the runner"
33+
exit 1
34+
fi
35+
chmod 600 ~/.ssh/ghrunnerci
36+
shell: bash
37+
38+
- name: Add remote host to known_hosts
39+
run: |
40+
mkdir -p ~/.ssh
41+
ssh-keyscan ${{ inputs.host }} >> ~/.ssh/known_hosts
42+
chmod 644 ~/.ssh/known_hosts
43+
shell: bash
44+
45+
- name: Check for docker-compose.yml in workspace
46+
run: |
47+
if [ ! -f ./api/docker-compose.yml ]; then
48+
echo "docker-compose.yml not found in the repository"
49+
exit 1
50+
fi
51+
shell: bash
52+
53+
- name: Check image exists on Docker registry
54+
run: |
55+
if ! docker manifest inspect "${{ env.IMAGE_NAME }}:${{ inputs.tag }}"; then
56+
echo "Docker image ${{ env.IMAGE_NAME }}:${{ inputs.tag }} not found on Docker registry"
57+
exit 1
58+
fi
59+
shell: bash
60+
61+
- name: Prepare .env for Compose
62+
run: |
63+
printf "IMAGE_NAME=%s\nIMAGE_TAG=%s\n" "${{ env.IMAGE_NAME }}" "${{ inputs.tag }}"> .env
64+
shell: bash
65+
66+
- name: Copy files to remote server
67+
run: |
68+
sudo scp -o StrictHostKeyChecking=no \
69+
-i ~/.ssh/ghrunnerci \
70+
./api/docker-compose.yml ./.env \
71+
${{ inputs.host }}:/opt/iapp-api/
72+
shell: bash
73+
74+
- name: Run Docker Compose on remote server
75+
run: |
76+
ssh -o StrictHostKeyChecking=no \
77+
-i ~/.ssh/ghrunnerci \
78+
${{ inputs.host }} << 'EOF'
79+
cd /opt/iapp-api
80+
docker compose pull
81+
docker compose down --remove-orphans
82+
sleep 5
83+
docker compose up -d
84+
EOF
85+
shell: bash

.github/workflows/reusable-api-docker.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
docker-publish:
2828
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/[email protected]
2929
with:
30-
image-name: 'iexec-iapp-api'
31-
registry: 'docker-regis.iex.ec'
30+
image-name: 'iexechub/iexec-iapp-api'
31+
registry: 'docker.io'
3232
dockerfile: 'api/Dockerfile'
3333
context: 'api'
3434
security-scan: true

api/.drone.yml

Lines changed: 0 additions & 58 deletions
This file was deleted.

api/docker-compose.yml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
services:
2-
iapp-api:
3-
build:
4-
context: .
5-
dockerfile: Dockerfile
6-
image: iapp-api
7-
container_name: iapp-api
2+
iapp:
3+
image: ${IMAGE_NAME}:${TAG}
4+
container_name: iapp
5+
restart: unless-stopped
86
ports:
97
- '3000:3000'
108
volumes:
119
- /var/run/docker.sock:/var/run/docker.sock
12-
- ./.env:/app/.env
10+
# .env.app already on the server
11+
- ./.env.app:/app/.env
12+
healthcheck:
13+
test: ['CMD', 'curl', '-f', 'http://localhost:3000/health']
14+
interval: 30s
15+
timeout: 1m
16+
retries: 3
17+
start_period: 20s
18+
labels:
19+
- autoheal=true
20+
21+
autoheal:
22+
image: willfarrell/autoheal:1.2.0
23+
container_name: autoheal
24+
volumes:
25+
- /var/run/docker.sock:/var/run/docker.sock
26+
restart: unless-stopped
27+
environment:
28+
- AUTOHEAL_START_PERIOD=20
29+
- AUTOHEAL_INTERVAL=30
30+
- AUTOHEAL_ONLY_MONITOR_RUNNING=true

0 commit comments

Comments
 (0)