# Github - Deploying a Nest.js App to AWS Elastic Beanstalk (Docker Platform) with Github Actions

### Create a workflow for staging and production instances

Click on **Actions** tab within the Github repo and then Press on **New workflow**

![](https://3346773804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB9_-JlsKGrVkwNqqpR%2F-MDTfeVAcY_OOsACFSb7%2F-MDTiQ_etQAMyhwidJkR%2F%E6%88%AA%E5%9C%96%202020-07-30%20%E4%B8%8B%E5%8D%883.49.51.png?alt=media\&token=9519cda7-632a-4cf5-bdb9-479058571505)

![](https://3346773804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB9_-JlsKGrVkwNqqpR%2F-MDTfeVAcY_OOsACFSb7%2F-MDTilvyQQh7RHhbz3UR%2F%E6%88%AA%E5%9C%96%202020-07-30%20%E4%B8%8B%E5%8D%883.50.05.png?alt=media\&token=1f934b5c-28d5-469d-b765-df6482c378f9)

> You can skip this (Choose a workflow template) and click the ***Setup a workflow yourself*** and start from scratch.

### Setup workflow files

![](https://3346773804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB9_-JlsKGrVkwNqqpR%2F-MDTfeVAcY_OOsACFSb7%2F-MDTj9_1WQkqnX54NSXG%2F%E6%88%AA%E5%9C%96%202020-07-30%20%E4%B8%8B%E5%8D%883.50.33.png?alt=media\&token=dca2c634-2bba-4620-8ccf-3a22b6d8ebcf)

`production.yml`

```yaml
# This is a basic workflow to help you get started with Actions

name: CD

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

# A map of environment variables that are available to all jobs and steps in the workflow
env:
  AWS_ENVIRONMENT_NAME: nest-production
  AWS_DEFAULT_REGION: ap-northeast-1

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v1
        with:
          node-version: '12.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Install
        run: |
          node -v
          yarn -v
          yarn install

      - name: Build
        run: yarn build

      - name: Deploy
        uses: hmanzur/actions-aws-eb@v1.0.0
        with:
          command: 'deploy ${{ env.AWS_ENVIRONMENT_NAME }}'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }}
```

`staging.yml`

```yaml
# This is a basic workflow to help you get started with Actions

name: CD

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [staging]
  pull_request:
    branches: [staging]

# A map of environment variables that are available to all jobs and steps in the workflow
env:
  AWS_ENVIRONMENT_NAME: nest-staging
  AWS_DEFAULT_REGION: ap-northeast-1

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v1
        with:
          node-version: '12.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Install
        run: |
          node -v
          yarn -v
          yarn install

      - name: Build
        run: yarn build

      - name: Deploy
        uses: hmanzur/actions-aws-eb@v1.0.0
        with:
          command: 'deploy ${{ env.AWS_ENVIRONMENT_NAME }}'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }}
```

{% hint style="info" %}
Replace `AWS_ENVIRONMENT_NAME` with your environment of the elastic beanstalk application.\
Replace `AWS_DEFAULT_REGION` with the region where you created your elastic beanstalk application.
{% endhint %}

### Add AWS secrets to Github

![](https://3346773804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB9_-JlsKGrVkwNqqpR%2F-MDXf0qAljqBCwahCixy%2F-MDXfMhCg0dXnwc8fNPB%2F%E6%88%AA%E5%9C%96%202020-07-31%20%E4%B8%8A%E5%8D%8810.21.58.png?alt=media\&token=68d9a46f-d646-433d-a78b-77020be8f454)

### Push code to Github then Github Action will deploy your app to aws automatically
