Skip to content

Commit caed217

Browse files
authored
Merge pull request #953 from Code4GovTech/feature/auto-deploy
Feature - Auto deployment
2 parents b5c4524 + 4726a5d commit caed217

File tree

3 files changed

+167
-19
lines changed

3 files changed

+167
-19
lines changed

.github/workflows/deploy.yml

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,141 @@
1-
name: Test and deploy
1+
name: Build
2+
3+
env:
4+
APP_NAME: CMS-BACKEND-API
5+
PROJECT_NAME: CMS-BACKEND-API
6+
DOCKER_COMPOSE_PATH: /root/app/docker-compose.yml
7+
REGISTRY: ghcr.io
8+
DOCKER_REGISTRY: ghcr.io/code4govtech/dmp-cms-backend-api
9+
DOT_ENV_FILE_NAME: env.dmp-cms-backend-api
10+
211

312
on:
13+
workflow_dispatch:
414
push:
5-
branches: [ main ]
6-
pull_request:
7-
branches: [ main ]
15+
branches:
16+
- devops
17+
- dev
18+
- main
19+
20+
permissions:
21+
contents: write
22+
packages: write
23+
824

925
jobs:
10-
build:
26+
set_vars:
27+
name: Set Environment Variables
28+
runs-on: ubuntu-latest
29+
outputs:
30+
TAG_LATEST: ${{ steps.tag_values.outputs.TAG_LATEST }}
31+
TAG_ENV_COMMIT: ${{ steps.tag_values.outputs.TAG_ENV_COMMIT }}
32+
APP_ENV: ${{ steps.tag_values.outputs.APP_ENV }}
33+
steps:
34+
- name: Set Docker Image Tags
35+
id: tag_values
36+
run: |
37+
case "${{ github.ref }}" in
38+
'refs/heads/main')
39+
echo "TAG_LATEST=prod-latest" >> $GITHUB_OUTPUT
40+
echo "TAG_ENV_COMMIT=prod-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
41+
echo "APP_ENV=PROD" >> $GITHUB_OUTPUT
42+
;;
43+
'refs/heads/devops')
44+
echo "TAG_LATEST=dev-latest" >> $GITHUB_OUTPUT
45+
echo "TAG_ENV_COMMIT=dev-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
46+
echo "APP_ENV=DEV" >> $GITHUB_OUTPUT
47+
;;
48+
'refs/heads/dev')
49+
echo "TAG_LATEST=dev-latest" >> $GITHUB_OUTPUT
50+
echo "TAG_ENV_COMMIT=dev-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
51+
echo "APP_ENV=DEV" >> $GITHUB_OUTPUT
52+
;;
53+
esac
1154
55+
build:
56+
name: Build
1257
runs-on: ubuntu-latest
58+
needs: [set_vars]
59+
permissions:
60+
contents: read
61+
packages: write
62+
env:
63+
TAG_LATEST: ${{ needs.set_vars.outputs.TAG_LATEST }}
64+
TAG_ENV_COMMIT: ${{ needs.set_vars.outputs.TAG_ENV_COMMIT }}
65+
steps:
66+
- name: Checkout code
67+
uses: actions/checkout@v2
1368

14-
strategy:
15-
matrix:
16-
node-version: [16.x]
69+
# - name: Login to GitHub Packages
70+
# run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
1771

18-
steps:
19-
- name: Checkout repository
20-
uses: actions/checkout@v2
72+
- name: Log in to the Container registry
73+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
74+
with:
75+
registry: ${{ env.REGISTRY }}
76+
username: ${{ github.actor }}
77+
password: ${{ secrets.GITHUB_TOKEN }}
78+
79+
- name: Set Docker Tags
80+
uses: actions/setup-node@v2
81+
82+
- name: Read Secrets
83+
run: |
84+
touch .env
85+
mv .env ${{ env.DOT_ENV_FILE_NAME }}
86+
87+
- name: Copy env file to DEV Server
88+
uses: appleboy/[email protected]
89+
if: needs.set_vars.outputs.APP_ENV == 'DEV'
90+
with:
91+
host: ${{ vars.DEV_SERVER_HOST }}
92+
username: ${{ vars.DEV_SERVER_USERNAME }}
93+
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
94+
port: ${{ vars.DEV_SERVER_PORT }}
95+
source: "${{ env.DOT_ENV_FILE_NAME }}"
96+
target: /root/app/
97+
98+
- name: Build ${{ env.APP_NAME }} Docker image
99+
run: |
100+
docker build -t ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }} .
21101
22-
- name: Set up Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v1
24-
with:
25-
node-version: ${{ matrix.node-version }}
102+
- name: Add tag to Docker image
103+
run: |
104+
echo ${{ github.sha }}
105+
docker tag ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }} ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_ENV_COMMIT }}
26106
27-
- name: Install dependencies
28-
run: yarn install --frozen-lockfile
107+
- name: Push Docker image to GitHub Packages
108+
run: |
109+
docker push ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }}
110+
docker push ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_ENV_COMMIT }}
111+
112+
deploy:
113+
name: Deployment
114+
runs-on: ubuntu-latest
115+
needs: build
116+
if: github.event_name == 'push' && github.ref_type == 'branch'
117+
118+
steps:
119+
- name: Deploy to DevOps/Dev Environment
120+
if: github.ref == 'refs/heads/devops' || github.ref == 'refs/heads/dev'
121+
uses: appleboy/[email protected]
122+
env:
123+
DOCKER_COMPOSE_PATH: ${{ env.DOCKER_COMPOSE_PATH }}
124+
APP_NAME: ${{ env.APP_NAME }}
125+
DOCKER_REGISTRY: ${{ env.DOCKER_REGISTRY }}
126+
with:
127+
host: ${{ vars.DEV_SERVER_HOST }}
128+
username: ${{ vars.DEV_SERVER_USERNAME }}
129+
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
130+
port: ${{ vars.DEV_SERVER_PORT }}
131+
allenvs: true
132+
script_stop: true
133+
envs: DOCKER_COMPOSE_PATH,APP_NAME,DOCKER_REGISTRY
134+
script: |
135+
echo "Docker Compose Path $DOCKER_COMPOSE_PATH"
136+
docker compose -f $DOCKER_COMPOSE_PATH pull
137+
docker compose -f $DOCKER_COMPOSE_PATH up -d
29138
30-
- name: Build
31-
run: yarn build
139+
- name: Deploy to Prod environment
140+
if: github.ref == 'refs/heads/main'
141+
run: echo "Deploying to Kubernetes"

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# syntax=docker/dockerfile:1
2+
3+
## Start with a base image containing NodeJS so we can build Docusaurus.
4+
FROM node:lts as base
5+
## Disable colour output from yarn to make logs easier to read.
6+
ENV FORCE_COLOR=0
7+
## Enable corepack.
8+
RUN corepack enable
9+
## Set the working directory to `/opt/docusaurus`.
10+
WORKDIR /opt/docusaurus
11+
12+
# Stage 2b: Production build mode.
13+
FROM base as prod
14+
## Set the working directory to `/opt/docusaurus`.
15+
WORKDIR /opt/docusaurus
16+
## Copy over the source code.
17+
COPY . /opt/docusaurus/
18+
## Install dependencies with `--immutable` to ensure reproducibility.
19+
RUN npm ci
20+
## Build the static site.
21+
RUN npm run build
22+
23+
# Stage 3a: Serve with `docusaurus serve`.
24+
FROM prod as serve
25+
## Expose the port that Docusaurus will run on.
26+
EXPOSE 3000
27+
## Run the production server.
28+
CMD ["npm", "run", "serve", "--", "--host", "0.0.0.0"]

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "docusaurus"
2+
services:
3+
serve:
4+
build:
5+
context: .
6+
target: serve
7+
ports:
8+
- "3000:3000"
9+
environment:
10+
- NODE_ENV=production

0 commit comments

Comments
 (0)