Skip to content

Commit

Permalink
Revert "Revert "SHARD-916 Workflow addition to build and push docker …
Browse files Browse the repository at this point in the history
…image (#24)""

This reverts commit 91afd5c.
  • Loading branch information
aniketdivekar committed Oct 26, 2024
1 parent 91afd5c commit d58e0d1
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 3 deletions.
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
74 changes: 74 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Create and publish a Docker image

on:
push:
branches: ['dev']
workflow_dispatch:
inputs:
tag:
description: 'Tag for the Docker image'
required: true

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 and determine branch name
id: set-env-vars
run: |
# Get short commit hash
echo "SHORT_COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
# Determine branch name
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: Set Docker image tag
id: set-docker-tag
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "DOCKER_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_ENV
else
echo "DOCKER_TAG=${{ env.BRANCH_NAME }}-${{ env.SHORT_COMMIT_HASH }}" >> $GITHUB_ENV
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.DOCKER_TAG }}
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
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# syntax=docker/dockerfile:1

FROM node:18.16.1-alpine
SHELL [ "/bin/sh", "-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

0 comments on commit d58e0d1

Please sign in to comment.