Skip to content

Commit

Permalink
Refactor build scripts and gh-action
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed May 8, 2024
1 parent db8bcef commit 52d9e0d
Show file tree
Hide file tree
Showing 20 changed files with 305 additions and 335 deletions.
35 changes: 6 additions & 29 deletions .github/workflows/build-and-deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,41 @@ run-name: Deploy by @${{ github.actor }}
on:
push:
branches:
- test
- test-deployment

env:
ENVIRONMENT: "test"
CARDANO_NETWORK: "sanchonet"
DOMAIN: "test-sanchonet.govtool.byron.network"

jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./scripts/govtool
working-directory: ./tests/test-infrastructure
env:
DBSYNC_POSTGRES_DB: "cexplorer"
DBSYNC_POSTGRES_USER: "postgres"
DBSYNC_POSTGRES_PASSWORD: "pSa8JCpQOACMUdGb"
DOCKER_HOST: ssh://ec2-user@${{secrets.TEST_STACK_SERVER_IP }}
GRAFANA_ADMIN_PASSWORD: ${{ secrets.GRAFANA_ADMIN_PASSWORD }}
GRAFANA_SLACK_RECIPIENT: ${{ secrets.GRAFANA_SLACK_RECIPIENT }}
GRAFANA_SLACK_OAUTH_TOKEN: ${{ secrets.GRAFANA_SLACK_OAUTH_TOKEN }}
NGINX_BASIC_AUTH: ${{ secrets.NGINX_BASIC_AUTH }}
SENTRY_DSN_BACKEND: ${{ secrets.SENTRY_DSN_BACKEND }}
TRAEFIK_LE_EMAIL: "admin+govtool@binarapps.com"
GTM_ID: ${{ secrets.GTM_ID }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN_FRONTEND }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
IP_ADDRESS_BYPASSING_BASIC_AUTH1: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH1 }}
IP_ADDRESS_BYPASSING_BASIC_AUTH2: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH2 }}
USERSNAP_SPACE_API_KEY: ${{ secrets.USERSNAP_SPACE_API_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.GHA_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.GHA_AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-1

- name: Login to AWS ECR
uses: aws-actions/configure-aws-credentials@v2
with:
aws-region: eu-west-1

- name: Setup SSH agent
uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.GHA_SSH_PRIVATE_KEY }}

- name: Deploy app
run: |
make --debug=b all
ssh-private-key: ${{ secrets.TEST_STACK_SSH_KEY }}

- name: Reprovision Grafana
- name: Update images
run: |
sleep 30 # give grafana time to start up
make --debug=b reload-grafana
./build-and-deploy.sh update-images
4 changes: 0 additions & 4 deletions gov-action-loader/backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
KUBER_API_URL=https://sanchonet.kuber.cardanoapi.io
KUBER_API_KEY=xxxxxxxxxxxxx

## Not required anymore
BLOCKFROST_API_URL=
BLOCKFROST_PROJECT_ID=
11 changes: 4 additions & 7 deletions gov-action-loader/backend/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from pydantic_settings import BaseSettings
from pydantic import BaseModel


class Settings(BaseSettings):
class Settings(BaseModel):
kuber_api_url: str
kuber_api_key: str
kuber_api_key: str = "" # Default value is an empty string

blockfrost_api_url: str
blockfrost_project_id: str


settings = Settings()
settings = Settings(kuber_api_url="your_api_url_here")
5 changes: 3 additions & 2 deletions govtool/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ARG BASE_IMAGE_TAG
FROM 733019650473.dkr.ecr.eu-west-1.amazonaws.com/backend-base:$BASE_IMAGE_TAG
ARG BASE_IMAGE_TAG=latest
ARG BASE_IMAGE_REPO=733019650473.dkr.ecr.eu-west-1.amazonaws.com/backend-base
FROM $BASE_IMAGE_REPO:$BASE_IMAGE_TAG
WORKDIR /src
COPY . .
RUN cabal build
Expand Down
6 changes: 3 additions & 3 deletions tests/test-infrastructure/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
STACK_NAME=govtool
PROJECT_NAME=govtool
CARDANO_NETWORK=sanchonet
BASE_DOMAIN=govtool.cardanoapi.io
BLOCKFROST_API_URL=""
BLOCKFROST_PROJECT_ID=""
GOVTOOL_TAG=test
155 changes: 31 additions & 124 deletions tests/test-infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -1,134 +1,41 @@
GovTool Test Infrastructure
====================

Services required for testing GovTool
Compose files and scripts to deploy and test environment of govtool.
Additionally, it deploys services required to perform integration test on the environment

## 1. Setting up the services
## Compose files and services
1. [basic-services](./docker-compose-basic-services.yml) : postgres and gateway
2. [cardano](./docker-compose-cardano.yml) : node, dbsync and kuber
3. [govtool](./docker-compose-govtool.yml) : govtool-frontend and govtool-backend
4. [govaction-loader](./docker-compose-govaction-loader.yml) : govaction-loader frontend and badkcne
5. [test](./docker-compose-test.yml) : lighthouse-server and metadata-api

## Setting up the services

#### a. Deploy with docker on swarm mode.

#### a. Update .env file and DNS records

- Create `.env` file by copying `.env.example` and update it.
- Make sure that DNS is pointed to the right server. Following are the domains used.
- lighthouse.BASE_DOMAIN
- metabase.BASE_DOMAIN
- sonarqube.BASE_DOMAIN
- metrics.BASE_DOMAIN
- kuber.BASE_DOMAIN


`docker stack deploy` command doesn't support `.env` file secret/config files.
There's a helper script `deploy-swarm.sh` to load the environment variables from `.env` file and generate rendered docker compose file.
```bash
cd ./test/test-infrastructure # cd into the test-infrastructure folder
docker swarm init # if swarm mode is not enabled yet.
docker compose build # build the images
docker node update xxxx --label-add govtool-test-stack=true ## set the node to be used for deploying the services
./gen-configs.sh # generate configs and secrets.
./deploy-swarm.sh prepare # start postgres and nginx
sleep 30 # wait for 30 secs for postgres to be healthy
./deploy-swarm.sh finalize # deploy all the required services.
```

#### b. Setup
When the stack is ready, further configuration is required it the services and github repo secrets and workflow files.

# 2. Services List

## SonarQube Server
#### Requires
- postgres database

#### Used by
- Github Action to submit sonar-sacanner result

`sonar-scanner` is used for static analysis of code.
The analysis generated by sonar-scanner is saved to SonarQube server for better visibility and to see progress over time.


