GuilhermeStracini/POC-GHActions-CI-NetFramework

View on GitHub
.github/workflows/update-packages.yml

Summary

Maintainability
Test Coverage
name: Update packages

on:
  workflow_dispatch:
  schedule:
    - cron: "0 2 * * 0"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:

  update_packages:
    name: Update packages
    runs-on: windows-latest
    outputs:
      branchExists: ${{ steps.verify-changed-files.outputs.files_changed }}

    steps:

      - name: Generate a token
        id: generate_token
        uses: tibdex/github-app-token@v2.1.0
        with:
          app_id: ${{ secrets.APP_ID }}
          private_key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ steps.generate_token.outputs.token }}

      - name: Set solution name
        run: |
          echo "solution=$(([io.fileinfo]$(Get-ChildItem -Path .\* -Include *.sln)).name)" | Out-File -FilePath $env:GITHUB_ENV -Append

      - name: Add MSBuild to PATH
        uses: microsoft/setup-msbuild@v2
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Setup Nuget
        uses: nuget/setup-nuget@v2
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Create new branch
        run: git branch features/update_dependencies_${{ github.run_number }}

      - name: Switch branch
        run: git checkout features/update_dependencies_${{ github.run_number }}

      - name: NuGet restore
        run: nuget restore

      - name: Update NuGet packages
        run: nuget update "${{ env.solution }}" -FileConflictAction Ignore

      - name: Setup GIT config
        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: Verify Changed files
        uses: tj-actions/verify-changed-files@v20
        id: verify-changed-files

      - name: Commit and Push
        if: steps.verify-changed-files.outputs.files_changed == 'true'
        run: |
          git add .
          git commit -a -m "Packages update (CI)"
          git push

  create_pull_request:
    name: Create pull request
    runs-on: ubuntu-latest
    needs: update_packages
    if: needs.update_packages.outputs.branchExists == 'true'

    steps:

      - name: Generate a token
        id: generate_token
        uses: tibdex/github-app-token@v2.1.0
        with:
          app_id: ${{ secrets.APP_ID }}
          private_key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ steps.generate_token.outputs.token }}
          ref: features/update_dependencies_${{ github.run_number }}

      - name: Create pull request
        uses: devops-infra/action-pull-request@v0.5.5
        with:
          github_token: ${{ steps.generate_token.outputs.token }}
          source_branch: features/update_dependencies_${{ github.run_number }}
          target_branch: main
          title: Update NuGet packages
          assignee: guibranco
          reviewer: guibranco
          label: dependencies,.NET,infra,nuget,packages
          template: .github/PULL_REQUEST_TEMPLATE/FEATURE.md
          get_diff: true