|
| 1 | +name: Publish OAuth2Client Package |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + cab_id: |
| 6 | + description: "CAB id for the change/release" |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-and-publish: |
| 12 | + |
| 13 | + runs-on: ubuntu-latest |
| 14 | + environment: prod |
| 15 | + |
| 16 | + outputs: |
| 17 | + # Used to conditionally run publish steps and notifications |
| 18 | + client_version: ${{steps.calc_version.outputs.version}} |
| 19 | + should_publish: ${{steps.check_changes.outputs.has_changes}} |
| 20 | + |
| 21 | + permissions: |
| 22 | + contents: write |
| 23 | + pull-requests: write |
| 24 | + |
| 25 | + steps: |
| 26 | + |
| 27 | + - name: checkout dotnet repo |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + repository: XeroAPI/Xero-NetStandard |
| 31 | + path: Xero-NetStandard |
| 32 | + fetch-depth: 0 # Fetch all history for git diff |
| 33 | + |
| 34 | + - name: Check for changes |
| 35 | + id: check_changes |
| 36 | + run: | |
| 37 | + # Get last client tag |
| 38 | + LAST_CLIENT_TAG=$(git tag -l 'client-*' --sort=-v:refname | head -n 1) |
| 39 | + echo "Found last client tag: $LAST_CLIENT_TAG" |
| 40 | + echo "last_client_tag=$LAST_CLIENT_TAG" >> $GITHUB_OUTPUT |
| 41 | +
|
| 42 | + if [ -z "$LAST_CLIENT_TAG" ]; then |
| 43 | + echo "No previous client tag found. Treating as changed (initial release)." |
| 44 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | +
|
| 48 | + # Check if OAuth2Client files changed since last client tag |
| 49 | + if git diff --name-only $LAST_CLIENT_TAG HEAD | grep -q 'Xero.NetStandard.OAuth2Client/'; then |
| 50 | + echo "Changes detected in Xero.NetStandard.OAuth2Client/" |
| 51 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 52 | + else |
| 53 | + echo "No changes detected in Xero.NetStandard.OAuth2Client/" |
| 54 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 55 | + fi |
| 56 | + working-directory: Xero-NetStandard |
| 57 | + env: |
| 58 | + GH_TOKEN: ${{secrets.GITHUB_TOKEN}} |
| 59 | + |
| 60 | + - name: Calculate OAuth2Client version |
| 61 | + id: calc_version |
| 62 | + if: steps.check_changes.outputs.has_changes == 'true' |
| 63 | + run: | |
| 64 | + LAST_CLIENT_TAG="${{ steps.check_changes.outputs.last_client_tag }}" |
| 65 | + |
| 66 | + if [ -z "$LAST_CLIENT_TAG" ]; then |
| 67 | + # No previous client tag, start with version from csproj |
| 68 | + CURRENT_VERSION=$(grep '<Version>' ./Xero.NetStandard.OAuth2Client/Xero.NetStandard.OAuth2Client.csproj | sed 's/.*<Version>\(.*\)<\/Version>.*/\1/') |
| 69 | + echo "No previous client tag found, using csproj version: $CURRENT_VERSION" |
| 70 | + echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | + |
| 74 | + LAST_VERSION=${LAST_CLIENT_TAG#client-} |
| 75 | + echo "Last OAuth2Client version: $LAST_VERSION (tag: $LAST_CLIENT_TAG)" |
| 76 | + |
| 77 | + # Determine Bump Type from Main SDK tags |
| 78 | + BUMP_TYPE="patch" |
| 79 | + if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then |
| 80 | + CURRENT_MAIN_TAG=$GITHUB_REF_NAME |
| 81 | + # Find previous main tag (exclude current) |
| 82 | + PREV_MAIN_TAG=$(git tag -l '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | grep -v "^$CURRENT_MAIN_TAG$" | head -n 1) |
| 83 | + |
| 84 | + if [ ! -z "$PREV_MAIN_TAG" ]; then |
| 85 | + echo "Main SDK Bump: $PREV_MAIN_TAG -> $CURRENT_MAIN_TAG" |
| 86 | + IFS='.' read -r m1 m2 m3 <<< "$CURRENT_MAIN_TAG" |
| 87 | + IFS='.' read -r p1 p2 p3 <<< "$PREV_MAIN_TAG" |
| 88 | + |
| 89 | + if [ "$m1" != "$p1" ]; then |
| 90 | + BUMP_TYPE="major" |
| 91 | + elif [ "$m2" != "$p2" ]; then |
| 92 | + BUMP_TYPE="minor" |
| 93 | + fi |
| 94 | + fi |
| 95 | + fi |
| 96 | + echo "Detected bump type: $BUMP_TYPE" |
| 97 | + |
| 98 | + # Increment version based on bump type |
| 99 | + IFS='.' read -r c1 c2 c3 <<< "$LAST_VERSION" |
| 100 | + if [ "$BUMP_TYPE" == "major" ]; then |
| 101 | + NEW_VERSION="$((c1 + 1)).0.0" |
| 102 | + elif [ "$BUMP_TYPE" == "minor" ]; then |
| 103 | + NEW_VERSION="${c1}.$((c2 + 1)).0" |
| 104 | + else |
| 105 | + NEW_VERSION="${c1}.${c2}.$((c3 + 1))" |
| 106 | + fi |
| 107 | + |
| 108 | + echo "Incrementing version ($BUMP_TYPE): $LAST_VERSION -> $NEW_VERSION" |
| 109 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 110 | + working-directory: Xero-NetStandard |
| 111 | + env: |
| 112 | + GH_TOKEN: ${{secrets.GITHUB_TOKEN}} |
| 113 | + |
| 114 | + - name: Update csproj version |
| 115 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 116 | + run: | |
| 117 | + NEW_VERSION="${{steps.calc_version.outputs.version}}" |
| 118 | + sed -i "s/<Version>.*<\/Version>/<Version>$NEW_VERSION<\/Version>/" \ |
| 119 | + ./Xero.NetStandard.OAuth2Client/Xero.NetStandard.OAuth2Client.csproj |
| 120 | + echo "Updated csproj to version $NEW_VERSION" |
| 121 | + working-directory: Xero-NetStandard |
| 122 | + |
| 123 | + - name: Setup .NET |
| 124 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 125 | + uses: actions/setup-dotnet@v4 |
| 126 | + with: |
| 127 | + dotnet-version: 8.0.x |
| 128 | + |
| 129 | + - name: Restore dependencies |
| 130 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 131 | + run: dotnet restore |
| 132 | + working-directory: Xero-NetStandard |
| 133 | + |
| 134 | + - name: Build |
| 135 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 136 | + run: dotnet build --no-restore |
| 137 | + working-directory: Xero-NetStandard |
| 138 | + |
| 139 | + - name: Create OAuth2Client Package for Nuget.org |
| 140 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 141 | + run: dotnet pack ./Xero.NetStandard.OAuth2Client/Xero.NetStandard.OAuth2Client.csproj |
| 142 | + working-directory: Xero-NetStandard |
| 143 | + |
| 144 | + - name: Publish OAuth2Client Package to Nuget.org |
| 145 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 146 | + run: dotnet nuget push ./Xero.NetStandard.OAuth2Client/bin/Release/Xero.NetStandard.OAuth2Client.${{steps.calc_version.outputs.version}}.nupkg --api-key ${{ secrets.NUGET_APIKEY }} --source https://api.nuget.org/v3/index.json |
| 147 | + working-directory: Xero-NetStandard |
| 148 | + |
| 149 | + - name: Commit, Push and Tag |
| 150 | + if: steps.calc_version.outputs.should_publish == 'true' |
| 151 | + run: | |
| 152 | + git config user.name "github-actions[bot]" |
| 153 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 154 | + |
| 155 | + # Commit the version bump |
| 156 | + git add ./Xero.NetStandard.OAuth2Client/Xero.NetStandard.OAuth2Client.csproj |
| 157 | + git commit -m "Bump OAuth2Client version to ${{steps.calc_version.outputs.version}}" |
| 158 | + |
| 159 | + # Push the commit to master |
| 160 | + git push origin HEAD:master |
| 161 | + |
| 162 | + # Create and push the tag |
| 163 | + git tag "client-${{steps.calc_version.outputs.version}}" |
| 164 | + git push origin "client-${{steps.calc_version.outputs.version}}" |
| 165 | + working-directory: Xero-NetStandard |
| 166 | + env: |
| 167 | + GH_TOKEN: ${{secrets.GITHUB_TOKEN}} |
| 168 | + |
| 169 | + notify-slack-on-success: |
| 170 | + runs-on: ubuntu-latest |
| 171 | + needs: check-and-publish |
| 172 | + if: success() && needs.check-and-publish.outputs.should_publish == 'true' |
| 173 | + permissions: |
| 174 | + contents: read |
| 175 | + steps: |
| 176 | + - name: Checkout Xero-NetStandard repo |
| 177 | + uses: actions/checkout@v4 |
| 178 | + with: |
| 179 | + repository: XeroAPI/Xero-NetStandard |
| 180 | + path: Xero-NetStandard |
| 181 | + |
| 182 | + - name: Send slack notification on success |
| 183 | + uses: ./Xero-NetStandard/.github/actions/notify-slack |
| 184 | + with: |
| 185 | + heading_text: "OAuth2Client Package publish job has succeeded !" |
| 186 | + alert_type: "thumbsup" |
| 187 | + job_status: "Success" |
| 188 | + XERO_SLACK_WEBHOOK_URL: ${{secrets.XERO_SLACK_WEBHOOK_URL}} |
| 189 | + job_url: "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}" |
| 190 | + button_type: "primary" |
| 191 | + package_version: ${{needs.check-and-publish.outputs.client_version}} |
| 192 | + repo_link: ${{github.server_url}}/${{github.repository}} |
| 193 | + |
| 194 | + notify-slack-on-failure: |
| 195 | + runs-on: ubuntu-latest |
| 196 | + needs: check-and-publish |
| 197 | + if: failure() |
| 198 | + permissions: |
| 199 | + contents: read |
| 200 | + steps: |
| 201 | + - name: Checkout Xero-NetStandard repo |
| 202 | + uses: actions/checkout@v4 |
| 203 | + with: |
| 204 | + repository: XeroAPI/Xero-NetStandard |
| 205 | + path: Xero-NetStandard |
| 206 | + |
| 207 | + - name: Send slack notification on failure |
| 208 | + uses: ./Xero-NetStandard/.github/actions/notify-slack |
| 209 | + with: |
| 210 | + heading_text: "OAuth2Client Package publish job has failed !" |
| 211 | + alert_type: "alert" |
| 212 | + job_status: "Failed" |
| 213 | + XERO_SLACK_WEBHOOK_URL: ${{secrets.XERO_SLACK_WEBHOOK_URL}} |
| 214 | + job_url: "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}" |
| 215 | + button_type: "danger" |
| 216 | + package_version: ${{needs.check-and-publish.outputs.client_version}} |
| 217 | + repo_link: ${{github.server_url}}/${{github.repository}} |
0 commit comments