|
| 1 | +name: CI Pipeline |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [master, main] |
| 5 | +jobs: |
| 6 | + unit-test: |
| 7 | + runs-on: ubuntu-latest |
| 8 | + env: |
| 9 | + CI: true |
| 10 | + build_env: production |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v2 |
| 13 | + - name: Run Unit Tests |
| 14 | + uses: actions/setup-node@v1 |
| 15 | + with: |
| 16 | + node-version: 14.x |
| 17 | + - run: | |
| 18 | + yarn |
| 19 | + yarn test --watchAll=false |
| 20 | + build: |
| 21 | + needs: unit-test |
| 22 | + runs-on: ubuntu-latest |
| 23 | + env: |
| 24 | + CI: true |
| 25 | + strategy: |
| 26 | + matrix: |
| 27 | + include: |
| 28 | + - env: stage |
| 29 | + bucket: <% index .Params `stagingFrontendSubdomain` %><% index .Params `stagingHostRoot` %> |
| 30 | + - env: production |
| 31 | + bucket: <% index .Params `productionFrontendSubdomain` %><% index .Params `productionHostRoot` %> |
| 32 | + outputs: |
| 33 | + needs-approval: "${{ steps.needs-approval.outputs.needs-approval }}" |
| 34 | + approved-deploy: "${{ steps.approved-deploy.outputs.approved-deploy }}" |
| 35 | + steps: |
| 36 | + - uses: actions/checkout@v2 |
| 37 | + - name: Build Static Site |
| 38 | + uses: actions/setup-node@v1 |
| 39 | + with: |
| 40 | + node-version: 14.x |
| 41 | + - run: | |
| 42 | + yarn |
| 43 | + yarn build |
| 44 | + env: |
| 45 | + REACT_APP_CONFIG: ${{ matrix.env }} |
| 46 | + - name: Upload build artifact to Github |
| 47 | + uses: actions/upload-artifact@v1 |
| 48 | + with: |
| 49 | + name: build-artifact-${{ matrix.env }} |
| 50 | + path: build/ |
| 51 | + ## This sets `step.outputs` is picked up by `jobs.outputs`, then when the step deploy reads `jobs.outputs` |
| 52 | + ## this provides data and set matrix for the deploy steps. This allows us to specify the data once |
| 53 | + ## and have the subsequence step can pass along the information created during this step. |
| 54 | + # the Deploy step will have a strategy matrix as follows: |
| 55 | + # { |
| 56 | + # "include": [{ |
| 57 | + # "env": "stage", |
| 58 | + # "buildName": "build-artifact-stage", |
| 59 | + # "bucket": "app.mydomain.com" |
| 60 | + # }] |
| 61 | + # } |
| 62 | + ## |
| 63 | + - name: needs-approval |
| 64 | + id: needs-approval |
| 65 | + if: ${{ matrix.env == 'production' }} |
| 66 | + run: echo "::set-output name=needs-approval::{\"include\":[{\"env\":\"${{ matrix.env }}\",\"buildName\":\"build-artifact-${{ matrix.env }}\",\"bucket\":\"${{ matrix.bucket }}\"}]}" |
| 67 | + - name: approved-deploy |
| 68 | + id: approved-deploy |
| 69 | + if: ${{ matrix.env == 'stage' }} |
| 70 | + run: echo "::set-output name=approved-deploy::{\"include\":[{\"env\":\"${{ matrix.env }}\",\"buildName\":\"build-artifact-${{ matrix.env }}\",\"bucket\":\"${{ matrix.bucket }}\"}]}" |
| 71 | + deploy: |
| 72 | + name: Deploy |
| 73 | + runs-on: ubuntu-latest |
| 74 | + needs: build |
| 75 | + strategy: |
| 76 | + # with `needs` declared, the job will have access to the output of the needed step |
| 77 | + # and in the needed step(`build`) we have the output of what should be instantly deployed |
| 78 | + matrix: ${{ fromJSON(needs.build.outputs.approved-deploy) }} |
| 79 | + env: |
| 80 | + region: <% index .Params `region` %> |
| 81 | + steps: |
| 82 | + # Once github action supports nested composite actions (anything `uses` is a composite action) |
| 83 | + # Therefore we cannot reuse the code as a separate composite action until it supports it, |
| 84 | + # current the deploy logic is in this file twice because of it |
| 85 | + ## https://github.com/actions/runner/issues/862 |
| 86 | + - uses: actions/checkout@v2 |
| 87 | + - name: Configure AWS credentials for S3 sync |
| 88 | + uses: aws-actions/configure-aws-credentials@v1 |
| 89 | + with: |
| 90 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 91 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 92 | + aws-region: <% index .Params `region` %> |
| 93 | + - name: Download build artifact from Github |
| 94 | + uses: actions/download-artifact@v1 |
| 95 | + with: |
| 96 | + name: ${{ matrix.buildName }} |
| 97 | + path: build/ |
| 98 | + - name: Sync with S3 |
| 99 | + shell: bash |
| 100 | + run: | |
| 101 | + cd build |
| 102 | + aws s3 sync . s3://${{ matrix.bucket }} |
| 103 | + - name: Invalidate Cloudfront |
| 104 | + shell: bash |
| 105 | + run: | |
| 106 | + export DIST_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items[?@=='${{ matrix.bucket }}']].Id | [0]" | tr -d '"') |
| 107 | + aws cloudfront create-invalidation --distribution-id ${DIST_ID} --paths "/*" |
| 108 | + integration-test: |
| 109 | + needs: [deploy] |
| 110 | + runs-on: ubuntu-latest |
| 111 | + name: integration-test |
| 112 | + steps: |
| 113 | + ## Example of smoke test against staging env before deploying to production |
| 114 | + ## To be enhanced to more sophisicated checks |
| 115 | + - run: echo "TEST_RESPONSE_COD=$(curl -o /dev/null -s -w \"%{http_code}\" https://<% index .Params `stagingFrontendSubdomain` %><% index .Params `stagingHostRoot` %>)" >> $GITHUB_ENV |
| 116 | + - if: ${{env.TEST_RESPONSE_COD != '"200"'}} |
| 117 | + run: exit 1 |
| 118 | + production-deploy: |
| 119 | + needs: [build, integration-test] |
| 120 | + runs-on: ubuntu-latest |
| 121 | + name: production-deploy |
| 122 | + env: |
| 123 | + region: <% index .Params `region` %> |
| 124 | + # with `needs` declared, the job will have access to the output of the needed step |
| 125 | + # and in the needed step(`build`) we have the output of what should be instantly deployed |
| 126 | + strategy: |
| 127 | + matrix: ${{ fromJSON(needs.build.outputs.needs-approval) }} |
| 128 | + steps: |
| 129 | + - uses: actions/checkout@v2 |
| 130 | + - name: Configure AWS credentials for S3 sync |
| 131 | + uses: aws-actions/configure-aws-credentials@v1 |
| 132 | + with: |
| 133 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 134 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 135 | + aws-region: ${{ env.region }} |
| 136 | + - name: Download build artifact from Github |
| 137 | + uses: actions/download-artifact@v1 |
| 138 | + with: |
| 139 | + name: ${{ matrix.buildName }} |
| 140 | + path: build/ |
| 141 | + - name: Sync with S3 |
| 142 | + run: | |
| 143 | + cd build |
| 144 | + aws s3 sync . s3://${{ matrix.bucket }} |
| 145 | + - name: Invalidate Cloudfront |
| 146 | + run: | |
| 147 | + export DIST_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items[?@=='${{ matrix.bucket }}']].Id | [0]" | tr -d '"') |
| 148 | + aws cloudfront create-invalidation --distribution-id ${DIST_ID} --paths "/*" |
0 commit comments