Skip to content

Commit 003d888

Browse files
authored
azure-pipelines: Refactor NPM release pipeline (#1868)
1 parent 034cf42 commit 003d888

File tree

2 files changed

+124
-64
lines changed

2 files changed

+124
-64
lines changed

.azure-pipelines/release-npm.yml

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pr: none # Disable PR trigger
33

44
# Choose a package to publish at the time of job creation
55
parameters:
6-
- name: PackageToPublish
6+
- name: packageToPublish
77
displayName: Package to Publish
88
type: string
99
values:
@@ -16,10 +16,13 @@ parameters:
1616
- microsoft-vscode-azext-github
1717
- microsoft-vscode-azext-serviceconnector
1818
- microsoft-vscode-azext-utils
19-
- name: BranchToPublish
20-
displayName: Branch to Publish
19+
- name: publishVersion
20+
displayName: Publish Version
2121
type: string
22-
default: "refs/heads/main"
22+
- name: dryRun
23+
displayName: Dry Run
24+
type: boolean
25+
default: false
2326

2427
# Grab the base templates from https://github.com/microsoft/vscode-azuretools/tree/main/azure-pipelines
2528
resources:
@@ -29,13 +32,17 @@ resources:
2932
name: microsoft/vscode-azuretools
3033
ref: main
3134
endpoint: GitHub-AzureTools
35+
pipelines:
36+
- pipeline: build # This must be "build"
37+
source: \Azure Tools\VSCode\Packages\vscode-azuretools # name of the pipeline that produces the artifacts
3238

3339
# Use those base templates
3440
extends:
3541
template: azure-pipelines/1es-release-npm.yml@templates
3642
parameters:
37-
PackageToPublish: ${{ parameters.PackageToPublish }}
38-
BranchToPublish: ${{ parameters.BranchToPublish }}
39-
PipelineDefinition: 20425
43+
packageToPublish: ${{ parameters.packageToPublish }}
44+
artifactName: Build ${{ parameters.packageToPublish}}
45+
publishVersion: ${{ parameters.publishVersion }}
46+
dryRun: ${{ parameters.dryRun }}
4047
OwnerAlias: "jinglou"
4148
ApproverAlias: "bwater"

azure-pipelines/1es-release-npm.yml

+110-57
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
parameters:
2-
- name: PackageToPublish
3-
displayName: Package to Publish
2+
# The name of the package to publish
3+
- name: packageToPublish
44
type: string
5-
- name: BranchToPublish
6-
displayName: Branch to Publish
5+
# In the build artifacts, the name of the artifact containing the package to publish
6+
- name: artifactName
77
type: string
8-
- name: PipelineDefinition
9-
displayName: Pipeline Definition
10-
type: number
8+
default: Build Root
9+
10+
# The intended package version to publish.
11+
# This is used to verify the version in package.json matches the version to publish to avoid accidental publishing.
12+
- name: publishVersion
13+
type: string
14+
15+
# When true, skips the deployment job which actually publishes the package
16+
- name: dryRun
17+
type: boolean
18+
default: false
19+
1120
- name: OwnerAlias
1221
displayName: Owner Alias
1322
type: string
@@ -26,69 +35,113 @@ resources:
2635
extends:
2736
template: azure-pipelines/1ES.Official.Publish.yml@MicroBuildTemplate
2837
parameters:
29-
sdl:
30-
tsa:
31-
enabled: true
32-
configFile: '$(Build.SourcesDirectory)\.azure-pipelines\compliance\tsaoptions.json'
33-
credscan:
34-
suppressionsFile: $(Build.SourcesDirectory)\.azure-pipelines\compliance\CredScanSuppressions.json
35-
sourceAnalysisPool:
36-
name: AzurePipelines-EO
37-
image: 1ESPT-Windows2022
38-
# codeql:
39-
# enabled: true # TODO: would like to enable only on scheduled builds but CodeQL cannot currently be disabled per https://eng.ms/docs/cloud-ai-platform/devdiv/one-engineering-system-1es/1es-docs/codeql/1es-codeql
4038
pool:
41-
name: AzurePipelines-EO # Name of your hosted pool
42-
image: 1ESPT-Ubuntu20.04 # Name of the image in your pool. If not specified, first image of the pool is used
43-
os: linux # OS of the image. Allowed values: windows, linux, macOS
39+
name: VSEngSS-MicroBuild2022-1ES # Name of your hosted pool
40+
image: server2022-microbuildVS2022-1es # Name of the image in your pool. If not specified, first image of the pool is used
41+
os: windows # OS of the image. Allowed values: windows, linux, macOS
42+
4443
stages:
4544
- stage: ReleaseStage
45+
displayName: Release package
4646
jobs:
47-
- job: ReleaseJob
48-
steps:
49-
- task: DownloadPipelineArtifact@2
50-
displayName: "\U0001F449 Download Artifact"
51-
inputs:
52-
buildType: "specific"
53-
project: "DevDiv"
54-
definition: ${{ parameters.PipelineDefinition }}
55-
buildVersionToDownload: "latestFromBranch"
56-
branchName: ${{ parameters.BranchToPublish }}
47+
- job: Publish
48+
displayName: Publish package
49+
templateContext:
50+
type: releaseJob
51+
isProduction: true
52+
inputs:
53+
- input: pipelineArtifact
54+
pipeline: build
5755
targetPath: $(System.DefaultWorkingDirectory)
58-
itemPattern: "**/*.tgz"
59-
- task: CmdLine@2
60-
displayName: "\U0001F449 Validate Artifact"
61-
inputs:
62-
script: |
63-
TarballPath=`find . -type f -iname "${{ parameters.PackageToPublish }}*.tgz"`
64-
if [[ $TarballPath =~ ((microsoft|vscode)-.*)-([0-9]+\.[0-9]+\.[0-9]+) ]]; then
65-
echo "##vso[task.setvariable variable=Version]${BASH_REMATCH[3]}"
66-
echo "##vso[task.setvariable variable=TarballPath]$TarballPath"
67-
echo "##vso[task.setvariable variable=TarballFolder]$(dirname "$TarballPath")"
68-
echo "Found tarball \"$(basename "$TarballPath")\" in folder \"$(dirname "$TarballPath")\""
69-
else
70-
echo "Failed to parse tarball path \"$TarballPath\""
71-
exit 1
72-
fi
73-
workingDirectory: $(System.DefaultWorkingDirectory)
74-
- template: MicroBuild.Publish.yml@MicroBuildTemplate
75-
parameters:
76-
intent: "PackageDistribution"
77-
contentType: "npm"
78-
contentSource: "Folder"
79-
folderLocation: "$(System.DefaultWorkingDirectory)/$(TarballFolder)"
80-
waitForReleaseCompletion: true
81-
owners: "${{ parameters.OwnerAlias }}@microsoft.com"
82-
approvers: "${{ parameters.ApproverAlias }}@microsoft.com"
56+
artifactName: ${{ parameters.artifactName }}
57+
steps:
58+
59+
# Locate the desired .tgz file and set the relevant variables
60+
- powershell: |
61+
# Find the desired .tgz file
62+
$tgzFiles = Get-ChildItem -Path $(System.DefaultWorkingDirectory) -Filter ${{ parameters.packageToPublish }}*.tgz -File -Recurse
63+
64+
# Check if more than one .tgz file is found
65+
if ($tgzFiles.Count -gt 1) {
66+
Write-Error "More than one .tgz file found."
67+
exit 1
68+
} elseif ($tgzFiles.Count -eq 0) {
69+
Write-Error "No .tgz files found."
70+
exit 1
71+
} else {
72+
# Set the pipeline variable
73+
$tgzFileName = $tgzFiles.FullName
74+
$tgzFolderName = $tgzFiles.DirectoryName
75+
Write-Output "##vso[task.setvariable variable=tgzFileName;]$tgzFileName"
76+
Write-Output "##vso[task.setvariable variable=tgzFolderName;]$tgzFolderName"
77+
Write-Output "Found .tgz file: $tgzFileName"
78+
}
79+
displayName: "\U0001F449 Find and Set .tgz File/Folder Variables"
80+
81+
# Modify the build number to include package name, package version, and if dry run is true
82+
- powershell: |
83+
# Get the version from package.json
84+
85+
$packageJsonPath = "$(tgzFolderName)/package.json"
86+
$npmVersionString = (Get-Content $packageJsonPath | ConvertFrom-Json).version
87+
$isDryRun = "$env:dryRun"
88+
$currentBuildNumber = "$(Build.BuildId)"
89+
90+
$dry = ""
91+
if ($isDryRun -eq 'True') {
92+
Write-Output "Dry run was set to True. Adding 'dry' to the build number."
93+
$dry = "dry"
94+
}
95+
96+
$newBuildNumber = "$env:packageToPublish-$npmVersionString-$dry-$currentBuildNumber"
97+
Write-Output "##vso[build.updatebuildnumber]$newBuildNumber"
98+
displayName: "\U0001F449 Prepend version from package.json to build number"
99+
env:
100+
dryRun: ${{ parameters.dryRun }}
101+
packageToPublish: ${{ parameters.packageToPublish }}
102+
103+
# For safety, verify the version in package.json matches the version to publish entered by the releaser
104+
# If they don't match, this step fails
105+
- powershell: |
106+
# Get the version from package.json
107+
$packageJsonPath = "$(tgzFolderName)/package.json"
108+
$npmVersionString = (Get-Content $packageJsonPath | ConvertFrom-Json).version
109+
$publishVersion = "$env:publishVersion"
110+
Write-Output "Publishing version: $publishVersion"
111+
if ($npmVersionString -eq $publishVersion) {
112+
Write-Output "Publish version matches package.json version. Proceeding with release."
113+
} else {
114+
Write-Error "Publish version $publishVersion doesn't match version found in package.json $npmVersionString. Cancelling release."
115+
exit 1
116+
}
117+
displayName: "\U0001F449 Verify publish version"
118+
env:
119+
publishVersion: ${{ parameters.publishVersion }}
120+
121+
# Publish the package to NPM
122+
- ${{ if eq(parameters.dryRun, false) }}:
123+
- template: MicroBuild.Publish.yml@MicroBuildTemplate
124+
parameters:
125+
intent: "PackageDistribution"
126+
contentType: "npm"
127+
contentSource: "Folder"
128+
folderLocation: "$(tgzFolderName)"
129+
waitForReleaseCompletion: true
130+
owners: "${{ parameters.OwnerAlias }}@microsoft.com"
131+
approvers: "${{ parameters.ApproverAlias }}@microsoft.com"
132+
133+
# Create a release on GitHub containing the package
83134
- task: GitHubRelease@1
84135
displayName: "\U0001F449 GitHub release (create)"
136+
condition: and(succeeded(), ${{ eq(parameters.dryRun, false) }})
85137
inputs:
86138
gitHubConnection: "GitHub-AzureTools"
87139
tagSource: userSpecifiedTag
88140
tag: "${{ parameters.PackageToPublish }}-v$(Version)"
89141
title: "${{ parameters.PackageToPublish }} v$(Version)"
90142
releaseNotesSource: inline
91-
assets: "$(System.DefaultWorkingDirectory)/$(TarballPath)"
143+
assets: "$(tgzFileName)"
92144
isDraft: true
93145
isPreRelease: true
94146
addChangeLog: false
147+

0 commit comments

Comments
 (0)