|
| 1 | +# CI pipeline |
| 2 | +# This runs against all the commits landed on master |
| 3 | +# It will do the following things: |
| 4 | +# - run sanity tests against the checked-out code |
| 5 | +# - build an image and upload the docker image to ECR |
| 6 | +# - deploy to staging environment |
| 7 | +# - run smoke test against staging enviroment |
| 8 | +# - deploy to production |
| 9 | +name: CI Pipeline |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: [master, main] |
| 13 | +env: |
| 14 | + # Environment variables shared across jobs and doesn't change per environment |
| 15 | + region: <% index .Params `region` %> |
| 16 | + accountId: "<% index .Params `accountId` %>" |
| 17 | + repo: <% .Name %> |
| 18 | + namespace: <% .Name %> |
| 19 | +jobs: |
| 20 | + unit-test: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + env: |
| 23 | + CI: true |
| 24 | + build_env: production |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v2 |
| 27 | + - uses: actions/setup-node@v2 |
| 28 | + with: |
| 29 | + node-version: "14" |
| 30 | + - run: npm install |
| 31 | + - run: npm test |
| 32 | + |
| 33 | + build: |
| 34 | + needs: unit-test |
| 35 | + runs-on: ubuntu-latest |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@v2 |
| 38 | + - name: Set IMAGE_TAG as env |
| 39 | + run: | |
| 40 | + IMAGE_TAG=$(git rev-parse --short=7 ${{ github.sha }}) |
| 41 | + echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV |
| 42 | + - name: Configure AWS credentials |
| 43 | + uses: aws-actions/configure-aws-credentials@v1 |
| 44 | + with: |
| 45 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 46 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 47 | + aws-region: ${{ env.region }} |
| 48 | + - name: Login to Amazon ECR |
| 49 | + id: login-ecr |
| 50 | + uses: aws-actions/amazon-ecr-login@v1 |
| 51 | + - name: Build and push |
| 52 | + id: docker_build |
| 53 | + uses: docker/build-push-action@v2 |
| 54 | + with: |
| 55 | + push: true |
| 56 | + tags: ${{ env.accountId }}.dkr.ecr.${{ env.region }}.amazonaws.com/${{ env.repo }}:${{ env.IMAGE_TAG }} |
| 57 | + |
| 58 | + staging-deploy: |
| 59 | + needs: build |
| 60 | + runs-on: ubuntu-latest |
| 61 | + env: |
| 62 | + environment-overlay: staging |
| 63 | + cluster-name: "<% .Name %>-stage-<% index .Params `region` %>" |
| 64 | + cluster-authentication-role-arn: "arn:aws:iam::<% index .Params `accountId` %>:role/<% .Name %>-kubernetes-deployer-stage" |
| 65 | + steps: |
| 66 | + - uses: actions/checkout@v2 |
| 67 | + - name: Set IMAGE_TAG as env |
| 68 | + run: | |
| 69 | + IMAGE_TAG=$(git rev-parse --short=7 ${{ github.sha }}) |
| 70 | + echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV |
| 71 | + - name: Install kubectl |
| 72 | + uses: azure/setup-kubectl@v1 |
| 73 | + - name: Configure AWS credentials |
| 74 | + uses: aws-actions/configure-aws-credentials@v1 |
| 75 | + with: |
| 76 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 77 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 78 | + aws-region: ${{ env.region }} |
| 79 | + - name: Setup binaries(aws-cli/kustomize/iam-authenticator) |
| 80 | + uses: ./.github/actions/setup-aws-kustomize |
| 81 | + with: |
| 82 | + cluster-authentication-role-arn: ${{ env.cluster-authentication-role-arn }} |
| 83 | + cluster-name: ${{ env.cluster-name }} |
| 84 | + region: ${{ env.region }} |
| 85 | + - name: Check Namespace |
| 86 | + run: | |
| 87 | + DEPLOYMENT=${{ env.repo }} |
| 88 | + NAMESPACE=${{ env.namespace }} |
| 89 | + kubectl create namespace $NAMESPACE || echo "Namespace already exists" |
| 90 | + - name: Database migration |
| 91 | + uses: ./.github/actions/db-migration |
| 92 | + with: |
| 93 | + namespace: ${{ env.namespace }} |
| 94 | + repository-name: ${{ env.repo }} |
| 95 | + - name: Deploy image |
| 96 | + uses: ./.github/actions/deploy |
| 97 | + with: |
| 98 | + namespace: ${{ env.namespace }} |
| 99 | + repository-name: ${{ env.repo }} |
| 100 | + image-tag: ${{ env.IMAGE_TAG }} |
| 101 | + docker-host: ${{ env.accountId }}.dkr.ecr.${{ env.region }}.amazonaws.com |
| 102 | + region: ${{ env.region }} |
| 103 | + environment-overlay: ${{ env.environment-overlay }} |
| 104 | + integration-test: |
| 105 | + needs: [staging-deploy] |
| 106 | + runs-on: ubuntu-latest |
| 107 | + name: integration-test |
| 108 | + steps: |
| 109 | + ## Example of smoke test against staging env before deploying to production |
| 110 | + ## To be enhanced to more sophisicated checks |
| 111 | + - run: echo "TEST_RESPONSE_COD=$(curl -o /dev/null -s -w \"%{http_code}\" https://<% index .Params `stagingBackendSubdomain` %><% index .Params `stagingHostRoot` %>)" >> $GITHUB_ENV |
| 112 | + - if: env.TEST_RESPONSE_COD >= 400 |
| 113 | + run: exit 1 |
| 114 | + production-deploy: |
| 115 | + needs: [build, integration-test] |
| 116 | + runs-on: ubuntu-latest |
| 117 | + name: production-deploy |
| 118 | + env: |
| 119 | + environment-overlay: production |
| 120 | + cluster-name: "<% .Name %>-prod-<% index .Params `region` %>" |
| 121 | + cluster-authentication-role-arn: "arn:aws:iam::<% index .Params `accountId` %>:role/<% .Name %>-kubernetes-deployer-prod" |
| 122 | + steps: |
| 123 | + - uses: actions/checkout@v2 |
| 124 | + - name: Set IMAGE_TAG as env |
| 125 | + run: | |
| 126 | + IMAGE_TAG=$(git rev-parse --short=7 ${{ github.sha }}) |
| 127 | + echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV |
| 128 | + - name: Install kubectl |
| 129 | + uses: azure/setup-kubectl@v1 |
| 130 | + - name: Configure AWS credentials |
| 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: Setup binaries(aws-cli/kustomize/iam-authenticator) |
| 137 | + uses: ./.github/actions/setup-aws-kustomize |
| 138 | + with: |
| 139 | + cluster-authentication-role-arn: ${{ env.cluster-authentication-role-arn }} |
| 140 | + cluster-name: ${{ env.cluster-name }} |
| 141 | + region: ${{ env.region }} |
| 142 | + - name: Check Namespace |
| 143 | + run: | |
| 144 | + DEPLOYMENT=${{ env.repo }} |
| 145 | + NAMESPACE=${{ env.namespace }} |
| 146 | + kubectl create namespace $NAMESPACE || echo "Namespace already exists" |
| 147 | + - name: Database migration |
| 148 | + uses: ./.github/actions/db-migration |
| 149 | + with: |
| 150 | + namespace: ${{ env.namespace }} |
| 151 | + repository-name: ${{ env.repo }} |
| 152 | + - name: Deploy image |
| 153 | + uses: ./.github/actions/deploy |
| 154 | + with: |
| 155 | + namespace: ${{ env.namespace }} |
| 156 | + repository-name: ${{ env.repo }} |
| 157 | + image-tag: ${{ env.IMAGE_TAG }} |
| 158 | + docker-host: ${{ env.accountId }}.dkr.ecr.${{ env.region }}.amazonaws.com |
| 159 | + region: ${{ env.region }} |
| 160 | + environment-overlay: ${{ env.environment-overlay }} |
0 commit comments