我正在尝试为 Azure DevOps Repo 中的代码自动生成和更新文档。我已经配置了一个管道在提交到 master 分支时运行 python 脚本。此脚本从存储库中的文件中提取相关信息并创建一个 markdown 文件并将输出存储为 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()
最佳答案
完全可能,完全遵循此行动计划,如有问题请提出。
- 在文件转换发生之前和结帐之后,添加一个包含以下内联代码的 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)
先决条件:
- 您需要为您的管道启用 OAuth token ,这将验证推送回您的 Git 存储库的操作。对于 YAML 管道,您需要添加显式 Checkout作为第一步,将选项 persistCredentials 设置为 true,例如
- checkout: self
persistCredentials: true
- 推送操作将使用 Build Service identity 的权限、项目或集合范围。默认情况下,这些身份不具有Contribute generic权限,因此您需要将其授予它们。仅供引用,这些身份用于 Azure DevOps 中的所有管道。您的身份命名如下:
组织范围:项目集合构建服务({OrgName})
项目范围:{Project Name}构建服务({Org Name})
从项目设置 -> 存储库中授予他们贡献权限
前 Azure DevOps 和 GitHub 支持工程师。我在提交和推送步骤中将 Python 排除在外,尽管它可能比 Bash 更难排除故障。
关于azure-devops - 是否可以让 Azure Pipeline 在提交时修改 README.md?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61212433/