我正在尝试在 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脚本 https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/bash?view=azure-devops按如下步骤进入您的管道:
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)
先决条件:
-
You need要为您的管道启用 OAuth 令牌,这将对推送操作进行身份验证返回您的 Git 存储库。对于 YAML 管道,您需要添加显式作为带有选项的第一步持久化凭证 set to true, e.g.
- checkout: self
persistCredentials: true
- 推送操作将使用 a 的权限建立服务身份 https://learn.microsoft.com/azure/devops/pipelines/build/options?view=azure-devops#build-job-authorization-scope、项目或集合范围。这些身份do not have 贡献通用默认情况下权限,因此您需要将其授予他们。仅供参考,这些身份被广泛使用allAzure DevOps 中的管道。您的身份命名如下:
组织范围:项目集合构建服务({组织名称})
项目范围:{项目名}构建服务({组织名称})
授予他们贡献项目设置 -> 存储库的权限
前 Azure DevOps 和 GitHub 支持工程师。我将 Python 从提交和推送步骤的等式中剔除,尽管它可能比 Bash 更难排除故障。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)