Skip to content

Deploy

Deploy #42

Workflow file for this run

name: Test and Deploy
on:
pull_request:
branches:
- stage
- main
types:
- closed
permissions:
id-token: write
contents: read
pull-requests: write
env:
ECR_REPOSITORY_NESTJS: 'hhp-nestjs'
ECS_SERVICE_NAME: 'nestjs'
ECS_CLUSTER_NAME: 'hhp-cluster'
JWT_SECRET: ${{ secrets.JWT_SECRET }}
JWT_EXPIRES_IN: ${{ secrets.JWT_EXPIRES_IN }}
DATABASE_CONNECTION: ${{ secrets.DATABASE_CONNECTION }}
DATABASE_HOST: ${{ secrets.DATABASE_HOST }}
DATABASE_PORT: ${{ secrets.DATABASE_PORT }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
DATABASE_NAME: ${{ secrets.DATABASE_NAME }}
SWAGGER_URL: ${{ secrets.SWAGGER_URL }}
API_AUTH_USER: ${{ secrets.API_AUTH_USER }}
API_AUTH_PASSWORD: ${{ secrets.API_AUTH_PASSWORD }}
AWS_LOG_GROUP_NAME: ${{ secrets.AWS_LOG_GROUP_NAME }}
AWS_LOG_STREAM_NAME: ${{ secrets.AWS_LOG_STREAM_NAME }}
AWS_LOG_ACCESS_KEY_ID: ${{ secrets.AWS_LOG_ACCESS_KEY_ID }}
AWS_LOG_SECRET_ACCESS_KEY: ${{ secrets.AWS_LOG_SECRET_ACCESS_KEY }}
AWS_LOG_REGION: ${{ secrets.AWS_LOG_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
jobs:
test:
name: test nestjs
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: ${{ env.DATABASE_USERNAME }}
POSTGRES_PASSWORD: ${{ env.DATABASE_PASSWORD }}
POSTGRES_DB: ${{ env.DATABASE_NAME }}
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup node
uses: actions/[email protected]
with:
node-version: '18'
cache: 'npm'
- name: Cache node modules
uses: actions/[email protected]
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: npm install
- name: Run lint
run: npm run lint
- name: Run tests
run: npm run test
- name: Comment with Test Coverage
uses: dkershner6/jest-coverage-commenter-action@v1
with:
github_token: "${{ secrets.GITHUB_TOKEN }}"
test_command: "npm run test:cov"
- name: Migrate database
env:
DATABASE_CONNECTION: 'postgres'
DATABASE_HOST: 'localhost'
DATABASE_PORT: '5432'
DATABASE_USERNAME: ${{ env.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ env.DATABASE_PASSWORD }}
DATABASE_NAME: 'postgres'
run: npm run migration:run
- name: Run e2e tests
env:
DATABASE_CONNECTION: 'postgres'
DATABASE_HOST: 'localhost'
DATABASE_PORT: '5432'
DATABASE_USERNAME: ${{ env.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ env.DATABASE_PASSWORD }}
DATABASE_NAME: 'postgres'
run: npm run test:e2e
- name: Notify Slack on Failure
if: failure()
uses: 8398a7/[email protected]
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
author_name: ${{ github.actor }}
channel: ${{ secrets.SLACK_CHANNEL }}
username: ${{ secrets.SLACK_USERNAME }}
text: 'Tests failed! :x:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Notify Slack on Success
if: success()
uses: 8398a7/[email protected]
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
author_name: ${{ github.actor }}
channel: ${{ secrets.SLACK_CHANNEL }}
username: ${{ secrets.SLACK_USERNAME }}
text: 'Tests passed! :white_check_mark:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
build_and_deploy:
if: github.event.pull_request.merged == true
name: build docker image and ecs deploy
runs-on: ubuntu-22.04
needs: test
steps:
- name: checkout
uses: actions/[email protected]
- name: configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-region: ap-northeast-2
role-to-assume: arn:aws:iam::332034673285:role/HHP-Org
role-session-name: HHP-Org
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and push NestJS
uses: docker/[email protected]
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY_NESTJS }}:${{ github.sha }}
build-args: |
NODE_ENV=production
JWT_SECRET=${{ env.JWT_SECRET }}
JWT_EXPIRES_IN=${{ env.JWT_EXPIRES_IN }}
DATABASE_CONNECTION=${{ env.DATABASE_CONNECTION }}
DATABASE_HOST=${{ env.DATABASE_HOST }}
DATABASE_PORT=${{ env.DATABASE_PORT }}
DATABASE_USERNAME=${{ env.DATABASE_USERNAME }}
DATABASE_PASSWORD=${{ env.DATABASE_PASSWORD }}
DATABASE_NAME=${{ env.DATABASE_NAME }}
SWAGGER_URL=${{ env.SWAGGER_URL }}
API_AUTH_USER=${{ env.API_AUTH_USER }}
API_AUTH_PASSWORD=${{ env.API_AUTH_PASSWORD }}
AWS_LOG_GROUP_NAME=${{ env.AWS_LOG_GROUP_NAME }}
AWS_LOG_STREAM_NAME=${{ env.AWS_LOG_STREAM_NAME }}
AWS_LOG_ACCESS_KEY_ID=${{ env.AWS_LOG_ACCESS_KEY_ID }}
AWS_LOG_SECRET_ACCESS_KEY=${{ env.AWS_LOG_SECRET_ACCESS_KEY }}
AWS_LOG_REGION=${{ env.AWS_LOG_REGION }}
AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}
AWS_REGION=${{ env.AWS_REGION }}
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: ${{ env.ECR_REPOSITORY_NESTJS }}
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY_NESTJS }}:${{ github.sha }}
- name: configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-region: ap-northeast-2
role-to-assume: arn:aws:iam::332034673285:role/HHP-Org
role-session-name: HHP-Org
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE_NAME }}
cluster: ${{ env.ECS_CLUSTER_NAME }}
wait-for-service-stability: false
- name: Notify Slack on Failure
if: failure()
uses: 8398a7/[email protected]
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
author_name: ${{ github.actor }}
channel: ${{ secrets.SLACK_CHANNEL }}
username: ${{ secrets.SLACK_USERNAME }}
text: 'Deploy failed! :x:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Notify Slack on Success
if: success()
uses: 8398a7/[email protected]
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
author_name: ${{ github.actor }}
channel: ${{ secrets.SLACK_CHANNEL }}
username: ${{ secrets.SLACK_USERNAME }}
text: 'Deploy success! :white_check_mark:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}