6

I'm trying to automatically generate and update documentation for my code in an Azure DevOps Repo. I've configured a pipeline to run a python script upon commit to the master branch. This script pulls the relevant information out from the files in the repository and creates a markdown file and stores the output as README.md

However, when I run the pipeline nothing happens. The job registers as completed but the README.md file is unchanged. I don't get an error come up or anything, so not really sure what is going wrong, maybe a permissions thing. Does anybody know any fix for this?

Pipeline code:

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 script:

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()
Ryan
  • 101
  • 1
  • 6

1 Answers1

6

Totally possible, follow this action plan entirely, post questions if any.

  1. Before the transformation of your file takes place and right after the checkout, add a bash script with the following code inline:

git checkout $(Build.SourceBranchName)

Whatever transformation you are after handle it in Python and verify it using an inline bash script step in your pipeline as follows:

cat README.md

If you see the expected state of your README.md file in the logs of the pipeline then simply add a second inline bash script as follows:

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)

Prerequisites:

  1. You need to enable the use of the OAuth token for your pipeline, this will authenticate the push operation back to your Git repo. For YAML pipelines you need to add an explicit Checkout step as the first step with option persistCredentials set to true, e.g.
- checkout: self
  persistCredentials: true
  1. The push operation will use the permissions of a Build Service identity, project or collection scoped. These identities do not have Contribute generic permissions by default so you need to grant it to them. FYI these identities are used across all of your pipelines within Azure DevOps. Your identities are named as follows:

Organization scoped: Project Collection Build Service ({OrgName})
Project scoped: {Project Name} Build Service ({Org Name})

Grant them the Contribute permission from Project Settings -> Repositories

enter image description here

Former Azure DevOps and GitHub support engineer. I'm taking Python out of the ecuation for the commit and push steps, though possible it would be definitely harder to troubleshoot than Bash.

Aram Maliachi
  • 215
  • 2
  • 8
  • Thanks for the really detailed answer. I've tried you solution, it seems closer but the 2nd bash script is causing an error: `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: ##[error]Bash exited with code '128'.` – Ryan Apr 15 '20 at 12:33
  • 1
    My bad, you need this line yet in another bash script right after the checkout, before the transformation of the file takes place.```git checkout $(Build.SourceBranchName)```. Edited main answer too. – Aram Maliachi Apr 15 '20 at 13:23
  • Worked like a dream!! Thank you very much for the help – Ryan Apr 15 '20 at 15:43