-
Notifications
You must be signed in to change notification settings - Fork 1.8k
138 lines (121 loc) · 6.02 KB
/
teamcity-nightly-workflow.yaml
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
name: "TeamCity: Create empty branch off tip of main to aid nightly-testing"
# 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 3 * * *' # UTC 3AM (-7)-> 8PM PST
jobs:
rename-TC-nightly-branch:
if: github.repository == 'hashicorp/terraform-provider-google' || github.repository == 'hashicorp/terraform-provider-google-beta'
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: |
// Get yesterday's date, when the branch was originally made
const date = new Date()
date.setDate(date.getDate() -1);
// Get YYYY-MM-DD string
dateString = date.toJSON().slice(0, 10)
const oldBranchName = "nightly-test";
const newBranchName = "UTC-nightly-tests-" + dateString;
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")
}
console.log("Renamed branch " + oldBranchName + " to " + newBranchName)
nightly-test-branch-creation:
needs: rename-TC-nightly-branch
if: always()
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 mainRef = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/main"
})
const branchName = "nightly-test";
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: 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 + " using commit " + commitHash + " from main.")
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-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)