-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (95 loc) · 3.43 KB
/
Copy pathapply_terraform.yml
File metadata and controls
110 lines (95 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Apply terraform
on:
workflow_call:
secrets:
PROD_CP_GCP_PROJECT_ID:
required: true
DEV_CP_GCP_PROJECT_ID:
required: true
DEVOPS_ACCESS_REPO_TOKEN:
required: true
PROD_CP_GCP_SVC_GITHUB_RUNNER_KEY_FILE:
required: true
DEV_CP_GCP_SVC_GITHUB_RUNNER_KEY_FILE:
required: true
inputs:
TEST:
type: boolean
default: false
required: false
description: 'A flag to determine whether to apply testing envionment.'
outputs:
TEST_ENDPOINT:
value: ${{ jobs.apply_terraform.outputs.TEST_ENDPOINT }}
description: "The endpoint that used for integration test."
jobs:
apply_terraform:
if: ${{ github.actor != 'dependabot[bot]' }}
runs-on: ubuntu-latest
outputs:
TEST_ENDPOINT: ${{ steps.terraform_output.outputs.test_endpoint }}
steps:
- uses: actions/checkout@main
- name: Export environments
run: |
if [[ ${{ inputs.TEST }} == true ]]; then
REPOSITORY="test-${GITHUB_REPOSITORY##*/}"
else
REPOSITORY=${GITHUB_REPOSITORY##*/}
fi
if [[ "$REF" =~ ^[0-9]+.[0-9]+.[0-9]+$ || $GITHUB_REF_NAME == "master" ]]; then
GCP_ENV=production
GCP_PROJECT_ID=${{ secrets.PROD_CP_GCP_PROJECT_ID }}
INFRA_BRANCH=master
else
GCP_ENV=develop
GCP_PROJECT_ID=${{ secrets.DEV_CP_GCP_PROJECT_ID }}
INFRA_BRANCH=develop
fi
echo "node version: `node -v`"
cat <<- EOF >> ${GITHUB_ENV}
REPOSITORY=${REPOSITORY}
GCP_ENV=${GCP_ENV}
GCP_PROJECT_ID=${GCP_PROJECT_ID}
INFRA_BRANCH=${INFRA_BRANCH}
INFRA_REPO=CoolBitX-Technology/cp-platform-infrastructure
INFRA_DIR=${GITHUB_WORKSPACE}/_infra
EOF
- name: Checkout infra
uses: actions/checkout@main
with:
repository: ${{ env.INFRA_REPO }}
path: ${{ env.INFRA_DIR }}
ref: ${{ env.INFRA_BRANCH }}
token: ${{ secrets.DEVOPS_ACCESS_REPO_TOKEN }}
- name: Authenticate gcloud
if: ${{ env.GCP_ENV == 'production' }}
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.PROD_CP_GCP_SVC_GITHUB_RUNNER_KEY_FILE }}
- name: Authenticate gcloud
if: ${{ env.GCP_ENV == 'develop' }}
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.DEV_CP_GCP_SVC_GITHUB_RUNNER_KEY_FILE }}
- name: Setup gcloud
uses: google-github-actions/setup-gcloud@v2
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.9.5
terraform_wrapper: false
- name: Terraform init
working-directory: ${{ env.INFRA_DIR }}/terraform
run: |
terraform init -backend-config=bucket=${GCP_PROJECT_ID}-terraform-state
- name: Terraform apply
working-directory: ${{ env.INFRA_DIR }}/terraform
run: |
terraform apply -auto-approve -var=env=${GCP_ENV} -var=testing=${{ inputs.TEST }} -target=module.${REPOSITORY}
- name: Terraform output
id: terraform_output
working-directory: ${{ env.INFRA_DIR }}/terraform
run: |
test_endpoint=$(terraform output -raw ${REPOSITORY}-endpoint)
echo "test_endpoint=$test_endpoint" >> "$GITHUB_OUTPUT"
if: ${{ inputs.TEST }}