Skip to content

Commit 0dbc1bf

Browse files
committed
Update "getTargetBranch" to work on both Azure Pipelines and Github Actions
1 parent 2db07b6 commit 0dbc1bf

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

.ado/scripts/prepublish-check.mjs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,20 @@ function versionToNumber(version) {
138138
* @returns {string | undefined}
139139
*/
140140
function getTargetBranch() {
141-
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
142-
const adoTargetBranchName = process.env["SYSTEM_PULLREQUEST_TARGETBRANCH"];
143-
return adoTargetBranchName?.replace(/^refs\/heads\//, "");
141+
// Azure Pipelines
142+
if (process.env["TF_BUILD"] === "True") {
143+
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
144+
const targetBranch = process.env["SYSTEM_PULLREQUEST_TARGETBRANCH"];
145+
return targetBranch?.replace(/^refs\/heads\//, "");
146+
}
147+
148+
// GitHub Actions
149+
if (process.env["GITHUB_ACTIONS"] === "true") {
150+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
151+
return process.env["GITHUB_BASE_REF"];
152+
}
153+
154+
return undefined;
144155
}
145156

146157
/**
@@ -150,15 +161,32 @@ function getTargetBranch() {
150161
* @returns {string}
151162
*/
152163
function getCurrentBranch(options) {
153-
const adoTargetBranchName = getTargetBranch();
154-
if (adoTargetBranchName) {
155-
return adoTargetBranchName;
164+
const targetBranch = getTargetBranch();
165+
if (targetBranch) {
166+
return targetBranch;
156167
}
157168

158-
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
159-
const adoSourceBranchName = process.env["BUILD_SOURCEBRANCHNAME"];
160-
if (adoSourceBranchName) {
161-
return adoSourceBranchName.replace(/^refs\/heads\//, "");
169+
// Azure DevOps Pipelines
170+
if (process.env["TF_BUILD"] === "True") {
171+
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
172+
const sourceBranch = process.env["BUILD_SOURCEBRANCHNAME"];
173+
if (sourceBranch) {
174+
return sourceBranch.replace(/^refs\/heads\//, "");
175+
}
176+
}
177+
178+
// GitHub Actions
179+
if (process.env["GITHUB_ACTIONS"] === "true") {
180+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
181+
const headRef = process.env["GITHUB_HEAD_REF"];
182+
if (headRef) {
183+
return headRef; // For pull requests
184+
}
185+
186+
const ref = process.env["GITHUB_REF"];
187+
if (ref) {
188+
return ref.replace(/^refs\/heads\//, ""); // For push events
189+
}
162190
}
163191

164192
const { "mock-branch": mockBranch } = options;

0 commit comments

Comments
 (0)