Skip to content

Commit

Permalink
azure-pipelines: Add a pipeline definition to facilitate releasing pa…
Browse files Browse the repository at this point in the history
…ckages to NPM (#1604)
  • Loading branch information
bwateratmsft authored Oct 18, 2023
1 parent b0dc641 commit 1aa8b77
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
39 changes: 39 additions & 0 deletions azure-pipelines/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Usage

### Primary pipelines

To use these base pipeline templates:
1. Your project must have an `.nvmrc` file with the appropriate Node.js version at the root of the repository
1. Your `package.json` file must contain the following NPM scripts:
Expand Down Expand Up @@ -48,3 +50,40 @@ resources:
extends:
template: azure-pipelines/jobs.yml@templates
```
### Releasing to NPM
1. Releasing to NPM requires only a simple YAML pipeline file, for example this `release-npm.yml` file in `.azure-pipelines`:

```yaml
trigger: none # Disable the branch trigger
pr: none # Disable PR trigger
# Choose a package to publish at the time of job creation
parameters:
- name: PackageToPublish
displayName: Package to Publish
type: string
values:
- microsoft-vscode-container-client
- microsoft-vscode-docker-registries
- your-packages-here
# Grab the base templates from https://github.com/microsoft/vscode-azuretools/tree/main/azure-pipelines
resources:
repositories:
- repository: templates
type: github
name: microsoft/vscode-azuretools
ref: main
endpoint: GitHub
# Use those base templates
extends:
template: azure-pipelines/release-npm.yml@templates
parameters:
PackageToPublish: ${{ parameters.PackageToPublish }}
PipelineDefinition: 33
```
2. Running the pipeline will release the package to NPM and create a draft GitHub release. Add change notes to the draft release and publish when complete.
64 changes: 64 additions & 0 deletions azure-pipelines/release-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
parameters:
- name: PackageToPublish
displayName: Package to Publish
type: string
- name: PipelineDefinition
displayName: Pipeline Definition ID
type: number
- name: BranchToPublish
displayName: Branch to Publish
type: string
default: "refs/heads/main"

jobs:
- job: Release
pool:
vmImage: ubuntu-latest
steps:
- task: DownloadPipelineArtifact@2
inputs:
buildType: "specific"
project: "AzCode"
definition: ${{ parameters.PipelineDefinition }}
buildVersionToDownload: "latestFromBranch"
branchName: ${{ parameters.BranchToPublish }}
targetPath: $(System.DefaultWorkingDirectory)
- task: CmdLine@2
displayName: Validate Artifact
inputs:
script: |
TarballPath=`find . -type f -iname "${{ parameters.PackageToPublish }}*.tgz"`
if [[ $TarballPath =~ ((microsoft|vscode)-.*)-([0-9]+\.[0-9]+\.[0-9]+) ]]; then
echo "##vso[task.setvariable variable=Version]${BASH_REMATCH[3]}"
echo "##vso[task.setvariable variable=TarballPath]$TarballPath"
else
echo "Failed to parse tarball path \"$TarballPath\""
exit 1
fi
workingDirectory: $(System.DefaultWorkingDirectory)
- task: CmdLine@2
displayName: Create .npmrc
inputs:
script: echo "registry=https://registry.npmjs.org" >> .npmrc
workingDirectory: $(System.DefaultWorkingDirectory)
- task: npmAuthenticate@0
displayName: "npm Authenticate"
inputs:
workingFile: "$(System.DefaultWorkingDirectory)/.npmrc"
customEndpoint: "npm token"
- task: CmdLine@2
displayName: NPM Publish
inputs:
script: npm --userconfig .npmrc publish --access public '$(System.DefaultWorkingDirectory)/$(TarballPath)'
- task: GitHubRelease@1
displayName: "GitHub release (create)"
inputs:
gitHubConnection: "AzCode-Bot"
tagSource: userSpecifiedTag
tag: "${{ parameters.PackageToPublish }}-v$(Version)"
title: "${{ parameters.PackageToPublish }} v$(Version)"
releaseNotesSource: inline
assets: "$(System.DefaultWorkingDirectory)/$(TarballPath)"
isDraft: true
isPreRelease: true
addChangeLog: false

0 comments on commit 1aa8b77

Please sign in to comment.