Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Forked from LukePammant/auto-increment-npm
Created March 30, 2021 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevewithington/46cc632d2a42867a6a3e6f290177e1a4 to your computer and use it in GitHub Desktop.
Save stevewithington/46cc632d2a42867a6a3e6f290177e1a4 to your computer and use it in GitHub Desktop.
Auto-increment NPM package in Azure Pipelines and publish to Azure Artifacts
trigger:
branches:
include:
- develop
- master
paths:
include:
- ./
resources:
- repo: self
stages:
- stage: Publish
displayName: Publish NPM Package
jobs:
- job: Publish
displayName: Publish to VSTS NPM Feed
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Bash@3
displayName: Set NPM package patch number
env:
companyName: '<your company name - used in the npm feed url>'
feedName: '<your feed name - used in the npm feed url>'
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
targetType: 'inline'
script: |
# Get package name and version from package.json file
packageName=$(jq -r ".name" package.json)
package_version=$(jq -r ".version" package.json)
get_package_id_URL="https://feeds.dev.azure.com/${companyName}/_apis/packaging/Feeds/${feedName}/packages?protocolType=Npm&packageNameQuery=$packageName"
# next, let's get all available versions for our package
all_versions_URL=$(curl -s -X GET -u PATUSER:$SYSTEM_ACCESSTOKEN $get_package_id_URL | jq -r '.value[0]._links.versions.href')
all_versions=$(curl -s -X GET -u PATUSER:$SYSTEM_ACCESSTOKEN $all_versions_URL | jq -r '.value[].version')
all=($all_versions)
echo All package versions: $all
# if we find out that the version we're trying to publish already exists in the feed, then let's increment patch version for that package and publish
if [[ " ${all[@]} " =~ " ${package_version} " ]]; then
echo Current package version found in existing packages. Iterating the patch number...
# get latest version currently published in the feed for our package
latest_version=$(curl -s -X GET -u PATUSER:$SYSTEM_ACCESSTOKEN $get_package_id_URL| jq -r '.value[].versions[].version')
IFS=. read i1 i2 i3 <<< "$latest_version"
i3_updated=$((i3 + 1))
new_version=$i1.$i2.$i3_updated
new_buildnumber=$i1.$i2.$i3_updated
echo New package version: $new_version
# update patch number variable
echo "##vso[task.setvariable variable=patch;]$i3_updated"
# update build number of the current build. let's keep things tidy
echo "##vso[build.updatebuildnumber]$new_buildnumber"
echo Replacing \"version\": \"$package_version\" with \"version\": \"$new_version\" in local package.json
sed -i 's/"version": "'${package_version}'"/"version": "'${new_version}'"/' package.json
echo new package.json version: $(jq -r ".version" package.json)
fi
- task: Npm@1
displayName: npm install
inputs:
command: install
- task: Npm@1
displayName: npm publish
inputs:
command: publish
publishRegistry: useFeed
publishFeed: '$(feedName)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment