1
votes

Context

We use Azure DevOps for our Git Repos, Azure Pipelines for our CI/CD and Azure Artifacts for our private hosting of our python packages.

Goal

I want to:

Tag a Git repo with Python Package version from Azure Pipelines in Azure DevOps Git repo

Example

git tag -a "$(python setup.py --version)" -m "Released version $(python setup.py --version)"

I have read the Azure documentation but it seems they all use the Azure Pipeline variables for build number.

On a build pipeline, there is a check box to tag the repo on successfully building the wheel artifact.

Image of 'Get Sources Job' option to tag a release on success

But I'm not sure how to expose the package version as a pipeline variable since this job has no spot for me to escape to perform custom code. I'm sure there is a way to do it but I'm at the point where I need help and the documentation isn't guiding me to the right places based on what I'm querying and reading.

1
I gave you an upvote because I think this is a reasonable well thought out question and I don't like it when people downvote without an explanation.d_kennetz
Could you run a bash script to tag it after the build? It would look a bit more manual, but gives you the option to run custom code like you want, and it can use pipeline variables that you can set for the buildC.Nivs

1 Answers

2
votes

Not happy with this approach but it achieves the outcome.

Thanks @C.Nivs for the suggestion to just have a crack at a Bash script between the wheel successfully building and the task that exports the build artifact.

# Get Package Version
export PACKAGE_VERSION="$(python setup.py --version)"
echo "Package Version: v${PACKAGE_VERSION}"

# Get the body of the last merge commit message
git log HEAD~1..HEAD --format="RELEASE:v${PACKAGE_VERSION} Notes:%s%n%b"
export TAG_MESSAGE=$(git log HEAD~1..HEAD --format="RELEASE:v${PACKAGE_VERSION} Notes:%s%n%b")
echo $TAG_MESSAGE

# Set the config for the CI to be able to commit a tag
git config --global user.email "[email protected]"
git config --global user.name "Azure Pipelines: python-wheel-artifact"

# Create the tag locally as an annotated tag
# https://stackguides.com/questions/11514075/what-is-the-difference-between-an-annotated-and-unannotated-tag
git tag -a "v$PACKAGE_VERSION" -m "$TAG_MESSAGE"

# Update the push remote as the Personal Access Token URL
# Inject these values from the Pipeline variables.
git remote set-url --push origin "https://$(tag_push_username):$(tag_push_personal_access_token)@dev.azure.com/<myorganization>/<myproject>/_git/<myreponame>"

# Ensure the source of truth receives the tag.
git push --tags

References: