@@ -6,9 +6,12 @@ import {
6
6
which ,
7
7
tool ,
8
8
setResourcePath ,
9
- setVariable
9
+ setVariable ,
10
+ getVariable
10
11
} from "azure-pipelines-task-lib" ;
11
12
import path from "path" ;
13
+ import fs from 'fs' ;
14
+ import { request } from 'undici' ;
12
15
13
16
function errorHandler ( error : unknown ) {
14
17
setResult ( TaskResult . Failed , `Unknown error thrown: ${ error } ` ) ;
@@ -17,6 +20,33 @@ function errorHandler(error: unknown) {
17
20
process . on ( "unhandledRejection" , errorHandler ) ;
18
21
process . on ( "unhandledException" , errorHandler ) ;
19
22
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
+
20
50
async function run ( ) {
21
51
try {
22
52
setResourcePath ( path . join ( __dirname , ".." , "task.json" ) ) ;
@@ -60,19 +90,35 @@ async function run() {
60
90
) ;
61
91
( { stdout, stderr, code } = vercelDeploy . execSync ( ) ) ;
62
92
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
-
70
93
if ( code !== 0 ) {
71
94
throw new Error (
72
95
`vercel deploy failed with exit code ${ code } . Error: ${ stderr } `
73
96
) ;
74
97
}
75
98
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
+
76
122
console . log ( message ) ;
77
123
78
124
setResult ( TaskResult . Succeeded , "Success" ) ;
0 commit comments