From 91236dde86fd34f6c032a63259dd0be3fd0ca93c Mon Sep 17 00:00:00 2001 From: Victor Radael Date: Mon, 21 Mar 2022 10:03:54 -0300 Subject: [PATCH 1/3] CI: Deploy --- .github/workflows/deploy.yml | 200 +++++++++++++++++++---------------- .github/workflows/test.yml | 10 +- .releaserc.json | 5 +- Dockerfile | 10 +- 4 files changed, 123 insertions(+), 102 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 87a14c3..59af93d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -9,103 +9,115 @@ on: jobs: deploy: runs-on: self-hosted + steps: - - name: Checkout - uses: actions/checkout@v2 + - name: Checkout + uses: actions/checkout@v2 - - name: Semantic Release - uses: cycjimmy/semantic-release-action@v2 - id: semantic - with: - semantic_version: 16 - extra_plugins: | - @saithodev/semantic-release-backmerge - conventional-changelog-eslint - branches: | - [ - 'main', - { - name: 'alpha', - prerelease: true - }, - { - name: 'beta', - prerelease: true - } - ] - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Because Semantic Realese deleted the .npmrc file, it was necessary to insert two steps to backup the file. + - name: BKP .npmrc Step 1 + run: | + cp .npmrc .npmrcbkp + - name: Semantic Release + uses: cycjimmy/semantic-release-action@v2 + id: semantic + with: + semantic_version: 16 + extra_plugins: | + @saithodev/semantic-release-backmerge + conventional-changelog-eslint + branches: | + [ + 'main', + { + name: 'alpha', + prerelease: true + }, + { + name: 'beta', + prerelease: true + } + ] + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - - name: Update Pip - if: steps.semantic.outputs.new_release_published == 'true' - run: | - python3 -m pip install --upgrade pip + # Because Semantic Realese deleted the .npmrc file, it was necessary to insert two steps to backup the file. + - name: BKP .npmrc Step 2 + run: | + cp .npmrcbkp .npmrc + - name: Update Pip + if: steps.semantic.outputs.new_release_published == 'true' + run: | + python3 -m pip install --upgrade pip + ls -a + - name: Install Docker Compose + if: steps.semantic.outputs.new_release_published == 'true' + run: | + python3 -m pip install docker-compose --upgrade + ls -a + - name: Install AWS CLI + if: steps.semantic.outputs.new_release_published == 'true' + run: | + python3 -m pip install awscli --upgrade + ls -a + - name: Install AWS Elastic Beanstalk CLI + if: steps.semantic.outputs.new_release_published == 'true' + run: | + python3 -m pip install awsebcli --upgrade + ls -a + - name: Configure AWS Region + if: steps.semantic.outputs.new_release_published == 'true' + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: us-east-1 - - name: Install Docker Compose - if: steps.semantic.outputs.new_release_published == 'true' - run: | - python3 -m pip install docker-compose --upgrade + - name: Extract Environment + if: steps.semantic.outputs.new_release_published == 'true' + shell: bash + run: | + ls -a + if [ ${GITHUB_REF} == "refs/heads/main" ]; then + echo "##[set-output name=result;]$(echo "production")" + else + echo "##[set-output name=result;]$(echo ${GITHUB_REF#refs/heads/})" + fi + id: extract_environment - - name: Install AWS CLI - if: steps.semantic.outputs.new_release_published == 'true' - run: | - python3 -m pip install awscli --upgrade + - name: Login to AWS ECR + if: steps.semantic.outputs.new_release_published == 'true' + id: login_ecr + uses: aws-actions/amazon-ecr-login@v1 - - name: Install AWS Elastic Beanstalk CLI - if: steps.semantic.outputs.new_release_published == 'true' - run: | - python3 -m pip install awsebcli --upgrade - - - name: Configure AWS Region - if: steps.semantic.outputs.new_release_published == 'true' - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-region: us-east-1 - - - name: Extract Environment - if: steps.semantic.outputs.new_release_published == 'true' - shell: bash - run: | - if [ ${GITHUB_REF} == "refs/heads/main" ]; then - echo "##[set-output name=result;]$(echo "production")" - else - echo "##[set-output name=result;]$(echo ${GITHUB_REF#refs/heads/})" - fi - id: extract_environment - - - name: Login to AWS ECR - if: steps.semantic.outputs.new_release_published == 'true' - id: login_ecr - uses: aws-actions/amazon-ecr-login@v1 - - - name: Build, Tag, and Push Image to AWS ECR - if: steps.semantic.outputs.new_release_published == 'true' - env: - ENV: ${{ steps.extract_environment.outputs.result }} - IMAGE_TAG: ${{ steps.semantic.outputs.new_release_version }} - run: | - docker-compose -f build.docker-compose.yml build - docker-compose -f build.docker-compose.yml push - - - name: Create ZIP file to Deploy AWS Beanstalk - if: steps.semantic.outputs.new_release_published == 'true' - env: - ENV: ${{ steps.extract_environment.outputs.result }} - IMAGE_TAG: ${{ steps.semantic.outputs.new_release_version }} - run: | - sed -i -e "s/\${ENV}/$ENV/g" docker-compose.yml - sed -i -e "s/\${IMAGE_TAG}/$IMAGE_TAG/g" docker-compose.yml - zip deploy.zip docker-compose.yml - - - name: Deploy AWS Beanstalk - if: steps.semantic.outputs.new_release_published == 'true' - env: - ENV: ${{ steps.extract_environment.outputs.result }} - AWS_REGION: us-east-1 - APP_NAME: maestro - run: | - [[ -d .elasticbeanstalk/ ]] && rm -R .elasticbeanstalk/ - printf "1\nn" | eb init $APP_NAME --region $AWS_REGION - eb use $APP_NAME-$ENV - echo -e "deploy:\n artifact: deploy.zip" >> .elasticbeanstalk/config.yml - eb deploy + - name: Build, Tag, and Push Image to AWS ECR + if: steps.semantic.outputs.new_release_published == 'true' + env: + ENV: ${{ steps.extract_environment.outputs.result }} + IMAGE_TAG: ${{ steps.semantic.outputs.new_release_version }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + docker-compose -f build.docker-compose.yml build --build-arg NPM_TOKEN=${NPM_TOKEN} + docker-compose -f build.docker-compose.yml push + ls -a + - name: Create ZIP file to Deploy AWS Beanstalk + if: steps.semantic.outputs.new_release_published == 'true' + env: + ENV: ${{ steps.extract_environment.outputs.result }} + IMAGE_TAG: ${{ steps.semantic.outputs.new_release_version }} + run: | + sed -i -e "s/\${ENV}/$ENV/g" docker-compose.yml + sed -i -e "s/\${IMAGE_TAG}/$IMAGE_TAG/g" docker-compose.yml + zip deploy.zip docker-compose.yml + ls -a + - name: Deploy AWS Beanstalk + if: steps.semantic.outputs.new_release_published == 'true' + env: + ENV: ${{ steps.extract_environment.outputs.result }} + AWS_REGION: us-east-1 + APP_NAME: maestro + run: | + [[ -d .elasticbeanstalk/ ]] && rm -R .elasticbeanstalk/ + printf "1\nn" | eb init $APP_NAME --region $AWS_REGION + eb use $APP_NAME-$ENV + echo -e "deploy:\n artifact: deploy.zip" >> .elasticbeanstalk/config.yml + eb deploy \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cc65444..1ec0dd7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,19 +16,21 @@ jobs: - name: Install Docker Compose run: | python3 -m pip install docker-compose --upgrade - - name: Build env: ENV: test IMAGE_TAG: test + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | export ENV=test - docker-compose -f build.docker-compose.yml build - + export IMAGE_TAG=test + docker-compose -f build.docker-compose.yml build --build-arg NPM_TOKEN=${NPM_TOKEN} - name: Run Test env: ENV: test IMAGE_TAG: test + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | export ENV=test - docker-compose run --rm --entrypoint="npm run test" maestro + export IMAGE_TAG=test + docker-compose -f build.docker-compose.yml run -e NPM_TOKEN=${NPM_TOKEN} --rm --entrypoint="npm run test" maestro \ No newline at end of file diff --git a/.releaserc.json b/.releaserc.json index 1fda875..80c93d6 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -29,7 +29,10 @@ [ "@saithodev/semantic-release-backmerge", { - "branches": ["alpha", "beta"], + "branches": [ + { "from": "main", "to": "alpha" }, + { "from": "main", "to": "beta" } + ], "backmergeStrategy": "merge" } ], diff --git a/Dockerfile b/Dockerfile index 3b435f3..f80c060 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,16 @@ -FROM node:14 as packages +FROM node:14.15.4-alpine3.12 as packages WORKDIR /packages +ARG NPM_TOKEN COPY package.json . +COPY .npmrc . RUN npm install +RUN rm -f ./.npmrc -FROM node:alpine +FROM node:14.15.4-alpine3.12 WORKDIR /app COPY . /app/ +ARG NPM_TOKEN COPY --from=packages /packages/node_modules /app/node_modules RUN npm run build EXPOSE 3333 -ENTRYPOINT npm run start +ENTRYPOINT npm run start \ No newline at end of file From 44d4b8e08c9ce82340e11bcd53ae7dfa4fd4f00d Mon Sep 17 00:00:00 2001 From: Victor Radael Date: Mon, 21 Mar 2022 10:39:41 -0300 Subject: [PATCH 2/3] CI: Deploy --- package-lock.json | 30 ++++++++++-------------------- package.json | 2 +- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ea838d..ef742f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1280,12 +1280,12 @@ } }, "@nestjs/config": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-1.2.0.tgz", - "integrity": "sha512-GGZOj2g6EMZ23orsQqeD2Vs5E2ZrmAiB0qCGvERv+5nQmZjY4nKkisG4awQsym1uotmmzgtsd9lOiKqTIFONhA==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-1.2.1.tgz", + "integrity": "sha512-EgaGTXvG4unD5lGWmdSrUFrkGpX32lQGE/8qS60EnL82sIZV7HT1ZL7ib5S86P1nB+DnFDbDhDqTaZ3mivTyOg==", "requires": { "dotenv": "16.0.0", - "dotenv-expand": "8.0.1", + "dotenv-expand": "5.1.0", "lodash": "4.17.21", "uuid": "8.3.2" }, @@ -1964,21 +1964,11 @@ } }, "@victorradael/protospack": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/@victorradael/protospack/-/protospack-1.1.3.tgz", - "integrity": "sha512-+Pc5vaVWQSCKzPOWxpPr1v+X2e6AJhRW6S2meKIty2dZzsA92rZNx4268R/1d2qzMD9VbmPqgsnA2lvSuIP/nw==", + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@victorradael/protospack/-/protospack-1.2.9.tgz", + "integrity": "sha512-zOgUz74iNA0cqh41mX5aAcKoFQUHdR/AMRypkxTNMDDA8dXkgqaCvglCSpSehnbGAj0AVw8pHl1u9DP1JRVw3g==", "requires": { "rxjs": "^7.5.5" - }, - "dependencies": { - "rxjs": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.5.5.tgz", - "integrity": "sha512-sy+H0pQofO95VDmFLzyaw9xNJU4KTRSwQIGM6+iG3SypAtCiLDzpeG8sJrNCWn2Up9km+KhkvTdbkrdy+yzZdw==", - "requires": { - "tslib": "^2.1.0" - } - } } }, "@webassemblyjs/ast": { @@ -3239,9 +3229,9 @@ "integrity": "sha512-vwEppIphpFdvaMCaHfCEv9IgwcxMljMw2TnAQBB4VWPvzXQLTb82jwmdOKzlEVUL3gNFT4l4TPKO+Bn+sqcrVQ==" }, "dotenv-expand": { - "version": "8.0.1", - "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-8.0.1.tgz", - "integrity": "sha512-j/Ih7bIERDR5PzI89Zu8ayd3tXZ6E3dbY0ljQ9Db0K87qBO8zdLsi2dIvDHMWtjC3Yxb8XixOTHAtia0fDHRpg==" + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-5.1.0.tgz", + "integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==" }, "ecdsa-sig-formatter": { "version": "1.0.11", diff --git a/package.json b/package.json index 37cf174..f723b27 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@grpc/grpc-js": "^1.5.7", "@grpc/proto-loader": "^0.6.9", "@nestjs/common": "^8.4.0", - "@nestjs/config": "^1.2.0", + "@nestjs/config": "^1.2.1", "@nestjs/core": "^8.4.0", "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.0", From 26ec0b3dfb6892e29aebc3cca49b51de1df989cf Mon Sep 17 00:00:00 2001 From: Victor Radael Date: Mon, 21 Mar 2022 10:56:46 -0300 Subject: [PATCH 3/3] CI: Deploy --- docker-compose.yml | 2 ++ src/modules/health/health.service.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 619f44f..fb0aad4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: restart: always ports: - 3333:3333 + env_file: + - .env node_exporter: image: prom/node-exporter:v1.3.1 diff --git a/src/modules/health/health.service.ts b/src/modules/health/health.service.ts index 62b5601..8754715 100644 --- a/src/modules/health/health.service.ts +++ b/src/modules/health/health.service.ts @@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class HealthService { check() { + console.log('HEALTH'); return { message: 'Ok' }; } }