From 3f8fcbcc6f11b723663cba2172fd987b23567ad5 Mon Sep 17 00:00:00 2001 From: Siddharth Rawat Date: Sat, 31 Dec 2022 20:59:46 +0530 Subject: [PATCH] :volcano: CI/CD pipeline updates - Update Packer AMI to use commit hash as `ami_name` - Update `actions/checkout` to `v3` - Update Packer action to `hashicorp/setup-packer` - Fix unit test CI pipeline to use `test` _ENVIRONMENT_ - Bypass database `connection` and `sync` for `test` _ENVIRONMENT_ [This avoids mocha to throw errors connecting and syncing the database while running unit tests] This commit solves #6 --- .github/workflows/packer-build.yml | 39 +++++++++++++-------------- .github/workflows/packer-validate.yml | 22 ++++++++------- .github/workflows/test-suite.yml | 37 +++++++++++++------------ ami.pkr.hcl | 2 +- app.js | 6 +++-- nodeserver.service | 2 +- scripts/post-install.sh | 9 ++++--- 7 files changed, 63 insertions(+), 54 deletions(-) diff --git a/.github/workflows/packer-build.yml b/.github/workflows/packer-build.yml index 7e2b81d..8bb24de 100644 --- a/.github/workflows/packer-build.yml +++ b/.github/workflows/packer-build.yml @@ -3,6 +3,7 @@ name: Instance Refresh env: ARTIFACT_NAME: webapp AWS_REGION: us-east-1 + ENVIRONMENT: test on: push: @@ -17,11 +18,11 @@ jobs: name: Run unit tests steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: 'Create .env file' run: | touch .env - echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env + echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env echo PORT=${{ secrets.PORT }} >> .env echo DATABASE=${{ secrets.DATABASE }} >> .env @@ -39,12 +40,12 @@ jobs: name: Build AWS Custom AMI with REST API app artifacts steps: - name: Checkout code from branch - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Build artifacts run: | touch .env - echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env + echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env echo PORT=${{ secrets.PORT }} >> .env echo DATABASE=${{ secrets.DATABASE }} >> .env @@ -60,26 +61,24 @@ jobs: aws-secret-access-key: ${{ secrets.AWS_DEV_SECRET_KEY }} aws-region: ${{ secrets.AWS_REGION }} - - name: Packer init - uses: hashicorp/packer-github-actions@master + - name: Setup `packer` + uses: hashicorp/setup-packer@main + id: setup with: - command: init - target: ami.pkr.hcl + version: 'latest' + + - name: Packer init + id: init + run: 'packer init ami.pkr.hcl' - name: Validate packer template - uses: hashicorp/packer-github-actions@master - with: - command: validate - target: ami.pkr.hcl + id: validate + run: 'packer validate -evaluate-datasources ami.pkr.hcl' + + - name: Build AMI using Packer + id: build + run: 'packer build ami.pkr.hcl' - - name: Packer build - uses: hashicorp/packer-github-actions@master - with: - command: build - arguments: '-color=false -on-error=abort' - target: ami.pkr.hcl - env: - PACKER_LOG: 1 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v1 with: diff --git a/.github/workflows/packer-validate.yml b/.github/workflows/packer-validate.yml index 8cd37ee..200f373 100644 --- a/.github/workflows/packer-validate.yml +++ b/.github/workflows/packer-validate.yml @@ -3,7 +3,7 @@ name: Packer AMI template validation on: pull_request: - branches: [ master ] + branches: [master] jobs: validate: @@ -11,21 +11,23 @@ jobs: name: Validate packer template for the custom AMI steps: - name: Checkout code from branch - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Build artifacts run: | zip -r webapp.zip ./ pwd ./webapp - - name: Packer init - uses: hashicorp/packer-github-actions@master + - name: Setup `packer` + uses: hashicorp/setup-packer@main + id: setup with: - command: init - target: ami.pkr.hcl + version: 'latest' + + - name: Packer init + id: init + run: 'packer init ami.pkr.hcl' - name: Validate packer template - uses: hashicorp/packer-github-actions@master - with: - command: validate - target: ami.pkr.hcl \ No newline at end of file + id: validate + run: 'packer validate -evaluate-datasources ami.pkr.hcl' diff --git a/.github/workflows/test-suite.yml b/.github/workflows/test-suite.yml index 564b994..4c529fb 100644 --- a/.github/workflows/test-suite.yml +++ b/.github/workflows/test-suite.yml @@ -3,11 +3,14 @@ name: Unit Test Suite +env: + ENVIRONMENT: test + # Controls when the action will run. on: # Triggers the workflow on push or pull request events but only for the main branch pull_request: - branches: [ master ] + branches: [master] jobs: build: @@ -18,19 +21,19 @@ jobs: name: Run unit tests steps: - - uses: actions/checkout@v2 - - name: 'Create .env file' - run: | - touch .env - echo ENVIRONMENT=${{ secrets.ENVIRONMENT }} >> .env - echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env - echo PORT=${{ secrets.PORT }} >> .env - echo DATABASE=${{ secrets.DATABASE }} >> .env - echo DBUSER=${{ secrets.DBUSER }} >> .env - echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node-version }} - - run: yarn - - run: yarn test + - uses: actions/checkout@v3 + - name: 'Create .env file' + run: | + touch .env + echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env + echo HOSTNAME=${{ secrets.HOSTNAME }} >> .env + echo PORT=${{ secrets.PORT }} >> .env + echo DATABASE=${{ secrets.DATABASE }} >> .env + echo DBUSER=${{ secrets.DBUSER }} >> .env + echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + - run: yarn + - run: yarn test diff --git a/ami.pkr.hcl b/ami.pkr.hcl index f7fd447..30e4afc 100755 --- a/ami.pkr.hcl +++ b/ami.pkr.hcl @@ -51,7 +51,7 @@ source "amazon-ebs" "ec2" { # ami_name = "EC2-AMI-${substr(data.git-commit.commit.hash, 0, 8)}" # ami_name = "EC2-AMI-{{ ${substr(data.git-commit.cwd-head.hash, 0, 8)} }}" region = "${var.aws_region}" - ami_name = "EC2-AMI-${local.timestamp}" + ami_name = "EC2-AMI-${local.version}" ami_description = "EC2 AMI for CSYE 6225 built by ${data.git-commit.cwd-head.author}" ami_users = [ "057919684206", # dev account ID diff --git a/app.js b/app.js index 0da6f67..39e6318 100644 --- a/app.js +++ b/app.js @@ -18,8 +18,10 @@ app.use(express.json()) app.use('/', health, userRoutes, documentRoute) -db.connectionTest() -db.sequelize.sync() +if (ENVIRONMENT !== 'test') { + db.connectionTest() + db.sequelize.sync() +} app.listen(PORT, () => { if (ENVIRONMENT !== 'prod') logger.info(`Server running at http://${HOSTNAME}:${PORT}`) diff --git a/nodeserver.service b/nodeserver.service index cf185fb..80df29b 100644 --- a/nodeserver.service +++ b/nodeserver.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=simple User=ubuntu -ExecStart=/usr/bin/node /home/ubuntu/webapp/app.js +ExecStart=node /home/ubuntu/webapp/app.js EnvironmentFile=/home/ubuntu/webapp/.env # ExecStartPost=/bin/sh -c "sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 1337" Restart=on-failure diff --git a/scripts/post-install.sh b/scripts/post-install.sh index 27be1dc..3ab57b3 100755 --- a/scripts/post-install.sh +++ b/scripts/post-install.sh @@ -14,10 +14,13 @@ echo "+------------------------------------------------------------------------- sudo cp /home/ubuntu/nodeserver.service /lib/systemd/system/nodeserver.service echo "Enabling the REST API Service" sudo systemctl daemon-reload +sleep 5 sudo systemctl enable nodeserver -# sudo systemctl start nodeserver -# sudo systemctl restart nodeserver -# sudo systemctl status nodeserver +sleep 5 +sudo systemctl start nodeserver +sleep 5 +sudo systemctl restart nodeserver +sudo systemctl status nodeserver APISRVC=$? if [ $APISRVC -eq 0 ]; then echo "API service is installed successfully!"