|
| 1 | +name: 'Deployment' |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - <change-me-to-main> |
| 7 | + pull_request: |
| 8 | + branches: [<change-me-to-main>] |
| 9 | +defaults: |
| 10 | + run: |
| 11 | + shell: bash |
| 12 | + |
| 13 | +permissions: |
| 14 | + id-token: write |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: ${{ github.head_ref || github.ref_name || github.run_id }} |
| 18 | +jobs: |
| 19 | + terraform: |
| 20 | + name: ${{matrix.runner}} - dev |
| 21 | + runs-on: ['${{ matrix.runner }}'] |
| 22 | + strategy: |
| 23 | + max-parallel: 1 |
| 24 | + matrix: |
| 25 | + include: |
| 26 | + - environment: dev |
| 27 | + runner: ubuntu-latest |
| 28 | + env: |
| 29 | + AWS_DEFAULT_REGION: us-east-1 |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v2 |
| 32 | + |
| 33 | + - name: Configure AWS credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@v1 |
| 35 | + with: |
| 36 | + role-to-assume: ${{ secrets.OIDC_IAM_ROLE_ARN }} |
| 37 | + aws-region: us-east-1 |
| 38 | + |
| 39 | + - name: Login to Amazon ECR |
| 40 | + id: login-ecr |
| 41 | + uses: aws-actions/amazon-ecr-login@v1 |
| 42 | + |
| 43 | + - name: Build, tag, and push the web image to Amazon ECR |
| 44 | + id: build-web-image |
| 45 | + env: |
| 46 | + ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} |
| 47 | + ECR_REPOSITORY: ${{ secrets.WEB_ECR_REPOSITORY }} |
| 48 | + OKTA_DOMAIN: ${{ secrets.OKTA_DOMAIN }} |
| 49 | + TEALIUM_ENV: 'dev' |
| 50 | + LD_CLIENT_ID: ${{ secrets.LD_CLIENT_ID }} |
| 51 | + TEALIUM_TAG: ${{ secrets.TEALIUM_TAG }} |
| 52 | + API_URL: ${{ secrets.API_URL }} |
| 53 | + |
| 54 | + run: | |
| 55 | + # Build a docker container and push it to ECR |
| 56 | + export IMAGE_TAG=$(git rev-parse --short "$GITHUB_SHA") |
| 57 | + docker build --quiet -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg LD_CLIENT_ID=$LD_CLIENT_ID --build-arg TEALIUM_ENV=$TEALIUM_ENV --build-arg TEALIUM_TAG=TEALIUM_TAG --build-arg OKTA_DOMAIN=$OKTA_DOMAIN -f web/DockerfileECS . |
| 58 | + echo "Pushing image to ECR..." |
| 59 | + export WEB_IMAGE=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 60 | + docker push $WEB_IMAGE |
| 61 | + echo "::set-output name=image::${WEB_IMAGE}" |
| 62 | +
|
| 63 | + - name: Build, tag, and push the api image to Amazon ECR |
| 64 | + id: build-api-image |
| 65 | + env: |
| 66 | + ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} |
| 67 | + ECR_REPOSITORY: ${{ secrets.API_ECR_REPOSITORY }} |
| 68 | + run: | |
| 69 | + # Build a docker container and push it to ECR |
| 70 | + export IMAGE_TAG=$(git rev-parse --short "$GITHUB_SHA") |
| 71 | + docker build --quiet -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f api/DockerfileECS . |
| 72 | + echo "Pushing image to ECR..." |
| 73 | + export API_IMAGE=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 74 | + docker push $API_IMAGE |
| 75 | + echo "::set-output name=image::${API_IMAGE}" |
| 76 | +
|
| 77 | + - uses: hashicorp/setup-terraform@v1 |
| 78 | + with: |
| 79 | + terraform_wrapper: false |
| 80 | + - name: Terraform Init |
| 81 | + id: init |
| 82 | + working-directory: terraform/greenfield/ecs |
| 83 | + run: | |
| 84 | + rm -rf .terraform |
| 85 | + terraform init -backend-config=envs/dev/backend.tfvars -upgrade=true -no-color -input=false |
| 86 | + - name: Terraform Plan |
| 87 | + id: plan |
| 88 | + working-directory: terraform/greenfield/ecs |
| 89 | + run: | |
| 90 | + terraform plan -input=false -var-file=envs/dev/inputs.tfvars -var "web_image=$WEB_IMAGE" -var "aws_account=$AWS_ACCOUNT" -var "api_image=$API_IMAGE" -no-color |
| 91 | + env: |
| 92 | + AWS_ACCOUNT: ${{ secrets.AWS_ACCOUNT }} |
| 93 | + WEB_IMAGE: ${{steps.build-web-image.outputs.image}} |
| 94 | + API_IMAGE: ${{steps.build-api-image.outputs.image}} |
| 95 | + - name: Terraform Apply |
| 96 | + if: github.ref == 'refs/heads/gf-ecs' |
| 97 | + id: apply |
| 98 | + working-directory: terraform/greenfield/ecs |
| 99 | + run: | |
| 100 | + terraform apply -auto-approve -input=false -var-file=envs/dev/inputs.tfvars -var "aws_account=$AWS_ACCOUNT" -var "web_image=$WEB_IMAGE" -var "api_image=$API_IMAGE" |
| 101 | + env: |
| 102 | + AWS_ACCOUNT: ${{ secrets.AWS_ACCOUNT }} |
| 103 | + WEB_IMAGE: ${{steps.build-web-image.outputs.image}} |
| 104 | + API_IMAGE: ${{steps.build-api-image.outputs.image}} |
| 105 | + - name: Terraform destroy |
| 106 | + if: github.ref == 'refs/heads/destroy' |
| 107 | + id: destroy |
| 108 | + working-directory: terraform/greenfiel/ecs |
| 109 | + run: | |
| 110 | + terraform destroy -auto-approve -input=false -var-file=envs/dev/inputs.tfvars |
| 111 | + env: |
| 112 | + WEB_IMAGE: ${{steps.build-web-image.outputs.image}} |
| 113 | + API_IMAGE: ${{steps.build-api-image.outputs.image}} |
0 commit comments