Add job for cleaning environments (#3359)
* add job for cleaning environments * Fix job for cleaning environment * Update .github/workflows/cleanEnvironments.js Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com> --------- Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com>
This commit is contained in:
parent
593867f0c5
commit
054cd415cf
3 changed files with 147 additions and 1 deletions
33
.github/workflows/clean-envs.yml
vendored
Normal file
33
.github/workflows/clean-envs.yml
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
name: Clean environments
|
||||
on:
|
||||
schedule:
|
||||
- cron: "0 0 * * 0"
|
||||
workflow_dispatch:
|
||||
branches: ["main"]
|
||||
|
||||
jobs:
|
||||
cleanEnvironments:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
TOKEN: ${{ secrets.CLOUD_ACCESS_TOKEN }}
|
||||
SNAPSHOT: 9oUFxT7f
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version-file: ".nvmrc"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
cd .github/workflows
|
||||
npm ci
|
||||
|
||||
- name: clean environments
|
||||
id: clean-environments
|
||||
run: |
|
||||
node .github/workflows/cleanEnvironments.js \
|
||||
--token "$TOKEN" \
|
||||
--snapshot "$SNAPSHOT"
|
113
.github/workflows/cleanEnvironments.js
vendored
Normal file
113
.github/workflows/cleanEnvironments.js
vendored
Normal file
|
@ -0,0 +1,113 @@
|
|||
const { Command } = require("commander");
|
||||
const fetch = require("node-fetch");
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.name("cleanEnvironments")
|
||||
.description("Clean environments")
|
||||
.option("--token <token>", "token fo login to cloud")
|
||||
.option("--snapshot <snapshot>", "snapshot to revert to")
|
||||
.action(async options => {
|
||||
const token = options.token;
|
||||
const snapshot = options.snapshot;
|
||||
const environmentsToClean = await getEnvironmentsForReleaseTesting(token);
|
||||
environmentsToClean.forEach(environment => {
|
||||
cleanEnvironment(environment, snapshot, token);
|
||||
});
|
||||
})
|
||||
.parse();
|
||||
|
||||
async function getEnvironmentsForReleaseTesting(token) {
|
||||
const environments = await getEnvironments(token);
|
||||
const environmentsForReleaseTesting = environments.filter(environment => {
|
||||
return (
|
||||
environment.domain.match(/^v\d*.staging/) ||
|
||||
environment.domain == "master.staging.saleor.cloud"
|
||||
);
|
||||
});
|
||||
return environmentsForReleaseTesting;
|
||||
}
|
||||
|
||||
async function getEnvironments(token) {
|
||||
const response = await fetch(
|
||||
`https://staging-cloud.saleor.io/api/organizations/saleor/environments/`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Token ${token}`,
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
},
|
||||
},
|
||||
);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
async function cleanEnvironment(environment, snapshot, token) {
|
||||
const response = await fetch(
|
||||
`https://staging-cloud.saleor.io/api/organizations/saleor/environments/${environment.key}/restore/`,
|
||||
{
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ restore_from: snapshot }),
|
||||
headers: {
|
||||
Authorization: `Token ${token}`,
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
},
|
||||
},
|
||||
);
|
||||
const responseInJson = await response.json();
|
||||
if (
|
||||
responseInJson.non_field_errors
|
||||
? responseInJson.non_field_errors
|
||||
: responseInJson.__all__
|
||||
) {
|
||||
console.warn(
|
||||
`${environment.name}: ${
|
||||
responseInJson.non_field_errors
|
||||
? responseInJson.non_field_errors
|
||||
: responseInJson.__all__
|
||||
}`,
|
||||
);
|
||||
} else {
|
||||
await waitUntilTaskInProgress(responseInJson.task_id, environment.name);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitUntilTaskInProgress(taskId, environment) {
|
||||
const throwErrorAfterTimeout = setTimeout(function () {
|
||||
throw new Error("Environment didn't upgrade after 30 minutes");
|
||||
}, 120000);
|
||||
|
||||
while (true) {
|
||||
const response = await fetch(
|
||||
`https://staging-cloud.saleor.io/api/service/task-status/${taskId}/`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
},
|
||||
},
|
||||
);
|
||||
const responseInJson = await response.json();
|
||||
switch (responseInJson.status) {
|
||||
case "PENDING":
|
||||
case "IN_PROGRESS":
|
||||
await new Promise(resolve => setTimeout(resolve, 10000));
|
||||
break;
|
||||
case "SUCCEEDED":
|
||||
console.log(
|
||||
`${environment}: Job ended with status - ${responseInJson.status} - ${responseInJson.job_name}`,
|
||||
);
|
||||
clearTimeout(throwErrorAfterTimeout);
|
||||
return responseInJson.status;
|
||||
default:
|
||||
clearTimeout(throwErrorAfterTimeout);
|
||||
throw console.warn(
|
||||
`${environment}: Job ended with status - ${responseInJson.status} - ${responseInJson.job_name}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
2
.github/workflows/tests-nightly.yml
vendored
2
.github/workflows/tests-nightly.yml
vendored
|
@ -75,7 +75,7 @@ jobs:
|
|||
env:
|
||||
CI: true
|
||||
SALEOR_CLI_ENV: staging
|
||||
run: npx saleor backup restore 9S4PckyI --skip-webhooks-update
|
||||
run: npx saleor backup restore W5yhqHkw --skip-webhooks-update
|
||||
|
||||
- name: Notify Slack
|
||||
if: ${{ failure() }}
|
||||
|
|
Loading…
Reference in a new issue