Setting up CI/CD with GitHub Actions and CapRover
Tags: actionshackathon21
Published: 2021-12-06 21:08:23

[Instructions]: # (To submit to the GitHub Actions x DEV Hackathon 2021, please fill out all sections.)


### My Workflow

https://github.com/streamlux/pulsebanner

[Note]: # (Please make sure the project links to the appropriate GitHub Actions repository, and includes [an open source license](https://choosealicense.com/) and README.)

![Screenshot of workflow run summary on GitHub.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ybc9wl9k3hondivwts3.png) 

I learned a lot about GitHub Actions creating this workflow, and specifically about how to use the [matrix strategy](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix). I've included the section of the workflow that uses the matrix strategy below. This job deploys all 3 apps to the staging environment on CapRover.

```yaml
    deploy_staging:
        name: Deploy to Staging
        runs-on: ubuntu-latest
        needs: [build_next, build_nest, build_remotion]
        environment: Staging
        concurrency: Staging

        env:
            CAPROVER_URL: ${{ secrets.CAPROVER_URL }}
            NEST_CAPROVER_APP_TOKEN: ${{ secrets.NEST_CAPROVER_APP_TOKEN }}
            NEXT_CAPROVER_APP_TOKEN: ${{ secrets.NEXT_CAPROVER_APP_TOKEN }}
            REMOTION_CAPROVER_APP_TOKEN: ${{ secrets.REMOTION_CAPROVER_APP_TOKEN }}

        strategy:
            matrix:
                include:
                    - app: nest # name of the app in Caprover
                      token-key: NEST_CAPROVER_APP_TOKEN # key used to get CAPROVER_APP_TOKEN from env
                      image: ${{ needs.build_nest.outputs.image-tag }} # image to deploy
                    - app: next
                      token-key: NEXT_CAPROVER_APP_TOKEN
                      image: ${{ needs.build_next.outputs.image-tag }}
                    - app: remotion
                      token-key: REMOTION_CAPROVER_APP_TOKEN
                      image: ${{ needs.build_remotion.outputs.image-tag }}

        steps:
            # Install Caprover CLI, which we use to deploy images to Caprover
            - name: 'Install caprover-cli'
              run: npm install -g caprover

            # Deploy each app by iterating over matrix values
            - name: 'Deploy ${{ matrix.app }}'
              env:
                  APP_NAME: ${{ matrix.app }}
                  APP_URL: ${{ env.CAPROVER_URL }}
                  CAPROVER_APP_TOKEN: ${{ env[matrix.token-key] }}
                  IMAGE_NAME: ${{ matrix.image }}
              run: 'caprover deploy --caproverUrl=$APP_URL --imageName=$IMAGE_NAME --appName=$APP_NAME'
```

### Submission Category: 

DIY Deployments

[Note]: # (Maintainer Must-Haves, DIY Deployments, Interesting IoT, Phone Friendly, or Wacky Wildcards)


### Yaml File or Link to Code

[Note]: # (Our markdown editor supports pretty embeds. Try this syntax: `{% github link_to_your_repo %}` to share a GitHub repository)

{% github https://github.com/streamlux/pulsebanner %}

### Additional Resources / Info

https://github.com/streamlux/pulsebanner