Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure-pipelines: Add a pipeline definition to facilitate releasing packages to NPM #1604

Merged
merged 14 commits into from
Oct 18, 2023
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