Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update the workflow file
  • Loading branch information
martintmk committed Sep 4, 2023
commit e9e2572f2cb363d5dbc145b1295c034e842bf629
80 changes: 67 additions & 13 deletions .github/workflows/on-push-do-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,76 @@ jobs:
- name: Setup .NET SDK
uses: actions/setup-dotnet@3447fd6a9f9e57506b15f895c5b76d3b197dc7c2 # v3.2.0

- name: Run MarkdownSnippets
shell: bash
- name: Update documentation
id: update-docs
shell: pwsh
env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
run: |
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

dotnet tool restore
dotnet mdsnippets

$GitStatus = (git status --porcelain)
if ([string]::IsNullOrEmpty($GitStatus)) {
Write-Host "No changes to commit."
exit 0
}

- name: Push changes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
run: |
git config --local user.email "138034000+polly-updater-bot[bot]@users.noreply.github.com"
git config --local user.name "polly-updater-bot[bot]"
git commit -m "Update documentation\n\nUpdate examples in documentation." -a || echo "Nothing to commit"
remote="https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git"
git push "${remote}" "${GITHUB_REF_NAME}" || echo "Nothing to push"
$TimeStamp = Get-Date -Format "yyyy-MM-dd-HH-mm"
$BranchName = "docs/update-docs-$TimeStamp"
echo "branchName=$BranchName" >> $env:GITHUB_OUTPUT

$GitEmail = "138034000+polly-updater-bot[bot]@users.noreply.github.com"
$GitUser = "polly-updater-bot[bot]"

git config user.email $GitEmail | Out-Null
git config user.name $GitUser | Out-Null
git remote set-url "${{ github.server_url }}/${{ github.repository }}.git" | Out-Null
git fetch origin | Out-Null
git rev-parse --verify --quiet ("remotes/origin/" + $BranchName) | Out-Null

if ($LASTEXITCODE -eq 0) {
Write-Host "Branch $BranchName already exists."
exit 0
}

git checkout -b $BranchName
git add .
git commit -m "Update the code-snippets in the documentation"
git push -u origin $BranchName
"updated-docs=true" >> $env:GITHUB_OUTPUT

- name: Generate GitHub application token
if: steps.update-docs.outputs.updated-docs == 'true'
id: generate-application-token
uses: peter-murray/workflow-application-token-action@8e1ba3bf1619726336414f1014e37f17fbadf1db # v2.1.0
with:
application_id: ${{ secrets.POLLY_UPDATER_BOT_APP_ID }}
application_private_key: ${{ secrets.POLLY_UPDATER_BOT_KEY }}
permissions: "contents:write, pull_requests:write"

- name: Create pull request
if: steps.update-docs.outputs.updated-docs == 'true'
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
with:
github-token: ${{ steps.generate-application-token.outputs.token }}
script: |
const { repo, owner } = context.repo;
const workflowUrl = `${{ github.server_url }}/${owner}/${repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
const branchName = "${{ steps.update-docs.outputs.branchName }}";
const result = await github.rest.pulls.create({
title: 'Update the code-snippets in the documentation',
owner,
repo,
head: branchName,
base: 'main',
body: [
'This PR updates the code-snippets in the documentation.',
'',
`This pull request was generated by [GitHub Actions](${workflowUrl}).`
].join('\n')
});