在 Azure DevOps Git 存储库中使用来自 Azure Pipelines 的 Python 包版本标记 Git 存储库

Posted

技术标签:

【中文标题】在 Azure DevOps Git 存储库中使用来自 Azure Pipelines 的 Python 包版本标记 Git 存储库【英文标题】:Tag Git repo with Python Package version from Azure Pipelines in Azure DevOps Git repo 【发布时间】:2020-04-11 10:33:05 【问题描述】:

上下文

我们将 Azure DevOps 用于我们的 Git Repos,将 Azure Pipelines 用于我们的 CI/CD,将 Azure Artifacts 用于我们的 Python 包的私有托管。

目标

我想:

示例

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

我已阅读 Azure 文档,但似乎它们都使用 Azure Pipeline 变量作为内部版本号。

在构建管道上,有一个复选框用于标记成功构建***工件的 repo。

但我不确定如何将包版本公开为管道变量,因为这项工作没有让我逃避执行自定义代码的地方。我确信有办法做到这一点,但我正处于需要帮助的地步,并且文档并没有根据我正在查询和阅读的内容将我引导到正确的位置。

【问题讨论】:

我给了你一个赞成票,因为我认为这是一个经过深思熟虑的合理问题,我不喜欢人们在没有解释的情况下投票。 你能在构建后运行一个 bash 脚本来标记它吗?它看起来更手动一些,但可以让您选择运行您想要的自定义代码,并且它可以使用您可以为构建设置的管道变量 【参考方案1】:

对这种方法不满意,但它取得了成果。

感谢@C.Nivs 建议在成功构建***和导出构建工件的任务之间尝试一个 Bash 脚本。

# 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 "ci-pipeline@myorganisation.com"
git config --global user.name "Azure Pipelines: python-wheel-artifact"

# Create the tag locally as an annotated tag
# https://***.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

参考资料:

What is the difference between an annotated and unannotated tag?

【讨论】:

感谢您在此处分享您的解决方案,如果是,请您接受您的解决方案作为答案吗?因此,对于遇到相同问题的其他成员轻松找到解决方案将很有帮助。祝你有美好的一天:)

以上是关于在 Azure DevOps Git 存储库中使用来自 Azure Pipelines 的 Python 包版本标记 Git 存储库的主要内容,如果未能解决你的问题,请参考以下文章

将构建工件发布到远程 Git 存储库 - Azure DevOps

从 Azure DevOps 中的子模块触发父存储库中的构建

从Azure DevOps的Git库中下载指定文件

Azure DevOps 2020 - 从集合中的多个项目的存储库中查找文件

无法使用服务主体从发布管道访问 Azure Devops Git 存储库

Azure Devops OnPremise,致命:克隆 Git 存储库时身份验证失败