Skip to content

Commit

Permalink
Add deploy action
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Nov 8, 2024
1 parent ddc059e commit d1173bc
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 4 deletions.
54 changes: 54 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
/integration_test
/.github
/deployment
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# cloud sql proxy
cloud-sql-proxy

#npm
.npmrc

# Sentry Config File
.env.sentry-build-plugin

# eslint config
eslint.config.mjs
Dockerfile

allure-results
84 changes: 84 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Build and deploy CCVA
run-name: Deploy by @${{ github.actor }}

on:
push:
branches:
- test
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
steps:
- name: Set up SSH and deploy
uses: appleboy/[email protected]
env:
COMMIT_SHA: ${{github.sha}}
with:
host: ${{ secrets.BASTION_SERVER_HOST }}
username: githubci
key: ${{ secrets.BASTION_SERVER_SSH_KEY }}
port: ${{ secrets.BASTION_SERVER_SSH_PORT }}
envs: COMMIT_SHA
command_timeout: 40m

script: |
set -euo pipefail
REPO_URL="[email protected]:${{ github.repository }}"
DEST_DIR="$HOME/Documents/${{ github.repository }}"
# Create parent directory if it does not exist
mkdir -p "$(dirname "$DEST_DIR")"
# Check if $DEST_DIR exists
if [ -d "$DEST_DIR" ]; then
cd $DEST_DIR || exit
if [ -d ".git" ]; then
echo "Updating repository..."
git remote set-url origin "$REPO_URL"
git fetch --all
git checkout --force "$COMMIT_SHA"
else
echo "Not a git repository. Re-cloning..."
rm -rf "$DEST_DIR"
git clone "$REPO_URL" "$DEST_DIR"
cd "$DEST_DIR" || exit
git checkout --force "$COMMIT_SHA"
fi
else
echo "Directory does not exist. Cloning repository..."
git clone "$REPO_URL" "$DEST_DIR"
cd "$DEST_DIR" || exit
git checkout --force "$COMMIT_SHA"
fi
cd $DEST_DIR/deployment
# REGISTRY_BASE=docker.io/ccva
# STACK_NAME=ccva
# DEPLOY_BASE_DOMAIN=ccv.cardanoapi.io
# IMAGE_VERSION_TAG=v1
# METADATA_API_DATABASE_URL=postgres://drep_id:xxxxxxxxxxx@postgres/drep_metadata
# CARDANO_NETWORK=preprod
# OPERATOR_SIGNING_KEY=xxxxxxxxxxx
# GITHUB_TOKEN=XXXXX
# Write environment variables to .env file
{
echo 'IMAGE_VERSION_TAG=${{ github.sha }}'
echo 'GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}'
echo 'REGISTRY_BASE=registry.sireto.io/cardanoapi'
echo 'STACK_NAME=ccva'
echo 'BASE_DOMAIN=ccv.cardanoapi.io'
echo 'DATABASE_PASSWORD=postgres'
echo 'CARDANO_NETWORK=preprod'
} > .env
# push images
export DOCKER_HOST=172.31.0.5:2376
./scripts/build-images.sh push
# make deployment. NOTE that this won't fail if the service fails. this might be mis-leading.
export DOCKER_HOST=172.31.0.5:2376
./scripts/deploy.sh stack ccva --with-registry-auth
9 changes: 6 additions & 3 deletions .github/workflows/test_integration_playwright.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: Integration Test [Playwright]

on:
push:
branches:
- test
workflow_run:
workflows:
- Build and deploy CCVA
branches: test
types: completed
workflow_dispatch:

jobs:
integration-tests:
Expand Down
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ARG NPM_AUTH_TOKEN

FROM node:20-alpine AS builder
WORKDIR /app
RUN npm install -g prisma
COPY ./package.json ./package-lock.json ./
COPY ./prisma ./prisma
RUN npm config set //registry.npmjs.org/:_authToken ${NPM_AUTH_TOKEN} --location=global
RUN npm install --legacy-peer-deps
COPY --chown=node:node . .


ENV NEXT_PUBLIC_NODE_ENV=production

RUN npm run build
RUN rm -rf ./.next/cache/* && mkdir moveTarget && mv public ./moveTarget


# Runtime image
FROM node:20-alpine
WORKDIR /app
USER node

ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
EXPOSE 3000


COPY --from=builder --chown=node:node /app/package.json ./
COPY --from=builder --chown=node:node /app/node_modules ./node_modules
COPY --from=builder --chown=node:node /app/moveTarget/ ./
COPY --from=builder --chown=node:node /app/.next ./.next
VOLUME /home/node/.next/cache
CMD ["npm","run","start"]

5 changes: 5 additions & 0 deletions deployment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
secrets/
configs/
.env
/*-rendered.yml

9 changes: 9 additions & 0 deletions deployment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# CCVA Deployment Scripts

Compose files and scripts to deploy and test environment of ccva.

## Usage

```sh
./scripts/build-and-deploy.sh
```
30 changes: 30 additions & 0 deletions deployment/deployment/scripts/build-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -e

if [ -z "$IMAGE_VERSION_TAG" ]; then
IMAGE_VERSION_TAG="$(git rev-parse HEAD)"
fi
export IMAGE_VERSION_TAG

# Define a function to log and execute Docker commands
docker_() {
local cmd="$*"
echo docker "$cmd"
docker $cmd
}

echo "$1" -eq 'push'

if [[ "$1" == 'push' ]]
then
set -eo pipefail
. ./scripts/docker-stack-deploy.sh
load_env
docker_ compose -f ./docker-compose-ccva.yml build

echo "Pushing the images..."

docker push $REGISTRY_BASE/webapp:${IMAGE_VERSION_TAG}
else
docker_ compose -f ./docker-compose-ccva.yml build
fi
33 changes: 33 additions & 0 deletions deployment/docker-compose-ccva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.9'

services:
webapp:
image: ${REGISTRY_BASE}/webapp:${IMAGE_VERSION_TAG}
build:
context: ../
networks:
- default
- frontend
environment:
- VIRTUAL_HOST=https://${DEPLOY_BASE_DOMAIN}
- DATABASE_URL=postgresql://postgres:postgres@db:5432/ccva
- NEXTAUTH_URL=https://${DEPLOY_BASE_DOMAIN}

db:
image: postgres:15
container_name: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=ccva
volumes:
- db:/var/lib/postgresql/data
networks:
- default

networks:
frontend:
name: frontend
external: true
volumes:
db:
48 changes: 48 additions & 0 deletions deployment/scripts/build-and-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
if [ -z "$IMAGE_VERSION_TAG" ]; then
IMAGE_VERSION_TAG="$(git rev-parse HEAD)"
fi
export IMAGE_VERSION_TAG
set -e

. ./scripts/docker-stack-deploy.sh
load_env
check_env
USE_REGISTRY="$2"
# Build images
if [[ "$USE_REGISTRY" == "use-registry" ]]
then
echo '> ./scripts/build-images.sh push'
./scripts/build-images.sh push
else
echo '+ ./scripts/build-images.sh'

./scripts/build-images.sh
fi

function update-service(){
if [[ "$USE_REGISTRY" == "use-registry" ]]
then
echo '> docker' service update --with-registry-auth --image "$2" "$1"
docker service update --image "$2" "$1"
else
echo '> docker' service update --with-registry-auth --image "$2" "$1"
docker service update --with-registry-auth --image "$2" "$1"
fi
}

if [[ "$1" == "update-images" ]]
then
update-service ccva_backend "$REGISTRY_BASE"/ccva-backend:${IMAGE_VERSION_TAG}
# test metadata API

elif [[ $1 == "full" ]]
then
if [[ "$USE_REGISTRY" == "use-registry" ]]
then
./scripts/deploy.sh stack all --with-registry-auth
else
./scripts/deploy.sh stack all
fi

fi
30 changes: 30 additions & 0 deletions deployment/scripts/build-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -e

if [ -z "$IMAGE_VERSION_TAG" ]; then
IMAGE_VERSION_TAG="$(git rev-parse HEAD)"
fi
export IMAGE_VERSION_TAG

# Define a function to log and execute Docker commands
docker_() {
local cmd="$*"
echo docker "$cmd"
docker $cmd
}

echo "$1" -eq 'push'

if [[ "$1" == 'push' ]]
then
set -eo pipefail
. ./scripts/docker-stack-deploy.sh
load_env
docker_ compose -f ./docker-compose-ccva.yml build

echo "Pushing the images..."

docker push $REGISTRY_BASE/webapp:${IMAGE_VERSION_TAG}
else
docker_ compose -f ./docker-compose-ccva.yml build
fi
Loading

0 comments on commit d1173bc

Please sign in to comment.