From 1dcd2f3b2b9fd8d6437f85e5fcbe4291e4dae5cd Mon Sep 17 00:00:00 2001 From: liviuancas-elrond Date: Fri, 25 Oct 2024 13:54:33 +0300 Subject: [PATCH 1/2] added dockerfile, workflow and entrypoint --- .github/workflows/build-docker-image.yaml | 45 ++++++++++++++++++++++ Dockerfile | 17 +++++++++ entrypoint.sh | 46 +++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 .github/workflows/build-docker-image.yaml create mode 100644 Dockerfile create mode 100644 entrypoint.sh diff --git a/.github/workflows/build-docker-image.yaml b/.github/workflows/build-docker-image.yaml new file mode 100644 index 000000000..9e6dd0f71 --- /dev/null +++ b/.github/workflows/build-docker-image.yaml @@ -0,0 +1,45 @@ +name: Publish Docker image + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + attestations: write + steps: + - name: Check out the repo + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '18' # Specify your Node.js version + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: multiversx/mx-api-service + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..72127a190 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM node:18.19-alpine + +WORKDIR /app +RUN chown -R node:node /app + +USER node +RUN mkdir -p /app/dist/src +COPY --chown=node . /app + +RUN npm install +RUN npm run init +RUN npm run build + +EXPOSE 3001 +RUN chmod +x entrypoint.sh + +CMD ["./entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 000000000..46230dac2 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# ENV VARIABLES + # MVX_ENV - defines what config to copy (default devnet) + # ELASTICSEARCH_URL - defines custom elasticsearch url - eg https://devnet-index.multiversx.com + # GATEWAY_URL - defines custom gateway url - eg https://devnet-gateway.multiversx.com + # REDIS_IP - defines redis ip - default 127.0.0.1 + +# CHECK IF ENV IS DEFINED +if [ -n "$MVX_ENV" ] && [ "$MVX_ENV" = "devnet" ]; then + # Copy config file + cp ./config/config.${MVX_ENV}.yaml /app/dist/config/config.yaml + + if [ $? -eq 0 ]; then + echo "Config file copied successfully from config/config.${MVX_ENV}.yaml /app/dist/config/config.yaml" + else + echo "Failed to copy the file." + fi + +else + cp ./config/config.devnet.yaml /app/dist/config/config.yaml + + if [ $? -eq 0 ]; then + echo "Default config file copied successfully from config/config.devnet.yaml /app/dist/config/config.yaml" + else + echo "Failed to copy the file." + fi +fi + +# Replaces urls if defined +if [ -n "$REDIS_IP" ]; then + echo "Redis IP defined: ${REDIS_IP}, replacing in config" + sed -i "s|redis: '127.0.0.1'|redis: '${REDIS_IP}'|g" /app/dist/config/config.yaml +fi + +if [ -n "$ELASTICSEARCH_URL" ]; then + echo "Elasticsearch url defined: ${ELASTICSEARCH_URL}, replacing in config" + sed -i "/^ elastic:/!b; n; s|.*| - '${ELASTICSEARCH_URL}'|" /app/dist/config/config.yaml +fi + +if [ -n "$GATEWAY_URL" ]; then + echo "Gateway url defined: ${GATEWAY_URL}, replacing in config" + sed -i "/^ gateway:/!b; n; s|.*| - '${GATEWAY_URL}'|" /app/dist/config/config.yaml +fi + + +exec /usr/local/bin/node dist/src/main.js From ad4dd11cec33fb04eb310e492506021441ec5810 Mon Sep 17 00:00:00 2001 From: cfaur09 Date: Mon, 28 Oct 2024 09:25:56 +0200 Subject: [PATCH 2/2] add entrypoint --- .github/workflows/build-docker-image.yaml | 4 ++-- entrypoint.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-docker-image.yaml b/.github/workflows/build-docker-image.yaml index 9e6dd0f71..4d0eff40d 100644 --- a/.github/workflows/build-docker-image.yaml +++ b/.github/workflows/build-docker-image.yaml @@ -4,7 +4,7 @@ on: release: types: [published] workflow_dispatch: - + jobs: push_to_registry: name: Push Docker image to Docker Hub @@ -42,4 +42,4 @@ jobs: file: ./Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} diff --git a/entrypoint.sh b/entrypoint.sh index 46230dac2..4d199798f 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -9,7 +9,7 @@ if [ -n "$MVX_ENV" ] && [ "$MVX_ENV" = "devnet" ]; then # Copy config file cp ./config/config.${MVX_ENV}.yaml /app/dist/config/config.yaml - + if [ $? -eq 0 ]; then echo "Config file copied successfully from config/config.${MVX_ENV}.yaml /app/dist/config/config.yaml" else @@ -18,7 +18,7 @@ if [ -n "$MVX_ENV" ] && [ "$MVX_ENV" = "devnet" ]; then else cp ./config/config.devnet.yaml /app/dist/config/config.yaml - + if [ $? -eq 0 ]; then echo "Default config file copied successfully from config/config.devnet.yaml /app/dist/config/config.yaml" else