Skip to content

Commit bcd07f2

Browse files
author
Ethan Arrowood
committed
add branch aliasing
1 parent 0c22e54 commit bcd07f2

File tree

5 files changed

+80
-11
lines changed

5 files changed

+80
-11
lines changed

pnpm-lock.yaml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vercel-deployment-task-source/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"dependencies": {
1414
"azure-devops-node-api": "12.0.0",
15-
"azure-pipelines-task-lib": "4.3.1"
15+
"azure-pipelines-task-lib": "4.3.1",
16+
"undici": "5.22.1"
1617
},
1718
"devDependencies": {
1819
"@types/node": "16.18.24",

vercel-deployment-task-source/src/index.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import {
66
which,
77
tool,
88
setResourcePath,
9-
setVariable
9+
setVariable,
10+
getVariable
1011
} from "azure-pipelines-task-lib";
1112
import path from "path";
13+
import fs from 'fs';
14+
import { request } from 'undici';
1215

1316
function errorHandler(error: unknown) {
1417
setResult(TaskResult.Failed, `Unknown error thrown: ${error}`);
@@ -17,6 +20,33 @@ function errorHandler(error: unknown) {
1720
process.on("unhandledRejection", errorHandler);
1821
process.on("unhandledException", errorHandler);
1922

23+
async function getStagingPrefix (token: string) {
24+
const projectJSONPath = path.join(__dirname, '.vercel', 'project.json')
25+
const projectJSONData = fs.readFileSync(projectJSONPath, 'utf-8');
26+
const projectJSON = JSON.parse(projectJSONData);
27+
const orgId: string = projectJSON.orgId;
28+
29+
const isTeam = orgId.startsWith('team_');
30+
const apiURL = isTeam
31+
? `https://api.vercel.com/v2/teams/${orgId}`
32+
: `https://api.vercel.com/v2/user`;
33+
34+
const { statusCode, body } = await request(apiURL, {
35+
"headers": {
36+
"Authorization": `Bearer ${token}`
37+
},
38+
"method": "GET"
39+
});
40+
41+
const result = await body.json();
42+
43+
if (statusCode !== 200) {
44+
throw new Error(`Failed to get project owner information. Error: ${result.message}`)
45+
}
46+
47+
return result.stagingPrefix;
48+
}
49+
2050
async function run() {
2151
try {
2252
setResourcePath(path.join(__dirname, "..", "task.json"));
@@ -60,19 +90,35 @@ async function run() {
6090
);
6191
({ stdout, stderr, code } = vercelDeploy.execSync());
6292

63-
const message =
64-
code === 0
65-
? `Successfully deployed to ${stdout}`
66-
: `Failed to deploy ${vercelProject}.\n\nError:\n${stderr}`;
67-
68-
setVariable('deploymentTaskMessage', message, false, true);
69-
7093
if (code !== 0) {
7194
throw new Error(
7295
`vercel deploy failed with exit code ${code}. Error: ${stderr}`
7396
);
7497
}
7598

99+
let deployURL = stdout;
100+
101+
if (!deployToProduction) {
102+
const branchName = getVariable('Build.SourceBranchName');
103+
const stagingPrefix = await getStagingPrefix(vercelToken);
104+
const aliasURL = `${vercelProject}-${branchName}-${stagingPrefix}.vercel.app`;
105+
deployURL = aliasURL;
106+
vercel = tool(which("vercel", true));
107+
const vercelAlias = vercel.arg(
108+
["alias", stdout, aliasURL, '--token', vercelToken]
109+
);
110+
({ stdout, stderr, code } = vercelAlias.execSync());
111+
if (code !== 0) {
112+
throw new Error(
113+
`vercel alias failed with exit code ${code}. Error: ${stderr}`
114+
);
115+
}
116+
}
117+
118+
const message = `Successfully deployed to ${deployURL}`;
119+
120+
setVariable('deploymentTaskMessage', message, false, true);
121+
76122
console.log(message);
77123

78124
setResult(TaskResult.Succeeded, "Success");

vercel-deployment-task-source/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"version": {
1212
"Major": 0,
1313
"Minor": 0,
14-
"Patch": 3
14+
"Patch": 4
1515
},
1616
"instanceNameFormat": "Deploying $(vercelProject) to Vercel",
1717
"inputs": [

vss-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"manifestVersion": 1,
44
"id": "vercel-deployment-extension",
55
"name": "Vercel Deployment Extension",
6-
"version": "0.0.8",
6+
"version": "0.0.9",
77
"publisher": "Vercel",
88
"targets": [
99
{

0 commit comments

Comments
 (0)