diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..9d67b809 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +terraform +.gitignore +README.md +LICENSE +yarn.lock +.env.example +.env +.git +test +node_modules \ No newline at end of file diff --git a/.github/workflows/build-latest-docker-image.yaml b/.github/workflows/build-latest-docker-image.yaml new file mode 100644 index 00000000..b9d92d1c --- /dev/null +++ b/.github/workflows/build-latest-docker-image.yaml @@ -0,0 +1,63 @@ +name: Build and push latest docker image + +on: + push: + branches: + - main + - feat/dockerize-build + workflow_dispatch: +jobs: + build-and-push-latest-docker-image: + strategy: + matrix: + os: + - ubuntu-20.04 + runs-on: ${{ matrix.os }} + permissions: + id-token: write + contents: read + env: + REPOSITORY: axelarscan-api + IMAGE_TAG: ${{ github.sha }} + AWS_ACCOUNT_ID: 499786161782 + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: fetch tags + run: | + git fetch --unshallow + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: us-east-2 + role-to-assume: arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/ghwf-${{ github.event.repository.name }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: check if an image already build on same commit hash + id: image-tag-check + run: | + app_version="v$(jq -r .version package.json)" + if [ ${{ github.ref }} != 'refs/heads/main' ]; then + app_version="${app_version}-dev_${GITHUB_RUN_NUMBER}.${GITHUB_RUN_ATTEMPT}" + fi + image_tag_exists=$(aws ecr batch-get-image --repository-name ${REPOSITORY} --image-ids "imageTag=${app_version}" | jq '.images | length') + echo "image_tag_exists=${image_tag_exists}" >> $GITHUB_OUTPUT + echo "IMAGE_TAG=${app_version}" >> $GITHUB_ENV + + - name: Build docker image + if: steps.image-tag-check.outputs.image_tag_exists == 0 + run: | + docker build --platform linux/amd64 -t axelarscan/axelarscan-api . + + - name: Push to ECR + if: steps.image-tag-check.outputs.image_tag_exists == 0 + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + run: | + docker image tag axelarscan/axelarscan-api "${ECR_REGISTRY}/${REPOSITORY}:${IMAGE_TAG}" + docker push "${ECR_REGISTRY}/${REPOSITORY}:${IMAGE_TAG}" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c35413a4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM public.ecr.aws/lambda/nodejs:20 + +COPY --from=public.ecr.aws/datadog/lambda-extension:55 /opt/extensions/ /opt/extensions + +COPY . ${LAMBDA_TASK_ROOT} + +RUN npm install yarn --global +RUN npm install datadog-lambda-js dd-trace + +RUN yarn + +CMD [ "index.handler" ]