-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-organization.terraform-cloud.github.yml
More file actions
191 lines (167 loc) · 6.41 KB
/
setup-organization.terraform-cloud.github.yml
File metadata and controls
191 lines (167 loc) · 6.41 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
name: 'Setup Organization'
on:
workflow_dispatch:
create:
jobs:
apply-terraform:
runs-on: ubuntu-latest
steps:
- name: Get necessary variables
id: repo_org
env:
GITHUB_TOKEN: ${{ secrets.SETUP_TOKEN }}
run: |
org_name=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 1)
echo "Organization: $org_name"
echo "org_name=$org_name" >> $GITHUB_ENV
org_owner_username=$(curl -s --request GET \
--url https://api.github.com/orgs/$org_name/members \
--header "Authorization: token $GITHUB_TOKEN" \
--header 'Content-Type: application/vnd.api+json' \
| jq '.[] | .login' \
| head -n 1 \
| tr -d '"')
org_owner_email=$(curl -s --request GET \
--url https://api.github.com/users/$org_owner_username \
--header "Authorization: token $GITHUB_TOKEN" \
--header 'Content-Type: application/vnd.api+json' \
| jq -r '.email')
echo "Organization owner login: $org_owner_username"
echo "Organization owner email: $org_owner_email"
echo "org_owner_email=$org_owner_email" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v3
with:
token: ${{ secrets.SETUP_TOKEN }}
- name: Replace ORGANIZATION_NAME with the actual organization name
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "ORGANIZATION_NAME"
replace: ${{ env.org_name }}
include: "**"
regex: false
- name: Replace USER_EMAIL with the actual user email
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "USER_EMAIL"
replace: ${{ env.org_owner_email }}
include: "**"
regex: false
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
cli_config_credentials_token: ${{ secrets.TF_CLOUD_PERSONAL_API_TOKEN }}
- name: Terraform Format
id: fmt
run: terraform fmt -check
- name: Terraform Init
id: init
run: terraform init
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
env:
TFE_TOKEN: ${{ secrets.TF_CLOUD_PERSONAL_API_TOKEN }}
run: |
terraform apply -auto-approve -input=false -var="github_token=${{ secrets.SETUP_TOKEN }}"
remove-terraform-cloud-personal-token:
runs-on: ubuntu-latest
needs:
- apply-terraform
steps:
- name: Remove TF_CLOUD_PERSONAL_API_TOKEN from organization secrets (a personal token has too many rights on TF Cloud)
env:
GITHUB_TOKEN: ${{ secrets.SETUP_TOKEN }}
run: |
org_name=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 1)
echo "Removing TF_CLOUD_PERSONAL_API_TOKEN from organization secrets..."
#curl -X DELETE \
# -H "Accept: application/vnd.github+json" \
# -H "Authorization: token $GITHUB_TOKEN" \
# "https://api.github.com/orgs/$org_name/actions/secrets/TF_CLOUD_PERSONAL_API_TOKEN"
#echo "TF_CLOUD_PERSONAL_API_TOKEN has been removed from organization secrets."
create-repo:
runs-on: ubuntu-latest
needs:
- apply-terraform
steps:
- name: Get current repository organization
id: repo_org
run: |
org_name=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 1)
echo "Organization: $org_name"
echo "org_name=$org_name" >> $GITHUB_ENV
- name: Get user information
id: user_info
run: |
response=$(curl -s -H "Authorization: token ${{ secrets.SETUP_TOKEN }}" https://api.github.com/user)
user_name=$(echo "$response" | jq -r '.name')
user_email=$(echo "$response" | jq -r '.email')
echo "user_name=$user_name" >> $GITHUB_ENV
echo "user_email=$user_email" >> $GITHUB_ENV
- name: Create a new repository from a template
env:
GITHUB_TOKEN: ${{ secrets.SETUP_TOKEN }}
run: |
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/codingones/dotgithub/generate \
-d '{
"owner": "${{ env.org_name }}",
"name": ".github-infrastructure-to-register-with-terraform",
"description": "This is a transient repository that will deploy your .github",
"include_all_branches": false,
"private": false
}'
- name: Checkout repository
uses: actions/checkout@v3
with:
repository: ${{ env.org_name }}/.github-infrastructure-to-register-with-terraform
token: ${{ secrets.SETUP_TOKEN }}
- name: Replace ORGANIZATION_NAME with the actual organization name
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "ORGANIZATION_NAME"
replace: ${{ env.org_name }}
include: "**"
regex: false
- name: Replace USER_NAME with the actual user name
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "USER_NAME"
replace: ${{ env.user_name }}
include: "**"
regex: false
- name: Replace USER_EMAIL with the actual user email
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "USER_EMAIL"
replace: ${{ env.user_email }}
include: "**"
regex: false
- name: Commit and push changes
run: |
git config user.name "${{ env.user_name }}"
git config user.email "${{ env.user_email }}"
git add .
git diff --quiet && git diff --staged --quiet || git commit -m "Replace ORGANIZATION_NAME, USER_NAME, USER_EMAIL with the actual values"
git push
delete-repo:
runs-on: ubuntu-latest
needs:
- create-repo
- remove-terraform-cloud-personal-token
steps:
- name: Delete this repository
env:
GITHUB_TOKEN: ${{ secrets.SETUP_TOKEN }}
run: |
echo "Deleting the current repository..."
curl -X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}"