From 1c9d78db1adba52b515cf412461d0d69407d13d2 Mon Sep 17 00:00:00 2001 From: Aniket Divekar <1047471+aniketdivekar@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:37:10 +0530 Subject: [PATCH] SHARD-916 Workflow addition to build and push docker image (#25) --- .dockerignore | 10 +++++ .github/workflows/docker.yml | 74 ++++++++++++++++++++++++++++++++++++ Dockerfile | 19 +++++++++ entrypoint.sh | 44 +++++++++++++++++++++ src/config/index.ts | 6 ++- src/storage/index.ts | 2 +- 6 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile create mode 100644 entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..aaacf37 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +node_modules +dist +.next +npm-debug.log +README.md +.dockerignore +Dockerfile +docker-compose.yml +.env +.github diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..8d1f11f --- /dev/null +++ b/.github/workflows/docker.yml @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b3e6c84 --- /dev/null +++ b/Dockerfile @@ -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 \ + < { export const initializeDB = async (): Promise => { await db.init({ - defaultDbSqlitePath: 'db.sqlite3', + defaultDbSqlitePath: config.dbPath, enableShardeumIndexer: config.enableShardeumIndexer, shardeumIndexerSqlitePath: config.shardeumIndexerSqlitePath, })