From 37208ea9bfafdf32101fabf412e00c900ee9ed7b Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 14 Jun 2024 11:57:04 -0700 Subject: [PATCH 1/8] separate workflows to use static branchFilter spec --- .../workflows/teamcity-nightly-sweeper.yaml | 101 ++++++++++++++++++ .../workflows/teamcity-nightly-workflow.yaml | 67 +----------- 2 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 .github/workflows/teamcity-nightly-sweeper.yaml diff --git a/.github/workflows/teamcity-nightly-sweeper.yaml b/.github/workflows/teamcity-nightly-sweeper.yaml new file mode 100644 index 00000000000..094afda46d4 --- /dev/null +++ b/.github/workflows/teamcity-nightly-sweeper.yaml @@ -0,0 +1,101 @@ +name: "TeamCity: Remove branches used for nightly test" + + +# To ensure nightly tests/builds run on the same commit, we checkout and create a new branch from main for TeamCity to run builds on + +on: + workflow_call: + workflow_dispatch: + inputs: + dayThreshold: + default: '3' + schedule: + - cron: '0 14 * * *' # UTC 2PM (-7)-> 7AM PST + +jobs: + rename-TC-nightly-branch: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 + with: + retries: 3 + retry-exempt-status-codes: 400, 401, 403, 404, 422 + script: | + let dateToday = new Date().toJSON().slice(0, 10); + const oldBranchName = "UTC-" + dateToday; + const newBranchName = "UTC-nightly-tests" + dateToday; + + try{ + await github.rest.repos.renameBranch({ + owner: context.repo.owner, + repo: context.repo.repo, + branch: oldBranchName, + new_name: newBranchName, + }) + } catch (error){ + core.setFailed(error + "- Failed to rename branch to be used running tonight\'s tests; branch with name " + oldBranchName + " doesn\'t exist") + } + console.log("Renamed branch " + oldBranchName + " to " + newBranchName) + + sweeping-outdated-branches: + needs: rename-TC-nightly-branch + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 + env: + DAYS_THRESHOLD: ${{ inputs.dayThreshold || '3'}} # this allows the default value to be 3 when triggered on schedule + with: + retries: 3 + retry-exempt-status-codes: 400, 401, 403, 404, 422 + script: | + const { DAYS_THRESHOLD } = process.env + console.log(`Removing nightly-test branches not made in the last ${DAYS_THRESHOLD} days`) + + function dateDifference(dateToday, branchDate){ + dateToday = new Date(dateToday) + branchDate = new Date(branchDate) + return (dateToday - branchDate) / 86_400_000 // calculates the difference in days based on milliseconds + } + + async function branchSweeper(daysThreshold){ + let dateToday = new Date().toJSON().slice(0, 10); + console.log("Today\'s date: ",dateToday); + // grab the list of branches then iterate through the list checking for the difference in days + const branchList = await github.rest.repos.listBranches({ + owner: context.repo.owner, + repo: context.repo.repo, + protected: false + }) + + const filteredBranches = branchList.data.filter( (branch) => { + const branchDate = /^UTC-nightly-\d{4}-\d{2}-\d{2}$/g.exec(branch.name) + return branchDate != null // skips if regex fails (is successful if matches with UTC-nightly-test branch format) + }) + + let branchesToDelete = [] + + for (let i = 0; i < filteredBranches.length; i++) { + const branchName = filteredBranches.at(i).name + const branchDate = /\d{4}-\d{1,2}-\d{1,2}/g.exec(branchName) + if (dateDifference(dateToday, branchDate[0]) >= daysThreshold) { // only happens if difference is greater than or equal to 3, we only want to keep the last 3 night branches + branchesToDelete.push(branchName) + } + } + + console.log("branches to be deleted: " + branchesToDelete) + + for (let i = 0; i < branchesToDelete.length; i++) { + const resp = await github.rest.git.deleteRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: "heads/" + branchesToDelete[i], + }) + if (resp.status == "422"){ + console.error("Branch doesn\'t exist") + } else{ + console.log("Deleted branch: " + branchesToDelete[i]) + } + } + } + + branchSweeper(DAYS_THRESHOLD) \ No newline at end of file diff --git a/.github/workflows/teamcity-nightly-workflow.yaml b/.github/workflows/teamcity-nightly-workflow.yaml index f7060fe4660..63a934ba82a 100644 --- a/.github/workflows/teamcity-nightly-workflow.yaml +++ b/.github/workflows/teamcity-nightly-workflow.yaml @@ -10,7 +10,7 @@ on: dayThreshold: default: '3' schedule: - - cron: '0 4 * * *' + - cron: '0 4 * * *' # UTC 4AM (-7)-> 9PM PST jobs: nigthly-test-branch-creation: @@ -28,7 +28,7 @@ jobs: repo: context.repo.repo, ref: "heads/main" }) - const branchName = "UTC-nightly-tests-" + dateToday; + const branchName = "UTC-" + dateToday; try{ await github.rest.git.createRef({ owner: context.repo.owner, @@ -40,66 +40,3 @@ jobs: core.setFailed(error + "- Failed to create new branch to be used running tonight\'s tests; branch with name " + branchName + " already exists") } console.log("Created Branch: " + branchName) - - sweeping-outdated-branches: - needs: nigthly-test-branch-creation - runs-on: ubuntu-latest - steps: - - uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 - env: - DAYS_THRESHOLD: ${{ inputs.dayThreshold || '3'}} # this allows the default value to be 3 when triggered on schedule - with: - retries: 3 - retry-exempt-status-codes: 400, 401, 403, 404, 422 - script: | - const { DAYS_THRESHOLD } = process.env - console.log(`Removing nightly-test branches not made in the last ${DAYS_THRESHOLD} days`) - - function dateDifference(dateToday, branchDate){ - dateToday = new Date(dateToday) - branchDate = new Date(branchDate) - return (dateToday - branchDate) / 86_400_000 // calculates the difference in days based on milliseconds - } - - async function branchSweeper(daysThreshold){ - let dateToday = new Date().toJSON().slice(0, 10); - console.log("Today\'s date: ",dateToday); - // grab the list of branches then iterate through the list checking for the difference in days - const branchList = await github.rest.repos.listBranches({ - owner: context.repo.owner, - repo: context.repo.repo, - protected: false - }) - - const filteredBranches = branchList.data.filter( (branch) => { - const branchDate = /^UTC-nightly-tests-\d{4}-\d{2}-\d{2}$/g.exec(branch.name) - return branchDate != null // skips if regex fails (is successful if matches with UTC-nightly-test branch format) - }) - - let branchesToDelete = [] - - for (let i = 0; i < filteredBranches.length; i++) { - const branchName = filteredBranches.at(i).name - const branchDate = /\d{4}-\d{1,2}-\d{1,2}/g.exec(branchName) - if (dateDifference(dateToday, branchDate[0]) >= daysThreshold) { // only happens if difference is greater than or equal to 3, we only want to keep the last 3 night branches - branchesToDelete.push(branchName) - } - } - - console.log("branches to be deleted: " + branchesToDelete) - - for (let i = 0; i < branchesToDelete.length; i++) { - const resp = await github.rest.git.deleteRef({ - owner: context.repo.owner, - repo: context.repo.repo, - ref: "heads/" + branchesToDelete[i], - }) - if (resp.status == "422"){ - console.error("Branch doesn\'t exist") - } else{ - console.log("Deleted branch: " + branchesToDelete[i]) - } - } - } - - branchSweeper(DAYS_THRESHOLD) From fd1785a362347e201e3279a84f30c463e7f4024d Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 14 Jun 2024 12:16:47 -0700 Subject: [PATCH 2/8] refinement --- .github/workflows/teamcity-nightly-sweeper.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/teamcity-nightly-sweeper.yaml b/.github/workflows/teamcity-nightly-sweeper.yaml index 094afda46d4..5bd44f3fd30 100644 --- a/.github/workflows/teamcity-nightly-sweeper.yaml +++ b/.github/workflows/teamcity-nightly-sweeper.yaml @@ -1,7 +1,8 @@ name: "TeamCity: Remove branches used for nightly test" -# To ensure nightly tests/builds run on the same commit, we checkout and create a new branch from main for TeamCity to run builds on +# To ensure TeamCity build is triggered by the newly created branch. We prevent their to be only +# one branch that matches the filter (+:UTC-*,-:UTC-nightly-tests*) on: workflow_call: @@ -23,7 +24,7 @@ jobs: script: | let dateToday = new Date().toJSON().slice(0, 10); const oldBranchName = "UTC-" + dateToday; - const newBranchName = "UTC-nightly-tests" + dateToday; + const newBranchName = "UTC-nightly-tests-" + dateToday; try{ await github.rest.repos.renameBranch({ @@ -33,7 +34,7 @@ jobs: new_name: newBranchName, }) } catch (error){ - core.setFailed(error + "- Failed to rename branch to be used running tonight\'s tests; branch with name " + oldBranchName + " doesn\'t exist") + core.setFailed(error + "- Failed to rename branch; branch with name " + oldBranchName + " doesn\'t exist") } console.log("Renamed branch " + oldBranchName + " to " + newBranchName) From e609a763b48bf1cfba70709cd7bc65f8c032c116 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Mon, 17 Jun 2024 11:05:44 -0700 Subject: [PATCH 3/8] update cronjob time --- .github/workflows/teamcity-nightly-workflow.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/teamcity-nightly-workflow.yaml b/.github/workflows/teamcity-nightly-workflow.yaml index 63a934ba82a..40500e583f6 100644 --- a/.github/workflows/teamcity-nightly-workflow.yaml +++ b/.github/workflows/teamcity-nightly-workflow.yaml @@ -10,10 +10,10 @@ on: dayThreshold: default: '3' schedule: - - cron: '0 4 * * *' # UTC 4AM (-7)-> 9PM PST + - cron: '0 3 * * *' # UTC 3AM (-7)-> 8PM PST jobs: - nigthly-test-branch-creation: + nightly-test-branch-creation: runs-on: ubuntu-latest steps: - uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 From eb082b7a1f273d15fc1f722bd3f8a80efc74b7ee Mon Sep 17 00:00:00 2001 From: BBBmau Date: Mon, 17 Jun 2024 11:06:24 -0700 Subject: [PATCH 4/8] update filter for sweeper --- .github/workflows/teamcity-nightly-sweeper.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/teamcity-nightly-sweeper.yaml b/.github/workflows/teamcity-nightly-sweeper.yaml index 5bd44f3fd30..637c52808b7 100644 --- a/.github/workflows/teamcity-nightly-sweeper.yaml +++ b/.github/workflows/teamcity-nightly-sweeper.yaml @@ -69,7 +69,7 @@ jobs: }) const filteredBranches = branchList.data.filter( (branch) => { - const branchDate = /^UTC-nightly-\d{4}-\d{2}-\d{2}$/g.exec(branch.name) + const branchDate = /^UTC-nightly-tests-\d{4}-\d{2}-\d{2}$/g.exec(branch.name) return branchDate != null // skips if regex fails (is successful if matches with UTC-nightly-test branch format) }) From 1d0974444f461190a3dd46a4cc0616c870589e51 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Mon, 17 Jun 2024 11:15:36 -0700 Subject: [PATCH 5/8] add cronjob comment for branch creation --- .github/workflows/teamcity-nightly-workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/teamcity-nightly-workflow.yaml b/.github/workflows/teamcity-nightly-workflow.yaml index 40500e583f6..4ad63a68dc6 100644 --- a/.github/workflows/teamcity-nightly-workflow.yaml +++ b/.github/workflows/teamcity-nightly-workflow.yaml @@ -10,7 +10,7 @@ on: dayThreshold: default: '3' schedule: - - cron: '0 3 * * *' # UTC 3AM (-7)-> 8PM PST + - cron: '0 3 * * *' # 3AM UTC (-7)-> 8PM PST # teamcity builds are triggered @ 4AM UTC jobs: nightly-test-branch-creation: From d41137cda61f448aabae16e6ce83b955a19ef391 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Tue, 18 Jun 2024 06:43:38 -0700 Subject: [PATCH 6/8] match cronjob times --- .github/workflows/teamcity-nightly-sweeper.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/teamcity-nightly-sweeper.yaml b/.github/workflows/teamcity-nightly-sweeper.yaml index 637c52808b7..14ae9912cc1 100644 --- a/.github/workflows/teamcity-nightly-sweeper.yaml +++ b/.github/workflows/teamcity-nightly-sweeper.yaml @@ -11,7 +11,7 @@ on: dayThreshold: default: '3' schedule: - - cron: '0 14 * * *' # UTC 2PM (-7)-> 7AM PST + - cron: '0 9 * * *' # UTC 9AM (-7)-> 2PM PST jobs: rename-TC-nightly-branch: From a65c37bcd9b0f60870deb42c80183d00e98fa5a3 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 21 Jun 2024 12:43:49 -0700 Subject: [PATCH 7/8] add commit hash in console log --- .github/workflows/teamcity-nightly-workflow.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/teamcity-nightly-workflow.yaml b/.github/workflows/teamcity-nightly-workflow.yaml index 4ad63a68dc6..67e2c66ffb1 100644 --- a/.github/workflows/teamcity-nightly-workflow.yaml +++ b/.github/workflows/teamcity-nightly-workflow.yaml @@ -29,14 +29,15 @@ jobs: ref: "heads/main" }) const branchName = "UTC-" + dateToday; + const commitHash = mainRef.data.object.sha; try{ await github.rest.git.createRef({ owner: context.repo.owner, repo: context.repo.repo, ref: "refs/heads/" + branchName, - sha: mainRef.data.object.sha + sha: commitHash }) } catch (error){ core.setFailed(error + "- Failed to create new branch to be used running tonight\'s tests; branch with name " + branchName + " already exists") } - console.log("Created Branch: " + branchName) + console.log("Created Branch: " + branchName + " using commit " + commitHash + " from main.") From 5dea7fe5c90c9c4a023d605e2a15553c7b2ebf5c Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:19:04 -0700 Subject: [PATCH 8/8] Update .github/workflows/teamcity-nightly-sweeper.yaml Co-authored-by: Sarah French <15078782+SarahFrench@users.noreply.github.com> --- .github/workflows/teamcity-nightly-sweeper.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/teamcity-nightly-sweeper.yaml b/.github/workflows/teamcity-nightly-sweeper.yaml index 14ae9912cc1..d7d5b05dce3 100644 --- a/.github/workflows/teamcity-nightly-sweeper.yaml +++ b/.github/workflows/teamcity-nightly-sweeper.yaml @@ -1,8 +1,8 @@ name: "TeamCity: Remove branches used for nightly test" -# To ensure TeamCity build is triggered by the newly created branch. We prevent their to be only -# one branch that matches the filter (+:UTC-*,-:UTC-nightly-tests*) +# TeamCity should use a newly created branches that matches the pattern `UTC-` and is the only branch matching that pattern. We rename past nightly test branches to avoid there being more than one `UTC-` branch (i.e. only one branch that matches the filter (+:UTC-*,-:UTC-nightly-tests*)). This workflow also removes renamed branches once they get past a certain age. +# ``` on: workflow_call: