Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHARD-916 Workflow addition to build and push docker image #24

Merged
merged 14 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
dist
.next
npm-debug.log
README.md
.dockerignore
Dockerfile
docker-compose.yml
.env
.github
62 changes: 62 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Create and publish a Docker image

on:
push:
branches: ['dev']
pull_request:
branches: ['dev']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
display-image-name:
runs-on: ubuntu-latest
steps:
- name: Display IMAGE_NAME
run: echo "IMAGE_NAME is ${{ env.IMAGE_NAME }}"
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get short commit hash
id: get-short-hash
run: echo "SHORT_COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Determine branch name
id: get-branch-name
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV # Source branch of the PR
else
echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV # Actual branch name for push events
fi
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }}-${{ env.SHORT_COMMIT_HASH }}
labels: |
version=${{ env.SHORT_COMMIT_HASH }}
branch=${{ env.BRANCH_NAME }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# syntax=docker/dockerfile:1

## global args
ARG NODE_VERSION=18.16.1

FROM node:${NODE_VERSION}
arhamj marked this conversation as resolved.
Show resolved Hide resolved
SHELL [ "/bin/bash", "-cex" ]

# Create app directory
WORKDIR /usr/src/app

# Bundle app source
COPY . .

# Install node_modules
RUN \
<<EOF
npm install
npm install pm2 -g
EOF

ENTRYPOINT [ "/usr/src/app/entrypoint.sh" ]
44 changes: 44 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash -x

# Default number of servers to start if not set in the environment
NO_OF_SERVERS=${NO_OF_SERVERS:-1}

# Function to start multiple collector servers using PM2
start_collector_server() {
local server_port=6001

for i in $(seq "$NO_OF_SERVERS"); do
pm2 start --daemon --name "ldrpc-server-$i" npm -- run server "$server_port"
server_port=$((server_port + 1))
done
}

# Function to start a collector using PM2
start_collector() {
pm2 start --daemon --name "ldrpc-collector" npm -- run collector
}

# Function to start a log server using PM2
start_log_server() {
pm2 start --daemon --name "ldrpc-log_server" npm -- run log_server
}

# Main script execution based on the input argument
case "$1" in
"server")
start_collector_server
;;
"collector")
start_collector
;;
"log_server")
start_log_server
;;
*)
echo "Error: Service '$1' is not recognizable."
exit 1
;;
esac

# Tail PM2 logs to keep the Docker container running
exec pm2 logs
6 changes: 4 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum collectorMode {
export interface Config {
env: string
host: string
dbPath: string
dataLogWrite: boolean
dataLogWriter: {
dirName: string
Expand Down Expand Up @@ -78,6 +79,7 @@ export interface Config {
let config: Config = {
env: process.env.SHARDEUM_COLLECTOR_MODE || envEnum.PROD, //default safe if env is not set
host: process.env.HOST || '127.0.0.1',
dbPath: process.env.COLLECTOR_DB_PATH || "db.sqlite3",
dataLogWrite: false,
dataLogWriter: {
dirName: 'data-logs',
Expand All @@ -91,7 +93,7 @@ let config: Config = {
secretKey: '',
},
hashKey: '69fa4195670576c0160d660c3be36556ff8d504725be8a59b5a96509e0c994bc',
enableCollectorSocketServer: false,
enableCollectorSocketServer: Boolean(process.env.ENABLE_COLLECTOR_SOCKET_SERVER) || false,
port: {
collector: process.env.COLLECTOR_PORT || '4444',
server: process.env.PORT || '6101',
Expand Down Expand Up @@ -119,7 +121,7 @@ let config: Config = {
enableTxHashCache: false,
findTxHashInOriginalTx: false,
enableShardeumIndexer: true,
shardeumIndexerSqlitePath: 'shardeum.sqlite',
shardeumIndexerSqlitePath: process.env.SERVICE_VALIDATOR_DB_PATH || "db.sqlite3",
blockIndexing: {
enabled: true,
blockProductionRate: 6,
Expand Down
2 changes: 1 addition & 1 deletion src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const isBlockIndexingEnabled = (): boolean => {

export const initializeDB = async (): Promise<void> => {
await db.init({
defaultDbSqlitePath: 'db.sqlite3',
defaultDbSqlitePath: config.dbPath,
enableShardeumIndexer: config.enableShardeumIndexer,
shardeumIndexerSqlitePath: config.shardeumIndexerSqlitePath,
})
Expand Down
Loading