**Docker Image:** [mc1arke/sonarqube-with-community-branch-plugin:9.9-community](https://hub.docker.com/layers/mc1arke/sonarqube-with-community-branch-plugin/9.9-community/images/sha256-b91ac551bea0fc3b394eaf7f82ea79115e03db9ab47d26610b9e1566723a07a5?context=explore)

**See :** [sonar-scanner](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scanners/sonarscanner/), [actions/sonar-scanner](https://github.com/marketplace/actions/sonar-scanner)

### Initial configuration.

- Login and change the initial password.
```
username: admin
password: admin
```
- Create new project and set the projectKey in file [govtool/frontend/sonar-project.properties](../../govtool/frontend/sonar-project.properties)
- Update the github action secrets
- SONAR_HOST_URL
- SONAR_TOKEN


## Metabase Server
#### Requires
- postgres database

Metabase provides UI to show graphs and visualization from different datasource.
It is used for visualizing the test metrics and the api response times over time.

**Docker Image:** [metabase/metabase:v0.46.6.4](https://hub.docker.com/layers/metabase/metabase/v0.46.6.4/images/sha256-95c60db0c87c5da9cb81f6aefd0cd548fe2c14ff8c8dcba2ea58a338865cdbd9?context=explore)

### Initial Configuration
- Setup initial account for login via the webapp.
- Under database section in admin settings, add the `govtool_lithghouse` and `govtool_metrics` databases
- Select the database and add visualizations, queries for the data.

## LightHouse Report Server
#### Requires
- postgres database

#### Used by
- GitHub Action to submit lighthouse report.

Lighthouse has audits for performance, accessibility, progressive web apps, SEO, and more.
Lighthouse-Server is used to host and display the audits generated by lighthouse.

**Docker Image:** [patrickhulce/lhci-server:0.12.0](https://hub.docker.com/r/patrickhulce/lhci-server)

### Initial Configuration
- install lhci locally and run `lhci wizard` to setup project
- update `--serverBaseUrl={{...}}` parameter in [.github/workflows/lighthouse.yml](../../.github/workflows/lighthouse.yml)
- update `LHCI_SERVER_TOKEN` in github secrets.
- install lighthouse github app on the repo
- obtain app token from lighthouse app and update `LHCI_GITHUB_APP_TOKEN` secret

See: **[lighthouse-server-docs](https://googlechrome.github.io/lighthouse-ci/docs/server.html)**


## Metrics API Server
#### Requires
- postgres database
- metabase *(for result visualization)


#### Used by
- Github Action - backend test to submit test metrics.

Metrics API Server receives metrics collected during backend test and saves them to database.
The results are visualized in metabase.

### Initial Configuration
- update `RECORD_METRICS_API` variable in file [.github/workflows/test_backend.yml](../../.github/workflows/test_backend.yml)


**Source Code:** [tests/test-metrics-api](../test-metrics-api)

## Kuber Server
#### Requires
- cardano-node's socket connection

#### Used by
- Cypress integration test
- Governance Data Loader

Opensource API server for transaction building and querying the ledger .
Kuber makes it easy to construct and submit transaction from the frontend.

**Docker Image:** [dquadrant/kuber:70be9b0166177eab5cf33e603fd3dc579e14cf31](https://hub.docker.com/layers/dquadrant/kuber/70be9b0166177eab5cf33e603fd3dc579e14cf31/images/sha256-d3b3f7c2304da8c4777155b26220238b682c81a3ff2b14753a5dc41c4f151364?context=explore)
- lighthouse-{BASE_DOMAIN}
- kuber-{BASE_DOMAIN}
- metadata-{BASE_DOMAIN}
- governance-{BASE_DOMAIN}

### b. Prepare the machine.
- Buy a virtual server
- Install `docker` and enable `docker compose` plugin.
- execute `docker swarm init` command.

### c. One time setup on the machine.
- Generate secrets and configurations required by the services
`./gen-configs.sh`
- Mark the nodes with labels to specify where the services should be run. In case of single node
docker swarm, all labels can be set to single node.
`./deploy.sh prepare`

### d. Build images and deploy the stacks.
- `./build-images.sh`
- `./deploy.sh stack all`

### Initial Configuration
- update `CYPRESS_kuberApiUrl` variable in [.github/workflows/test_integration_cypress.yml](../../.github/workflows/test_integration_cypress.yml)
30 changes: 30 additions & 0 deletions tests/test-infrastructure/build-and-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

BASE_IMAGE_NAME=govtool
export GOVTOOL_TAG="$(git rev-parse HEAD)"
export PROJECT_NAME=govtool
export CARDANO_NETWORK=sanchonet
export BASE_DOMAIN=govtool.cardanoapi.io

. ./scripts/deploy-stack.sh

check_env

# Build images
./build-images.sh
function update-service(){
docker service update --image "$2" "$1"
}

if [[ "$1" == "update-images" ]]

update-service govtool_backend "$BASE_IMAGE_NAME"/backend:${GOVTOOL_TAG}
update-service govtool_frontend "$BASE_IMAGE_NAME"/frontend:${GOVTOOL_TAG}
update-service govtool_metadata-validation "$BASE_IMAGE_NAME"/metadata-validation:${GOVTOOL_TAG}

update-service govaction-loader_backend "$BASE_IMAGE_NAME"/gov-action-loader-frontend:${GOVTOOL_TAG}
update-service govaction-loader_frontend "$BASE_IMAGE_NAME"/gov-action-loader-backend:${GOVTOOL_TAG}

elif [[ $1 == "full" ]]
./deploy stack all
fi
15 changes: 11 additions & 4 deletions tests/test-infrastructure/build-images.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/bash

set -e
export BASE_IMAGE_NAME="govtool"
# build the base image
docker build -t "$BASE_IMAGE_NAME"/backend-base -f ../../govtool/backend/Dockerfile.base ../../govtool/backend
BASE_IMAGE_EXISTS=$(docker images -q "$BASE_IMAGE_NAME"/backend-base 2> /dev/null)

if [ -z "$BASE_IMAGE_EXISTS" ]; then
echo "Building the base image..."
docker build -t "$BASE_IMAGE_NAME"/backend-base -f ../../govtool/backend/Dockerfile.base ../../govtool/backend
else
echo "Base image already exists. Skipping build."
fi

docker compose -f ./docker-compose-govtool.yml build
docker compose -f ./docker-compose.yml build
docker compose -f ./docker-compose-govaction-loader.yml build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE database ${STACK_NAME}_lighthouse;
CREATE database ${STACK_NAME}_metrics;
CREATE database ${STACK_NAME}_sonarqube;
CREATE database ${STACK_NAME}_dbsync;
CREATE database ${PROJECT_NAME}_lighthouse;
CREATE database ${PROJECT_NAME}_metrics;
CREATE database ${PROJECT_NAME}_sonarqube;
CREATE database ${PROJECßT_NAME}_dbsync;
Loading

0 comments on commit 52d9e0d

Please sign in to comment.