【NestJS + GitHubActions】 CI / CD環境を構築してみる


概要

以下を参考にCI/CD環境を構築してみた

参考にした記事

プロジェクトをセットアップする

$ npm i -g @nestjs/cli
$ nest new project-name

  We will scaffold your app in a few seconds..

CREATE meetup/.eslintrc.js (631 bytes)
CREATE meetup/.prettierrc (51 bytes)
CREATE meetup/README.md (3339 bytes)
CREATE meetup/nest-cli.json (64 bytes)
CREATE meetup/package.json (1995 bytes)
CREATE meetup/tsconfig.build.json (97 bytes)
CREATE meetup/tsconfig.json (546 bytes)
CREATE meetup/src/app.controller.spec.ts (617 bytes)
CREATE meetup/src/app.controller.ts (274 bytes)
CREATE meetup/src/app.module.ts (249 bytes)
CREATE meetup/src/app.service.ts (142 bytes)
CREATE meetup/src/main.ts (208 bytes)
CREATE meetup/test/app.e2e-spec.ts (630 bytes)
CREATE meetup/test/jest-e2e.json (183 bytes)

? Which package manager would you ❤️  to use? npm
✔ Installation in progress... ☕

🚀  Successfully created project meetup
👉  Get started with the following commands:

Herokuで新しいアプリを作成

新しいリポジトリを作成しプッシュ

テスト用ワークフローを定義

.github/workflows/node.js.yml
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
  deploy: ## <-- this is the new stuff
    needs: build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: akhileshns/[email protected]
      with:
        heroku_api_key: ${{secrets.HEROKU_API_KEY}}
        heroku_app_name: ${{secrets.HEROKU_APP_NAME}}
        heroku_email: ${{secrets.HEROKU_EMAIL}}
        remote_branch: master

GitHubSecretにデプロイ情報を登録

変更をmainブランチにプッシュする

先ほど設定したワークフローが実行されればOK