From d89cb3f3f6a3c8b35f83e7e5d1f13bd0c8cf050a Mon Sep 17 00:00:00 2001 From: Matt Edwards Date: Fri, 21 Jun 2024 15:23:39 -0400 Subject: [PATCH] update github actions (#17) --- .github/workflows/create-prerelease.yml | 97 ------------------------- .github/workflows/create-release.yml | 36 ++++++--- .github/workflows/publish.yml | 25 ++++++- .github/workflows/update-version.yml | 78 ++++++++++++++++++++ 4 files changed, 127 insertions(+), 109 deletions(-) delete mode 100644 .github/workflows/create-prerelease.yml create mode 100644 .github/workflows/update-version.yml diff --git a/.github/workflows/create-prerelease.yml b/.github/workflows/create-prerelease.yml deleted file mode 100644 index 5f84a44..0000000 --- a/.github/workflows/create-prerelease.yml +++ /dev/null @@ -1,97 +0,0 @@ -name: Create Prerelease - -on: - workflow_dispatch: - inputs: - version_type: - description: 'Update branch version by:' - type: choice - options: - - major - - minor - - patch - required: true - default: 'patch' - is_draft: - description: 'Create a draft of the release:' - type: boolean - required: true - default: false - -env: - IS_PRERELEASE: ${{ startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/heads/hotfix/') }} - -jobs: - update-version: - runs-on: ubuntu-latest - outputs: - version_tag: ${{ env.version_tag }} - previous_version_tag: ${{ env.previous_version_tag }} - - steps: - - name: Check For Main - if: env.IS_PRERELEASE == false - run: | - echo "This workflow should not be triggered on the main branch, please use development or hotfix" - exit 1 - - - name: Checkout Code - uses: actions/checkout@v4 - - - name: Run Update Version - id: set_version - shell: pwsh - run: | - Import-Module ./solution-helper.psm1 -Force - $previousVersion, $newVersion = Update-Version -type ${{ github.event.inputs.version_type }} - echo "version_tag=$newVersion" | Out-File -FilePath $env:GITHUB_ENV -Append - echo "previous_version_tag=$previousVersion" | Out-File -FilePath $env:GITHUB_ENV -Append - - - name: Check for Existing Release - run: | - # Fetch the list of releases - releases=$(gh release list --json createdAt,tagName --limit 100) - - echo -e "$releases" - - # Sort the releases by date and extract the most recent one that matches the version or version-prerelease - release_line=$(echo "$releases" | jq -r 'sort_by(.createdAt) | reverse | .[0] | select(.tagName | test("-prerelease$")) | .tagName') - - echo -e "$release_line" - - # Check if the version or version-prerelease already exists - if [[ -n "$release_line" ]]; then - echo "⛔ Prerelease for '$previous_version_tag' already exists, please remove the release or fix the version." - echo -e " - Failed on version: '$release_line'" - exit 1 - else - echo "✅ $previous_version_tag-prerelease does not exist" - fi - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Update Version Number - run: | - git config --global user.name 'github-actions' - git config --global user.email 'github-actions@github.com' - git commit -am "Previous version was '${{ env.previous_version_tag }}'. Version now '${{ env.version_tag }}'." - git push - - create-release: - needs: update-version - runs-on: ubuntu-latest - - steps: - - name: Checkout Code - uses: actions/checkout@v4 - - - name: Create Release - run: | - TAG_NAME="${{ needs.update-version.outputs.version_tag }}-prerelease" - if [[ ${{ github.event.inputs.is_draft }} ]]; then - gh release create $TAG_NAME --target ${{ github.ref_name }} --title $TAG_NAME --generate-notes --draft --prerelease - else - gh release create $TAG_NAME --target ${{ github.ref_name }} --title $TAG_NAME --generate-notes --prerelease - fi - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index b7131a5..5debdba 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -3,21 +3,29 @@ name: Create Release on: workflow_dispatch: inputs: + is_prerelease: + description: 'Create a prerelease:' + type: boolean + required: true + default: false is_draft: - description: 'Create a draft of the release:' + description: 'Set as a draft:' type: boolean required: true default: false +env: + ALLOW_PRERELEASE: ${{ startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/heads/hotfix/') }} + jobs: create-release: runs-on: ubuntu-latest steps: - - name: Fail if branch is not main - if: github.event_name == 'workflow_dispatch' && github.ref != 'refs/heads/main' + - name: Check For Valid Prerelease + if: ${{ env.ALLOW_PRERELEASE == 'true' && github.event.inputs.is_prerelease == 'false' || github.ref == 'refs/heads/main'}} run: | - echo "This workflow should not be triggered with workflow_dispatch on a branch other than main" + echo "Prereleases should not be triggered on the main branch, please use development or hotfix" exit 1 - name: Checkout Code @@ -29,14 +37,22 @@ jobs: run: | Import-Module ./solution-helper.psm1 -Force $version = Get-Version - echo "version_tag=$version" | Out-File -FilePath $env:GITHUB_ENV -Append + if ("${{ github.event.inputs.is_prerelease }}" -eq "true") { + $version_tag = "$version-develop.$(date +'%y%m%d%H%M%S')" + } else { + $version_tag = $version + } + echo "version_tag=$version_tag" | Out-File -FilePath $env:GITHUB_ENV -Append - name: Create Release run: | - if [[ ${{ github.event.inputs.is_draft }} ]]; then - gh release create ${{ env.version_tag }} --target ${{ github.ref_name }} --title ${{ env.version_tag }} --generate-notes --draft --latest - else - gh release create ${{ env.version_tag }} --target ${{ github.ref_name }} --title ${{ env.version_tag }} --generate-notes --latest - fi + echo "🎁 Creating release ${{ env.version_tag }}" + gh release create ${{ env.version_tag }} \ + --target ${{ github.ref_name }} \ + --title ${{ env.version_tag }} \ + --generate-notes \ + $(if [[ "${{ github.event.inputs.is_draft }}" == "true" ]]; then echo "--draft"; fi) \ + $(if [[ "${{ github.event.inputs.is_prerelease }}" == "true" ]]; then echo "--prerelease"; fi) \ + $(if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then echo "--latest"; fi) env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 4960dec..dc13585 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -22,13 +22,11 @@ jobs: if: ${{ env.BRANCH_NAME == 'main' && (github.event.release.prerelease == false || github.event_name == 'workflow_dispatch') }} run: | echo "BUILD_CONFIGURATION=Release" >> $GITHUB_ENV - echo "VERSION_SUFFIX=" >> $GITHUB_ENV - name: Debug Configuration if: ${{ (github.event.release.prerelease || github.event_name == 'workflow_dispatch') }} run: | echo "BUILD_CONFIGURATION=Debug" >> $GITHUB_ENV - echo "VERSION_SUFFIX=develop.$(date +'%y%m%d%H%M%S')" >> $GITHUB_ENV - name: Check Build Configuration if: ${{ env.BUILD_CONFIGURATION == '' }} @@ -39,6 +37,29 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 + - name: Get Current Version + id: get_version + shell: pwsh + run: | + Import-Module ./solution-helper.psm1 -Force + $build_version = Get-Version + echo "build_version=$build_version" | Out-File -FilePath $env:GITHUB_ENV -Append + + - name: Get Version Suffix + id: get_version_suffix + shell: bash + run: | + # Fetch the list of releases + releases=$(gh release list --json createdAt,tagName --limit 100) + + # Filter the releases based on the starting version number, sort them by date, and extract the most recent one + latest_release=$(echo "$releases" | jq -r --arg build_version "$build_version" 'map(select(.tagName | startswith($build_version))) | sort_by(.createdAt) | reverse | .[0] | .tagName') + + version_suffix=${latest_release#*$build_version-} + echo "VERSION_SUFFIX=$version_suffix" >> $GITHUB_ENV + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Setup .NET uses: actions/setup-dotnet@v4 with: diff --git a/.github/workflows/update-version.yml b/.github/workflows/update-version.yml new file mode 100644 index 0000000..c6758c9 --- /dev/null +++ b/.github/workflows/update-version.yml @@ -0,0 +1,78 @@ +name: Update Version + +on: + workflow_dispatch: + inputs: + version_type: + description: 'Update branch version by:' + type: choice + options: + - major + - minor + - patch + required: true + default: 'patch' + +env: + ALLOW_UPDATES: ${{ startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/heads/hotfix/') }} + +jobs: + update-version: + runs-on: ubuntu-latest + outputs: + version_tag: ${{ env.version_tag }} + previous_version_tag: ${{ env.previous_version_tag }} + + steps: + - name: Check For Valid Updates + if: env.ALLOW_UPDATES == false + run: | + echo "Version updates should only be done on development or hotfix" + exit 1 + + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Run Update Version + id: set_version + shell: pwsh + run: | + Import-Module ./solution-helper.psm1 -Force + $previousVersion, $newVersion = Update-Version -type ${{ github.event.inputs.version_type }} + echo "version_tag=$newVersion" | Out-File -FilePath $env:GITHUB_ENV -Append + echo "previous_version_tag=$previousVersion" | Out-File -FilePath $env:GITHUB_ENV -Append + + - name: Check for Existing Release + run: | + compare_versions() { + echo -e "$1\n$2" | sort -V | tail -n 1 + } + + # Fetch the list of releases + releases=$(gh release list --json createdAt,tagName --limit 100) + echo -e "$releases" + + # Sort the releases by date and extract the most recent one + latest_release=$(echo "$releases" | jq -r 'sort_by(.createdAt) | reverse | .[0] | .tagName') + echo -e "$latest_release" + + greater_version=$(compare_versions $latest_release $version_tag) + + if [ "$greater_version" = "$version_tag" ]; then + echo "✅ $version_tag is greater than $latest_release" + elif [ "$greater_version" = "$latest_release" ]; then + echo "⛔ $version_tag is less than $latest_release" + exit 1 + else + echo "⚠️ Versions are equal" + exit 1 + fi + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Update Version Number + run: | + git config --global user.name '${{ github.triggering_actor }}' + git config --global user.email '${{ github.triggering_actor }}@users.noreply.github.com' + git commit -am "Previous version was '${{ env.previous_version_tag }}'. Version now '${{ env.version_tag }}'." + git push \ No newline at end of file