From 3669169c2d1178aa7b3b2391c944dad6f0fde2f5 Mon Sep 17 00:00:00 2001 From: FrostyApeOne <78855469+FrostyApeOne@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:45:34 +0100 Subject: [PATCH] Updated to trigger CI (#584) * Updated to trigger CI * Updated CI to check the commit message for version indicator and tag if processed * Test version update (#update package version to 1.1.2) * Updated contents permission * Updated to trigger CI * Updated the CI * Updated to trigger CI * Created a new workflow for package deployment * Updated path to limit to PersonsApi.Client only * General refactoring --------- Co-authored-by: Farshad DASHTI --- .github/workflows/build-and-push-package.yml | 90 +++++++++++++++++++ .../continuous-integration-dotnet.yml | 57 +----------- 2 files changed, 91 insertions(+), 56 deletions(-) create mode 100644 .github/workflows/build-and-push-package.yml diff --git a/.github/workflows/build-and-push-package.yml b/.github/workflows/build-and-push-package.yml new file mode 100644 index 00000000..97ab61b1 --- /dev/null +++ b/.github/workflows/build-and-push-package.yml @@ -0,0 +1,90 @@ +name: Build and Push NuGet Package + +on: + push: + branches: + - package/release.* + paths: + - 'Dfe.PersonsApi.Client*/**' + +env: + DOTNET_VERSION: '8.0.x' + +jobs: + build-and-test: + runs-on: ubuntu-latest + permissions: + packages: write + contents: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.ref }} + fetch-depth: 0 # Shallow clones disabled for a better relevancy of SC analysis + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Set up curl and jq + run: sudo apt-get install -y curl jq + + - name: Check for custom version in commit message or check the feed for the latest version and increment it + id: check_custom_version + run: | + # Search the last 10 commits for the version update indicator + COMMIT_HASH=$(git log -n 10 --pretty=format:"%H %s" | grep -P '\(#update package version to \d+\.\d+\.\d+\)' | grep -oP '^\w+' | head -n 1) + + if [[ -n "$COMMIT_HASH" ]]; then + echo "Found commit with version update indicator: $COMMIT_HASH" + + # Check if the commit is already tagged + if git rev-parse "processed-nuget-version-${COMMIT_HASH}" >/dev/null 2>&1; then + echo "This commit has already been processed for version update. Skipping." + else + # Extract the version from the commit message + CUSTOM_VERSION=$(git show -s --format=%s $COMMIT_HASH | grep -oP '\(#update package version to \K([0-9]+\.[0-9]+\.[0-9]+)') + + if [[ -n "$CUSTOM_VERSION" ]]; then + echo "Using custom version: $CUSTOM_VERSION" + echo "NEW_VERSION=$CUSTOM_VERSION" >> $GITHUB_ENV + + # Tag the commit to prevent reprocessing + git tag "processed-nuget-version-${COMMIT_HASH}" + git push origin "processed-nuget-version-${COMMIT_HASH}" + else + echo "Failed to extract version from commit message. Exiting." + exit 1 + fi + fi + fi + + if [[ -z "$CUSTOM_VERSION" ]]; then + echo "No unprocessed custom version found in the last 10 commits. Proceeding to fetch and increment the latest version from the feed." + + # Fetch the latest version and increment the version + PACKAGE_ID="Dfe.PersonsApi.Client" + FEED_URL="https://nuget.pkg.github.com/DFE-Digital/query?q=$PACKAGE_ID" + LATEST_VERSION=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$FEED_URL" | jq -r '.data[0].version') + + if [[ -z "$LATEST_VERSION" || "$LATEST_VERSION" == "null" ]]; then + echo "No existing version found in the feed. Defaulting to version 1.0.0" + NEW_VERSION="1.0.0" + else + echo "Latest version is $LATEST_VERSION" + IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_VERSION" + NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$((VERSION_PARTS[2] + 1))" + echo "Incrementing to new version: $NEW_VERSION" + fi + + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV + fi + shell: /usr/bin/bash -e {0} + + - name: Build, pack and publish + working-directory: Dfe.PersonsApi.Client + run: | + dotnet build -c Release + dotnet pack -c Release -p:PackageVersion=${{ env.NEW_VERSION }} --output . + dotnet nuget push "*.nupkg" --api-key ${{ secrets.GITHUB_TOKEN }} --source https://nuget.pkg.github.com/DFE-Digital/index.json diff --git a/.github/workflows/continuous-integration-dotnet.yml b/.github/workflows/continuous-integration-dotnet.yml index f1daac52..349563ad 100644 --- a/.github/workflows/continuous-integration-dotnet.yml +++ b/.github/workflows/continuous-integration-dotnet.yml @@ -28,7 +28,7 @@ jobs: build-and-test: runs-on: ubuntu-latest permissions: - packages: write + packages: read contents: read steps: - uses: actions/checkout@v4 @@ -98,61 +98,6 @@ jobs: reportgenerator -reports:./**/coverage.cobertura.xml -targetdir:./CoverageReport -reporttypes:SonarQube dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" - - uses: dorny/paths-filter@v3 - id: person-api-changes - with: - filters: | - client: - - 'Dfe.PersonsApi.Client/**' - - - name: Set up curl and jq - run: sudo apt-get install -y curl jq - - - name: Check for custom version in commit message or check the feed for the latest version and increment it - id: check_custom_version - run: | - # Retrieve the last 3 commit messages - COMMIT_MESSAGES=$(git log -n 3 --pretty=format:%B) - - echo "Commit Messages:" - echo "$COMMIT_MESSAGES" - - # Search for custom version update pattern in the last 10 commits - CUSTOM_VERSION=$(echo "$COMMIT_MESSAGES" | grep -oP '\(#update package version to \K([0-9]+\.[0-9]+\.[0-9]+)' | head -n 1) - - if [[ -n "$CUSTOM_VERSION" ]]; then - echo "Using custom version: $CUSTOM_VERSION" - echo "NEW_VERSION=$CUSTOM_VERSION" >> $GITHUB_ENV - else - echo "No custom version found in the commit messages." - # Fetch the latest version and increment the version - PACKAGE_ID="Dfe.PersonsApi.Client" - FEED_URL="https://nuget.pkg.github.com/DFE-Digital/query?q=$PACKAGE_ID" - LATEST_VERSION=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$FEED_URL" | jq -r '.data[0].version') - - if [[ -z "$LATEST_VERSION" ]]; then - echo "No existing version found in the feed. Defaulting to version 1.0.0" - NEW_VERSION="1.0.0" - else - echo "Latest version is $LATEST_VERSION" - IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_VERSION" - NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$((VERSION_PARTS[2] + 1))" - echo "Incrementing to new version: $NEW_VERSION" - fi - - echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV - fi - shell: /usr/bin/bash -e {0} - if: steps.person-api-changes.outputs.client == 'true' && github.event_name != 'pull_request' - - - name: Build, pack and publish - working-directory: Dfe.PersonsApi.Client - run: | - dotnet build -c Release - dotnet pack -c Release -p:PackageVersion=${{ env.NEW_VERSION }} --output . - dotnet nuget push "*.nupkg" --api-key ${{ secrets.GITHUB_TOKEN }} --source https://nuget.pkg.github.com/DFE-Digital/index.json - if: steps.person-api-changes.outputs.client == 'true' && github.event_name != 'pull_request' - - name: Stop containers if: always() run: docker compose -f "docker-compose.yml" down