Skip to content

Instantly share code, notes, and snippets.

@guibranco
Last active September 17, 2023 20:25
Show Gist options
  • Save guibranco/f9fb20ae379ccf40d9ed4c24540b143e to your computer and use it in GitHub Desktop.
Save guibranco/f9fb20ae379ccf40d9ed4c24540b143e to your computer and use it in GitHub Desktop.
A GitHub action to build (debug) .NET framework project with some comments/reports on the Pull Request
name: Build & Bump Version
on:
push:
branches:
- '*'
- '*/*'
- '**'
- '!main'
paths-ignore:
- "**/Properties/AssemblyInfo.cs"
- "**/Properties/Version.txt"
workflow_dispatch:
concurrency:
group: bump-version
cancel-in-progress: true
env:
GHA_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
jobs:
updateVersion:
name: Build & Patch
runs-on: self-hosted
permissions:
pull-requests: write
steps:
- name: Generate a token
id: generate_token
uses: tibdex/github-app-token@0914d50df753bbc42180d982a6550f195390069f
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Update PR with comment (build started)
uses: mshick/add-pr-comment@v2
with:
repo-token: ${{ steps.generate_token.outputs.token }}
refresh-message-position: true
message-id: 'begin'
message: |
**Build debug & Version bump:** :beginner: [Build started](${{ env.GHA_URL }})
- name: Generate check
uses: LouisBrunner/checks-action@v1.6.2
id: generate_check
if: always()
with:
token: ${{ steps.generate_token.outputs.token }}
name: 'Build & Bump Version'
status: 'in_progress'
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ steps.generate_token.outputs.token }}
ref: ${{ github.event.pull_request.head.ref }}
- name: Check for changes in Src/ directory
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
src:
- "Src/**"
- name: Set solution name
run: |
echo "solution=$([io.path]::GetFileNameWithoutExtension($(Get-ChildItem -Path .\* -Include *.sln)))" | Out-File -FilePath $env:GITHUB_ENV -Append
echo "projectPath=$((Get-ChildItem -Path .\Src\ -Directory | Select-Object -First 1).Name)" | Out-File -FilePath $env:GITHUB_ENV -Append
- name: Update PR with comment (build started solution)
uses: mshick/add-pr-comment@v2
if: steps.changes.outputs.src == 'true'
with:
repo-token: ${{ steps.generate_token.outputs.token }}
refresh-message-position: true
message-id: 'begin'
message: |
**Build debug & Version bump:** :beginner: [Building ${{ env.solution }}.sln](${{ env.GHA_URL }})
- name: Update PR with comment (build started solution)
uses: mshick/add-pr-comment@v2
if: steps.changes.outputs.src == 'false'
with:
repo-token: ${{ steps.generate_token.outputs.token }}
refresh-message-position: true
message-id: 'begin'
message: |
**Build debug & Version bump:** :alien: [Not building ${{ env.solution }}.sln](${{ env.GHA_URL }}), no source files changed.
- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild@v1.3
if: steps.changes.outputs.src == 'true'
with:
msbuild-architecture: x64
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Setup Nuget
uses: nuget/setup-nuget@v1
if: steps.changes.outputs.src == 'true'
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Restore NuGet packages
if: steps.changes.outputs.src == 'true'
run: nuget restore "${{ env.solution }}.sln"
- name: Build Debug
if: steps.changes.outputs.src == 'true'
run: msbuild "${{ env.solution }}.sln" /p:Configuration=Debug -verbosity:detailed -flp1:"logfile=msbuild.errors.log;errorsonly" -flp2:"logfile=msbuild.warnings.log;warningsonly"
- name: Set version from file
if: steps.changes.outputs.src == 'true'
run: |
echo "version=$(type Src/${{ env.projectPath }}/Properties/Version.txt)" | Out-File -FilePath $env:GITHUB_ENV -Append
- name: Read warnings.txt
uses: guibranco/github-file-reader-action-v2@latest
if: steps.changes.outputs.src == 'true'
id: warnings
with:
path: msbuild.warnings.log
- name: Read errors.txt
uses: guibranco/github-file-reader-action-v2@latest
if: failure() && steps.changes.outputs.src == 'true'
id: errors
with:
path: msbuild.errors.log
- name: Update PR with comment (version)
uses: mshick/add-pr-comment@v2
if: steps.changes.outputs.src == 'true'
with:
repo-token: ${{ steps.generate_token.outputs.token }}
refresh-message-position: true
message-id: 'version'
message: |
**Build debug & Version bump:** :dart: [Build succeeded](${{ env.GHA_URL }}) - New version: ${{ env.version }}
- name: Set check as successfully before new commit
uses: LouisBrunner/checks-action@v1.6.2
with:
token: ${{ steps.generate_token.outputs.token }}
conclusion: 'success'
check_id: ${{ steps.generate_check.outputs.check_id }}
output: |
{"summary":"New version: ${{ env.version }}"}
- name: Setup GIT config
if: steps.changes.outputs.src == 'true'
run: |
git config user.name "net-framework-updater[bot]"
git config user.email "136581072+net-framework-updater[bot]@users.noreply.github.com"
git config --global --add --bool push.autoSetupRemote true
- name: Commit and Push
if: steps.changes.outputs.src == 'true'
run: |
git add .
git commit -a -m "Version bump (CI)"
echo "sha1=$(git rev-parse HEAD)" | Out-File -FilePath $env:GITHUB_ENV -Append
git push
- name: Update PR with comment
uses: mshick/add-pr-comment@v2
if: always() && steps.changes.outputs.src == 'true'
with:
repo-token: ${{ steps.generate_token.outputs.token }}
refresh-message-position: true
message-id: 'final'
message: |
**Build debug & Version bump:** :white_check_mark: [Successfully builded and patched](${{ env.GHA_URL }}) **${{ env.solution }}.sln**.
**Version:** :hash: ${{ env.version }}
**Warnings:** :warning:
```
${{ steps.warnings.outputs.contents }}
```
message-failure: |
**Build debug & Version bump:** :x: [Failed](${{ env.GHA_URL }})
**Errors:** :no_entry:
```
${{ steps.errors.outputs.contents }}
```
message-cancelled: |
**Build debug & Version bump:** :o: [Cancelled](${{ env.GHA_URL }})
- name: Set check as successfully after new commit
uses: LouisBrunner/checks-action@v1.6.2
if: steps.changes.outputs.src == 'true'
with:
token: ${{ steps.generate_token.outputs.token }}
conclusion: 'success'
name: 'Build & Bump Version'
output: |
{"summary":"New version: ${{ env.version }}"}
- name: Update check run status (Success)
uses: guibranco/github-status-action-v2@v1.1.7
if: success() && steps.changes.outputs.src == 'true'
with:
authToken: ${{ steps.generate_token.outputs.token }}
context: 'Build & Bump Version'
description: 'Build ${{ env.solution}}.sln - Version: ${{ env.version }}'
state: 'success'
sha: ${{ env.sha1 }}
target_url: ${{ env.GHA_URL }}
- name: Update check run status (Failure or Cancellation)
uses: guibranco/github-status-action-v2@v1.1.7
if: failure() || cancelled()
with:
authToken: ${{ steps.generate_token.outputs.token }}
context: 'Build & Bump Version'
description: ${{ job.status }}
state: 'failure'
sha: ${{ github.event.pull_request.head.sha || github.sha }}
target_url: ${{ env.GHA_URL }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment