The-Standard-Organization / ADotNet

ADotNet is a.NET library that enables software engineers on the .NET platform to develop AzureDevOps pipelines and Git Actions in C#.
98 stars 34 forks source link

RELEASES: Add Git Tag on Merge to Main #58

Closed cjdutoit closed 1 year ago

cjdutoit commented 1 year ago

The Ask

Create a job that will add a Git tag RELEASE to only when the pull request is merged into the main branch.

How will this be used

If a tag can be added on merge, then the tag can be used on other workflow runs i.e. if a build action run on main and it detects a tag RELEASE then we can automatically deploy a nuget package if build & test is a success.

cjdutoit commented 1 year ago

I think we can add a second job to do this. Here is a code sample as a starting point...

jobs:
  add_release_tag:
    if: github.event.pull_request.merged && github.event.pull_request.base.ref == 'main' && startsWith(github.event.pull_request.title, 'RELEASES:') && (contains(github.event.pull_request.labels.*.name, 'RELEASE') || contains(github.event.pull_request.labels.*.name, 'RELEASE'))  

    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Extract Version Number
        id: extract_version
        run: echo "::set-output name=version_number::$(grep -oP '(?<=<Version>)[^<]+' BuildTestApp/BuildTestApp.csproj)"

      - name: Print Version Number
        run: echo "Version number is ${{ steps.extract_version.outputs.version_number }}"

      - name: Configure Git
        run: |
          git config user.name "Add Git Release Tag Action"
          git config user.email "github.action@noreply.github.com"

      - name: Authenticate with GitHub
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT_FOR_TAGGING }}

      - name: Add Git Tag - Release
        run: |
          git tag -a "release-${{ steps.extract_version.outputs.version_number }}" -m "Release ${{ steps.extract_version.outputs.version_number }}"
          git push origin --tags