Skip to content

Instantly share code, notes, and snippets.

@larstobi
Last active December 9, 2024 13:51
Show Gist options
  • Save larstobi/5dc24c6e90fd3555f6602b066e8b3bef to your computer and use it in GitHub Desktop.
Save larstobi/5dc24c6e90fd3555f6602b066e8b3bef to your computer and use it in GitHub Desktop.
Azure Artifacts get latest version
- task: Bash@3
inputs:
targetType: 'inline'
script: |
# Variables
ORGANIZATION="your_organization"
PROJECT="your_project"
FEED_ID="your_feed_id"
PACKAGE_NAME="your_package_name"
API_VERSION="7.1-preview.1"
TOKEN=$(System.AccessToken)
# API Endpoint
API_URL="https://pkgs.dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/packaging/feeds/${FEED_ID}/packages?packageNameQuery=${PACKAGE_NAME}&includeDeleted=false&api-version=${API_VERSION}"
# Fetch package data
RESPONSE=$(curl -s -H "Authorization: Bearer ${TOKEN}" "${API_URL}")
# Extract versions using jq
VERSIONS=$(echo "${RESPONSE}" | jq -r '.value[0].versions[].version')
# Check if versions were found
if [ -z "${VERSIONS}" ]; then
echo "No versions found for package ${PACKAGE_NAME} in feed ${FEED_ID}"
exit 1
fi
# Sort versions and get the latest
LATEST_VERSION=$(echo "${VERSIONS}" | sort -V | tail -n 1)
# Output the latest version
echo "Latest version of ${PACKAGE_NAME}: ${LATEST_VERSION}"
# Set the pipeline variable
echo "##vso[task.setvariable variable=LatestVersion]${LATEST_VERSION}"
API_VERSION="7.0"
organization=$ORGANIZATION
project=$PROJECT
feedId=$FEED_ID
packageId=$PACKAGE_NAME
protocolType=upack
# API Endpoint
# API_URL="https://pkgs.dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/packaging/feeds/${FEED_ID}/packages?packageNameQuery=${PACKAGE_NAME}&includeDeleted=false&api-version=${API_VERSION}"
API_URL=https://feeds.dev.azure.com/${organization}/${project}/_apis/packaging/Feeds/${feedId}/Packages/${packageId}/versions?api-version=7.1
packageNameQuery=$PACKAGE_NAME
API_URL="https://feeds.dev.azure.com/${organization}/${project}/_apis/packaging/Feeds/${feedId}/packages?protocolType=${protocolType}&packageNameQuery=${packageNameQuery}&api-version=7.1"
curl -v -H "Authorization: Bearer ${TOKEN}" "${API_URL}"
jq -r '.value[]
| select(.name == "distroless-java17-debian12")
| .versions[]
| select(.isLatest == true)
| .version' result.json
{
"count": 2,
"value": [
{
"id": "zzz-xxx-ddfdd",
"normalizedName": "distroless-java17-debian12",
"name": "distroless-java17-debian12",
"protocolType": "UPack",
"url": "https://feeds.dev.azure.com/XXX/xxx-ttt-rrr/_apis/Packaging/Feeds/ttt-yyy-uuu/Packages/eee-tttt-yyyy",
"versions": [
{
"id": "aaa-sss-ddd-ffff",
"normalizedVersion": "0.0.2-debug-nonroot-359d49c",
"version": "0.0.2-debug-nonroot-359d49c",
"isLatest": true
},
{
"id": "aaa-ddd-gggg-",
"normalizedVersion": "0.0.2-debug-nonroot-3ca45d7f",
"version": "0.0.2-debug-nonroot-3ca45d7f",
"isLatest": false
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment