|
1 | 1 | name: Run tests and apply terraform changes for current branch |
2 | 2 |
|
| 3 | +concurrency: |
| 4 | + group: deploy-terraform |
| 5 | + cancel-in-progress: false |
| 6 | + |
3 | 7 | on: |
4 | 8 | push: |
5 | 9 | branches: [dev, staging] |
6 | 10 |
|
| 11 | +permissions: |
| 12 | + id-token: write |
| 13 | + contents: read |
| 14 | + pull-requests: read |
| 15 | + actions: read |
| 16 | + |
7 | 17 | jobs: |
8 | | - build_dev: |
| 18 | + build_eks_cluster: |
9 | 19 | runs-on: ubuntu-latest |
10 | 20 |
|
11 | 21 | env: |
12 | | - ENV: ${{ github.ref_name }} |
13 | | - AWS_ACCESS_KEY_ID: >- |
14 | | - ${{ github.ref_name == 'production' && secrets.aws_key_production || |
15 | | - github.ref_name == 'staging' && secrets.aws_key_staging || |
16 | | - secrets.aws_key_dev }} |
17 | | - AWS_SECRET_ACCESS_KEY: >- |
18 | | - ${{ github.ref_name == 'production' && secrets.aws_secret_production || |
19 | | - github.ref_name == 'staging' && secrets.aws_secret_staging || |
20 | | - secrets.aws_secret_dev }} |
21 | | - AWS_REGION: >- |
22 | | - ${{ github.ref_name == 'production' && secrets.aws_region_production || |
23 | | - github.ref_name == 'staging' && secrets.aws_region_staging || |
24 | | - secrets.aws_region_dev }} |
25 | | - TF_VAR_cloudflare_api_key: ${{ secrets.cloudflare_api_key }} |
26 | | - TF_VAR_cloudflare_email: ${{ secrets.cloudflare_email }} |
27 | | - TF_VAR_sparkpost_api_key: ${{ secrets.sparkpost_api_key }} |
| 22 | + ENV: ${{ github.ref_name }} |
| 23 | + AWS_ACCESS_KEY_ID: >- |
| 24 | + ${{ github.ref_name == 'production' && secrets.aws_key_production || |
| 25 | + github.ref_name == 'staging' && secrets.aws_key_staging || |
| 26 | + secrets.aws_key_dev }} |
| 27 | + AWS_SECRET_ACCESS_KEY: >- |
| 28 | + ${{ github.ref_name == 'production' && secrets.aws_secret_production || |
| 29 | + github.ref_name == 'staging' && secrets.aws_secret_staging || |
| 30 | + secrets.aws_secret_dev }} |
| 31 | + AWS_REGION: >- |
| 32 | + ${{ github.ref_name == 'production' && secrets.aws_region_production || |
| 33 | + github.ref_name == 'staging' && secrets.aws_region_staging || |
| 34 | + secrets.aws_region_dev }} |
| 35 | + TF_VAR_cloudflare_api_key: ${{ secrets.cloudflare_api_key }} |
| 36 | + TF_VAR_cloudflare_email: ${{ secrets.cloudflare_email }} |
| 37 | + TF_VAR_sparkpost_api_key: ${{ secrets.sparkpost_api_key }} |
28 | 38 |
|
29 | 39 | steps: |
30 | | - - uses: actions/checkout@v1 |
| 40 | + - uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: Setup terraform |
| 43 | + uses: hashicorp/setup-terraform@v3 |
| 44 | + with: |
| 45 | + terraform_version: 1.3.6 |
31 | 46 |
|
32 | 47 | - name: TF Init |
33 | | - run: ./scripts/infra -chdir=terraform init -backend-config=vars/backend-$ENV.tfvars |
| 48 | + run: terraform -chdir=terraform init -backend-config=vars/backend-$ENV.tfvars |
| 49 | + |
| 50 | + #- name: TF Plan |
| 51 | + # run: | |
| 52 | + # terraform -chdir=terraform plan -var-file=vars/terraform-$ENV.tfvars \ |
| 53 | + # -var "cloudflare_api_key=${TF_VAR_cloudflare_api_key}" \ |
| 54 | + # -var "cloudflare_email=${TF_VAR_cloudflare_email}" \ |
| 55 | + # -var "sparkpost_api_key=${TF_VAR_sparkpost_api_key}" \ |
| 56 | + # -out tf.plan |
| 57 | + |
| 58 | + - name: Get PR Number |
| 59 | + uses: jwalton/gh-find-current-pr@master |
| 60 | + id: findpr |
| 61 | + with: |
| 62 | + state: closed |
| 63 | + |
| 64 | + - name: Download TF EKS Cluster Plan |
| 65 | + uses: dawidd6/action-download-artifact@v3 |
| 66 | + with: |
| 67 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + workflow: terraform_plan.yaml |
| 69 | + pr: ${{ steps.findpr.outputs.pr }} |
| 70 | + name: tf_eks-${{ steps.findpr.outputs.pr }}.plan |
| 71 | + path: terraform/ |
| 72 | + check_artifacts: true |
34 | 73 |
|
35 | | - - name: TF Plan |
36 | | - run: | |
37 | | - ./scripts/infra -chdir=terraform plan -var-file=vars/terraform-$ENV.tfvars \ |
38 | | - -var "cloudflare_api_key=${TF_VAR_cloudflare_api_key}" \ |
39 | | - -var "cloudflare_email=${TF_VAR_cloudflare_email}" \ |
40 | | - -var "sparkpost_api_key=${TF_VAR_sparkpost_api_key}" |
41 | 74 | - name: TF Apply |
42 | 75 | run: | |
43 | | - ./scripts/infra -chdir=terraform apply -auto-approve -var-file=vars/terraform-$ENV.tfvars \ |
44 | | - -var "cloudflare_api_key=${TF_VAR_cloudflare_api_key}" \ |
45 | | - -var "cloudflare_email=${TF_VAR_cloudflare_email}" \ |
46 | | - -var "sparkpost_api_key=${TF_VAR_sparkpost_api_key}" |
| 76 | + terraform -chdir=terraform apply tf_eks-${{ steps.findpr.outputs.pr }}.plan |
| 77 | +
|
| 78 | + build_k8s_infra: |
| 79 | + runs-on: ubuntu-latest |
| 80 | + needs: build_eks_cluster |
| 81 | + |
| 82 | + env: |
| 83 | + ENV: ${{ github.ref_name }} |
| 84 | + AWS_ROLE: >- |
| 85 | + ${{ github.base_ref == 'production' && 'TBD' || |
| 86 | + github.base_ref == 'staging' && 'TBD' || |
| 87 | + 'arn:aws:iam::842534099497:role/wri-api-dev-githubactions-role' }} |
| 88 | + AWS_REGION: >- |
| 89 | + ${{ github.ref_name == 'production' && secrets.aws_region_production || |
| 90 | + github.ref_name == 'staging' && secrets.aws_region_staging || |
| 91 | + secrets.aws_region_dev }} |
| 92 | + TF_VAR_cloudflare_api_key: ${{ secrets.cloudflare_api_key }} |
| 93 | + TF_VAR_cloudflare_email: ${{ secrets.cloudflare_email }} |
| 94 | + TF_VAR_sparkpost_api_key: ${{ secrets.sparkpost_api_key }} |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Checkout repository |
| 98 | + uses: actions/checkout@v4 |
| 99 | + |
| 100 | + - name: Configure AWS Credentials |
| 101 | + uses: aws-actions/configure-aws-credentials@main |
| 102 | + with: |
| 103 | + role-to-assume: ${{ env.AWS_ROLE }} |
| 104 | + aws-region: ${{ env.AWS_REGION }} |
| 105 | + |
| 106 | + - name: Setup terraform |
| 107 | + uses: hashicorp/setup-terraform@v3 |
| 108 | + with: |
| 109 | + terraform_version: 1.3.6 |
| 110 | + |
| 111 | + - name: Configure Kubeconfig |
| 112 | + run: aws eks update-kubeconfig --region us-east-1 --name core-k8s-cluster-$ENV |
| 113 | + |
| 114 | + - name: TF Init K8s Infra |
| 115 | + run: terraform -chdir=terraform-k8s-infrastructure init -backend-config=vars/backend-$ENV.tfvars |
| 116 | + |
| 117 | + - name: Get PR Number |
| 118 | + uses: jwalton/gh-find-current-pr@master |
| 119 | + id: findpr |
| 120 | + with: |
| 121 | + state: closed |
| 122 | + |
| 123 | + - name: Download TF k8s Infra Plan |
| 124 | + uses: dawidd6/action-download-artifact@v3 |
| 125 | + with: |
| 126 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + workflow: terraform_plan.yaml |
| 128 | + pr: ${{ steps.findpr.outputs.pr }} |
| 129 | + name: tf_k8s_infra-${{ steps.findpr.outputs.pr }}.plan |
| 130 | + path: terraform-k8s-infrastructure/ |
| 131 | + check_artifacts: true |
| 132 | + |
| 133 | + - name: TF Apply |
| 134 | + run: | |
| 135 | + terraform -chdir=terraform-k8s-infrastructure apply \ |
| 136 | + tf_k8s_infra-${{ steps.findpr.outputs.pr }}.plan |
0 commit comments