-
Notifications
You must be signed in to change notification settings - Fork 0
287 lines (228 loc) · 9.17 KB
/
migrate-environments.yml
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
name: Migrate environments
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
migrate:
runs-on: ubuntu-latest
steps:
- name: Get Deployment Environments from Source Repo
id: get-envs
uses: actions/github-script@v7
with:
result-encoding: json
script: |
const response = await github.rest.repos.getAllEnvironments({
owner: context.repo.owner,
repo: context.repo.repo,
});
console.log(response.data);
return response.data;
- name: Create Deployment Environments in Target Repo
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_PAT }}
script: |
const environments = ${{ steps.get-envs.outputs.result }};
for (const env of environments.environments) {
let wait_timer;
if (env.protection_rules) {
console.log(env.protection_rules);
env.protection_rules.forEach((rule) => {
if (rule.type === 'wait_timer') {
wait_timer = rule.wait_timer;
} else if (rule.type === 'required_reviewers'){
console.log(rule.required_reviewers);
}
});
}
env.wait_timer = wait_timer;
const protected_branches = env.deployment_branch_policy ? env.deployment_branch_policy.protected_branches : null;
const custom_branch_policies = env.deployment_branch_policy ? env.deployment_branch_policy.custom_branch_policies : null;
await github.rest.repos.createOrUpdateEnvironment({
owner: context.repo.owner,
repo: 'calvin-test',
environment_name: env.name,
deployment_branch_policy: env.deployment_branch_policy ? {
protected_branches: protected_branches,
custom_branch_policies: custom_branch_policies,
} : null,
wait_timer: env.wait_timer,
});
}
- name: Gather Environment Secrets
id: get-sec
uses: actions/github-script@v7
with:
result-encoding: json
github-token: ${{ secrets.GH_PAT }}
script: |
const response = ${{ steps.get-envs.outputs.result }};
let envs = [];
for (const env of response.environments) {
let envObj = {
name: env.name,
secrets: [],
key_id: '',
key: '',
};
const repoID = await github.rest.repos.get({
owner: 'liatrio-enterprise',
repo: 'environment-migration-test',
});
console.log(repoID.data.id);
const secretsResponse = await github.rest.actions.listEnvironmentSecrets({
repository_id: repoID.data.id,
environment_name: env.name
});
const keyResponse = await github.rest.actions.getEnvironmentPublicKey({
repository_id: repoID.data.id,
environment_name: env.name,
});
envObj.key_id = keyResponse.data.key_id;
envObj.key = keyResponse.data.key;
console.log(JSON.stringify(secretsResponse));
console.log(secretsResponse.data.secrets);
console.log(keyResponse.data);
for (const secret of secretsResponse.data.secrets) {
console.log(secret.name);
// Get the value of the secret
const secretValue = await github.rest.actions.getEnvironmentSecret({
repository_id: repoID.data.id,
environment_name: env.name,
secret_name: secret.name,
});
envObj.secrets.push({
name: secret.name,
value: secretValue.data
});
console.log(secretValue.data);
envs.push(envObj);
}
}
console.log(JSON.stringify(envs));
return envs;
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Encrypt secrets
id: encrypt
run: node encrypt.js
env:
ENVS: ${{ steps.get-sec.outputs.result }}
- name: Migrate Environment Secrets
env:
ENVS: ${{ steps.encrypt.outputs.encrypted_output }}
uses: actions/github-script@v7
with:
result-encoding: json
github-token: ${{ secrets.GH_PAT }}
script: |
// const envs = ${{ steps.get-sec.outputs.result }};
// console.log(JSON.stringify(envs));
const envs = JSON.parse(process.env.ENVS);
console.log(JSON.stringify(process.env.ENVS));
const repoID = await github.rest.repos.get({
owner: 'liatrio-enterprise',
repo: 'calvin-test',
});
// const temp = 'S0LZjwp5CEf7SDF5YUKUuqnUlQ75pNMPMmtHaLTLeg96OOotB7hfAlLpi1eB1sBZz/REjQ==';
for (const env of envs) {
for (const sec of env.secrets){
// Migrate the secret to the target repository
const secretResponse = await github.rest.actions.createOrUpdateEnvironmentSecret({
repository_id: repoID.data.id,
environment_name: env.name,
secret_name: sec.name,
encrypted_value: env.encrypted,
key_id: env.key_id,
});
console.log(secretResponse.data);
}
}
- name: Generate Issues for Each Environment
uses: actions/github-script@v7
with:
result-encoding: json
github-token: ${{ secrets.GH_PAT }}
script: |
const envs = ${{ steps.get-sec.outputs.result }};
for (const env of envs) {
let issueBody = `Please update the following secrets for the ${env.name} environment:\n`;
for (const sec of env.secrets) {
issueBody += `- [ ] ${sec.name}\n`;
}
issueBody += `\n\nOnce the secrets have been updated, please close this issue.`;
const issueResut = await github.rest.issues.create({
owner: 'liatrio-enterprise',
repo: 'calvin-test',
title: 'Update secrets for environment: ' + env.name,
body: issueBody,
});
console.log(issueResut.data);
}
- name: Gather Environment Variables
id: get-vars
uses: actions/github-script@v7
with:
result-encoding: json
github-token: ${{ secrets.GH_PAT }}
script: |
const response = ${{ steps.get-envs.outputs.result }};
let envs = [];
for (const env of response.environments) {
let envObj = {
name: env.name,
vars: []
};
const repoID = await github.rest.repos.get({
owner: 'liatrio-enterprise',
repo: 'environment-migration-test',
});
console.log(repoID.data.id);
const variablesResponse = await github.rest.actions.listEnvironmentVariables({
repository_id: repoID.data.id,
environment_name: env.name,
});
console.log(JSON.stringify(variablesResponse));
console.log(variablesResponse.data.variables);
for (const variable of variablesResponse.data.variables) {
console.log(variable.name);
envObj.vars.push({
name: variable.name,
value: variable.value
});
envs.push(envObj);
}
}
console.log(JSON.stringify(envs));
return envs;
- name: Migrate Environment Variables
uses: actions/github-script@v7
with:
result-encoding: json
github-token: ${{ secrets.GH_PAT }}
script: |
const envs = ${{ steps.get-vars.outputs.result }};
console.log(JSON.stringify(envs));
for (const env of envs) {
const repoID = await github.rest.repos.get({
owner: 'liatrio-enterprise',
repo: 'calvin-test',
});
for (const variable of env.vars){
// Migrate the variable to the target repository
const variableResponse = await github.rest.actions.createEnvironmentVariable({
repository_id: repoID.data.id,
environment_name: env.name,
name: variable.name,
value: variable.value,
});
console.log(variableResponse.data);
}
}