【问题标题】:Is it possible to have a Azure Pipeline modify a README.md on commit?是否可以让 Azure Pipeline 在提交时修改 README.md?
【发布时间】:2020-07-27 11:03:36
【问题描述】:

我正在尝试在 Azure DevOps 存储库中为我的代码自动生成和更新文档。我已经配置了一个管道,以便在提交到 master 分支时运行 python 脚本。此脚本从存储库中的文件中提取相关信息并创建一个降价文件并将输出存储为 README.md

但是,当我运行管道时,什么也没有发生。作业注册为已完成,但 README.md 文件未更改。我没有出现错误或任何东西,所以不太确定出了什么问题,也许是权限问题。有没有人知道解决这个问题?

管道代码:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.8'

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
    python generate-documentation.py
  displayName: 'Generate Documentation'

Python 脚本:

import yaml

file = open('single-source.yaml')

documentation = yaml.load(file, Loader=yaml.FullLoader)


productdetails = documentation["product details"]
specifications = documentation["specifications"]
prerequisites = documentation["prerequisites"]
requiredinputs = documentation["required inputs"]
selfservice = documentation["self service"]
costsandcharging = documentation["costs and charging"]

f = open("README.md","w")

for x in productdetails.values():
  f.write(x+"\n" )

f = open("README.md","a")

if "specifications" in documentation:
    for x in specifications.values():
      f.write(x+"\n")

if "prerequisites" in documentation:
    for x in prerequisites.values():
        f.write(x+"\n")

if "requiredinputs" in documentation:
    for x in requiredinputs.values():
        f.write(x+"\n")

if "selfservice" in documentation:
    for x in selfservice.values():
        f.write(x+"\n")

if "costsandcharging" in documentation:
    for x in costsandcharging.values():
        f.write(x)

f.close()

【问题讨论】:

  • 可以添加 Python 脚本吗?
  • 你的意思是降价没有改变?您是否尝试在管道结束时将 Readme.md 文件放入脚本任务中以查看其实际内容?
  • 对不起,我添加了 python 脚本来尝试增加一些清晰度。我的意思是,存储库中的自述文件没有改变。
  • 你能写出你是如何准确检查自述文件的吗?

标签: azure-devops azure-pipelines azure-repos


【解决方案1】:

完全有可能,完全遵循此行动计划,如有任何问题,请发布。

  1. 在转换文件之前和结帐之后,添加一个包含以下内联代码的 bash 脚本:

git checkout $(Build.SourceBranchName)

无论您在 Python 中处理后进行何种转换,并使用管道中的内联 bash script 步骤对其进行验证,如下所示:

cat README.md

如果您在管道日志中看到 README.md 文件的预期状态,则只需添加第二个内联 bash 脚本,如下所示:

git add README.md
git config --global user.name "$(Build.RequestedFor)"
git config --global user.email "$(Build.RequestedForEmail)"
git commit -m "$(Build.BuildId)"
git push origin $(Build.SourceBranchName)

先决条件:

  1. 您需要为您的管道启用 OAuth 令牌,这将验证推送操作返回到您的 Git 存储库。对于 YAML 管道,您需要添加一个明确的 Checkout 步骤作为第一步,并将选项 persistCredentials 设置为 true,例如
- checkout: self
  persistCredentials: true
  1. 推送操作将使用Build Service identity、项目或集合范围的权限。默认情况下,这些身份不具有贡献通用权限,因此您需要将其授予它们。仅供参考,这些身份用于 Azure DevOps 中的所有管道。您的身份命名如下:

组织范围:项目集合构建服务 ({OrgName})
项目范围:{Project Name} Build Service ({Org Name})

从项目设置 -> 存储库中授予他们 贡献 权限

前 Azure DevOps 和 GitHub 支持工程师。我正在将 Python 排除在提交和推送步骤的评估之外,尽管可能它肯定比 Bash 更难排除故障。

【讨论】:

  • 感谢您提供非常详细的答案。我试过你的解决方案,它似乎更接近但第二个 bash 脚本导致错误:fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use git push origin HEAD:<name-of-remote-branch> ##[error]Bash exited with code '128'.
  • 我的错,在结帐之后,在文件转换发生之前,您还需要在另一个 bash 脚本中使用这一行。git checkout $(Build.SourceBranchName)。也编辑了主要答案。
  • 像梦一样工作!!非常感谢您的帮助
  • @Ryan 如果Aram的回答有帮助,请将其标记为回答,只需点击this之类的选项即可。因此对于遇到相同问题的其他成员轻松找到解决方案会有帮助。
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 2017-06-01
  • 2011-07-16
  • 2016-02-08
  • 2012-11-22
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多