Learn how to create automated documentation generation workflows using Gobi CLI. Set up AI agents to analyze code changes and generate or update documentation automatically in GitHub workflows or local development.
This guide demonstrates how to create automated documentation generation based on code updates in a git branch using the Gobi CLI, either as part of your local workflow or as part of a GitHub workflow.This process utilizes the Gobi CLI (cn) in headless mode to analyze changes and generate the necessary documentation, and commit and push the changes. The goal is to keep the workflow as simple as possible by using straightforward shell commands, Gobi CLI prompts, and basic git operations.
This example uses a manual workflow dispatch that requires two inputs: the repository name and the branch containing your code changes. This is helpful when you want to generate documentation for a feature branch before creating a pull request.
Required Inputs:
repository: The repository you are operating on (format: owner/repo)
branch_name: The name of the branch you have code changes on and want to generate documentation for
gobi_config: The Gobi agent configuration to use
Consider setting a default value if you have a default config your’d like to use
gobi_org: The Gobi org to use
Consider setting a default value if you have a default org your’d like to use
.github/workflows/generate-docs.yml
name: Generate Docs for Branchon: workflow_dispatch: inputs: repository: description: 'Repository (owner/repo)' required: true default: 'owner/repo' branch_name: description: 'Branch name to generate docs for' required: true gobi_config: description: 'Gobi agent configuration to use' required: true # Set a default value if you have a default config your'd like to use # default: 'agent-config-name' gobi_org: description: 'Gobi org to use' required: true # Set a default value if you have a default org your'd like to use # default: 'your-org-name'jobs: write-docs: runs-on: ubuntu-latest name: Write Documentation for Branch env: GOBI_API_KEY: ${{ secrets.GOBI_API_KEY }} GOBI_ORG: ${{ github.event.inputs.gobi_org }} GOBI_CONFIG: ${{ github.event.inputs.gobi_config }} steps: - name: Checkout fork repository uses: actions/checkout@v4 with: repository: ${{ github.event.inputs.repository || 'owner/repo' }} token: ${{ secrets.GH_PAT }} fetch-depth: 0 # Full history needed for sync - name: Setup git configuration run: | git config user.name "github-actions[doc-writer-bot]" git config user.email "[email protected]" - name: Checkout target branch run: | git fetch origin ${{ github.event.inputs.branch_name }} git checkout ${{ github.event.inputs.branch_name }} - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '18' - name: Install Gobi CLI run: | echo "Installing Gobi CLI..." npm i -g @gourmanddev/cli - name: Verify Gobi CLI installation run: | echo "Checking Gobi CLI version..." cn --version || exit 1 - name: Set branch name id: branch run: | BRANCH_NAME="${{ github.event.inputs.branch_name }}-docs-update-$(date +%Y-%m-%d-%H-%M-%S)" echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT echo "Branch name: $BRANCH_NAME" - name: Generate git diff context run: | echo "Git changes:" > context.txt git diff origin/main..HEAD --stat >> context.txt echo -e "\n\nFile list:" >> context.txt git diff origin/main..HEAD >> context.txt echo "Generated context file:" cat context.txt - name: Create documentation branch run: | echo "Creating branch: ${{ steps.branch.outputs.branch_name }}" git checkout -b "${{ steps.branch.outputs.branch_name }}" - name: Generate documentation with Gobi CLI run: | echo "Running Gobi agent to generate documentation..." cn --config <gobi-user>/<agent-config> \ --auto \ --allow Write \ -p \ --prompt ./context.txt \ "Analyze the provided git diff and identify any new functionality introduced. Summarise the new features in a few sentences. Then search the existing documentation in the site docs/ directory to determine whether these features are already documented in a way that enables users to use them. If documentation is missing or incomplete, create or modify Markdown files under the site diretory (without changing any code) to add clear explanations, usage instructions and examples for the new features in the same style and format as the existing documentation. Finally, print a brief summary of the documentation changes you made." - name: Clean up temporary files run: rm -f context.txt - name: Commit and push documentation changes run: | echo "Review git status..." git status echo "Adding files edited in site directory to git..." git add site/ echo "Committing changes..." git commit -s -m "docs: update for new functionality from branch ${{ github.event.inputs.branch_name }}" echo "Pushing changes to origin..." git push --set-upstream origin "${{ steps.branch.outputs.branch_name }}"
Make sure to set your GOBI_API_KEY environment variable before running local scripts to enable headless mode.
When using the Gobi CLI on your local machine, you can build workflows in various ways, one of which is by simply creating shell scripts that you can run, which call the CLI.The below shell script snippet shows the final part of a docs updating shell script that can be used to generate documentation for the code changes in a branch of a git repository.For the example snippet below to work you will need to set the following variables:
GOBI_ORG
GOBI_CONFIG
BASE_BRANCH
COMPARE_BRANCH
DOCS_BRANCH_NAME
For example you can either set these as environment variables or as command line arguments to be used by the script.Example shell script snippet for generating documentation
generate-docs.sh
#!/bin/bash# The script below shows the final part of a docs updating shell script that can be used to generate documentation for the code changes in a branch of a git repository.# Generate git diff contextecho "Generating git diff context..."echo "Git changes:" > context.txtgit diff "$BASE_BRANCH..$COMPARE_BRANCH" --stat >> context.txtecho -e "\n\nFile list:" >> context.txtgit diff "$BASE_BRANCH..$COMPARE_BRANCH" >> context.txtecho "Generated context file:"cat context.txt# Create documentation branchecho "Creating branch: $DOCS_BRANCH_NAME"git checkout -b "$DOCS_BRANCH_NAME"# Generate documentation with Gobi CLIecho "Running Gobi agent to generate documentation..."cn -config "$GOBI_ORG/$GOBI_CONFIG" \ --auto \ --allow Write \ -p \ --prompt ./context.txt \ "Analyze the provided git diff and identify any new functionality introduced. Summarise the new features in a few sentences. Then search the existing documentation in the site docs/ directory to determine whether these features are already documented in a way that enables users to use them. If documentation is missing or incomplete, create or modify Markdown files under the site diretory (without changing any code) to add clear explanations, usage instructions and examples for the new features in the same style and format as the existing documentation. Finally, print a brief summary of the documentation changes you made."# Clean up temporary filesecho "Cleaning up temporary files..."rm -f context.txt# Commit and push documentation changesecho "Reviewing git status..."git status# Only adding files that have been added and modified in the site directoryecho "Adding files edited in site directory to git..."git add site/if git diff --cached --quiet; then echo "No documentation changes to commit"else echo "Committing changes..." git commit -s -m "docs: update for new functionality from branch $BRANCH_NAME" echo "Pushing changes to origin..." git push --set-upstream origin "$DOCS_BRANCH_NAME" echo "Documentation branch created and pushed: $DOCS_BRANCH_NAME"fi