|
| 1 | +name: NBGV Set Version (CLI) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + target_branch: |
| 7 | + description: 'Branch to update (e.g., develop, main, hotfix/foo, v1.2)' |
| 8 | + type: string |
| 9 | + required: true |
| 10 | + mode: |
| 11 | + description: | |
| 12 | + How to set the version: |
| 13 | + - explicit: set to a specific value (e.g., 1.3-alpha) |
| 14 | + - bump: bump major/minor/patch from current SimpleVersion and apply optional prerelease |
| 15 | + - auto: policy-based (develop -> next minor -alpha; hotfix/* -> next patch -alpha; main/vX.Y -> stable) |
| 16 | + type: string |
| 17 | + required: true |
| 18 | + default: auto |
| 19 | + version: |
| 20 | + description: 'Required when mode=explicit (e.g., 1.3-alpha, 1.2.3, 1.4.0-rc)' |
| 21 | + type: string |
| 22 | + required: false |
| 23 | + increment: |
| 24 | + description: 'When mode=bump: major | minor | patch' |
| 25 | + type: string |
| 26 | + required: false |
| 27 | + default: minor |
| 28 | + prerelease: |
| 29 | + description: 'When mode=bump: prerelease suffix without leading dash (e.g., alpha, beta, rc). Empty for stable.' |
| 30 | + type: string |
| 31 | + required: false |
| 32 | + default: "" |
| 33 | + commit_message: |
| 34 | + description: 'Optional custom commit message' |
| 35 | + type: string |
| 36 | + required: false |
| 37 | + default: "chore(nbgv): set version via CI" |
| 38 | + secrets: |
| 39 | + GH_TOKEN: |
| 40 | + required: true |
| 41 | + |
| 42 | +permissions: |
| 43 | + contents: write |
| 44 | + pull-requests: write |
| 45 | + |
| 46 | +jobs: |
| 47 | + set-version: |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - name: Checkout branch |
| 51 | + uses: actions/checkout@v4 |
| 52 | + with: |
| 53 | + ref: ${{ inputs.target_branch }} |
| 54 | + fetch-depth: 0 |
| 55 | + token: ${{ secrets.GH_TOKEN }} |
| 56 | + |
| 57 | + - name: Install nbgv |
| 58 | + run: dotnet tool update -g nbgv --version 3.8.38-alpha || dotnet tool install -g nbgv --version 3.8.38-alpha |
| 59 | + |
| 60 | + - name: Read current version |
| 61 | + id: cur |
| 62 | + run: | |
| 63 | + nbgv get-version --format json > cur.json |
| 64 | + echo "simple=$(jq -r .SimpleVersion cur.json)" >> $GITHUB_OUTPUT |
| 65 | + echo "pre=$(jq -r .PrereleaseVersion cur.json)" >> $GITHUB_OUTPUT |
| 66 | + echo "nuget=$(jq -r .NuGetPackageVersion cur.json)" >> $GITHUB_OUTPUT |
| 67 | + echo "Current: $(cat cur.json)" |
| 68 | +
|
| 69 | + - name: Decide new version |
| 70 | + id: plan |
| 71 | + env: |
| 72 | + MODE: ${{ inputs.mode }} |
| 73 | + SIMPLE: ${{ steps.cur.outputs.simple }} |
| 74 | + PRE: ${{ steps.cur.outputs.pre }} |
| 75 | + INCR: ${{ inputs.increment }} |
| 76 | + PRENAME: ${{ inputs.prerelease }} |
| 77 | + EXPLICIT: ${{ inputs.version }} |
| 78 | + BR: ${{ inputs.target_branch }} |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | +
|
| 82 | + parse_semver() { |
| 83 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$1" |
| 84 | + # If PATCH missing, default 0 |
| 85 | + PATCH=${PATCH:-0} |
| 86 | + } |
| 87 | +
|
| 88 | + NEW="" |
| 89 | +
|
| 90 | + case "$MODE" in |
| 91 | + explicit) |
| 92 | + if [[ -z "$EXPLICIT" ]]; then |
| 93 | + echo "Version is required when mode=explicit"; exit 1 |
| 94 | + fi |
| 95 | + NEW="$EXPLICIT" |
| 96 | + ;; |
| 97 | +
|
| 98 | + bump) |
| 99 | + parse_semver "$SIMPLE" |
| 100 | + case "$INCR" in |
| 101 | + major) MAJOR=$((MAJOR+1)); MINOR=0; PATCH=0 ;; |
| 102 | + minor) MINOR=$((MINOR+1)); PATCH=0 ;; |
| 103 | + patch) PATCH=$((PATCH+1)) ;; |
| 104 | + *) echo "increment must be major|minor|patch"; exit 1 ;; |
| 105 | + esac |
| 106 | +
|
| 107 | + if [[ -n "$PRENAME" ]]; then |
| 108 | + NEW="${MAJOR}.${MINOR}.${PATCH}-${PRENAME}" |
| 109 | + else |
| 110 | + NEW="${MAJOR}.${MINOR}.${PATCH}" |
| 111 | + fi |
| 112 | + ;; |
| 113 | +
|
| 114 | + auto) |
| 115 | + # develop -> next minor -alpha |
| 116 | + # hotfix/* -> next patch -alpha |
| 117 | + # main or vX.Y -> stable (strip prerelease) |
| 118 | + parse_semver "$SIMPLE" |
| 119 | + if [[ "$BR" == "develop" ]]; then |
| 120 | + if [[ -z "$PRE" ]]; then |
| 121 | + MINOR=$((MINOR+1)) |
| 122 | + fi |
| 123 | + NEW="${MAJOR}.${MINOR}-alpha" |
| 124 | + elif [[ "$BR" =~ ^hotfix/ ]]; then |
| 125 | + if [[ -z "$PRE" ]]; then |
| 126 | + PATCH=$((PATCH+1)) |
| 127 | + fi |
| 128 | + NEW="${MAJOR}.${MINOR}.${PATCH}-alpha" |
| 129 | + elif [[ "$BR" == "main" || "$BR" =~ ^v[0-9]+\.[0-9]+$ ]]; then |
| 130 | + NEW="${MAJOR}.${MINOR}.${PATCH}" |
| 131 | + else |
| 132 | + # default: keep current prerelease if any; if none, apply -alpha bump minor |
| 133 | + if [[ -z "$PRE" ]]; then |
| 134 | + MINOR=$((MINOR+1)) |
| 135 | + NEW="${MAJOR}.${MINOR}-alpha" |
| 136 | + else |
| 137 | + # e.g., already -alpha; keep same simple but ensure suffix |
| 138 | + if [[ "$SIMPLE" =~ ^[0-9]+\.[0-9]+$ ]]; then |
| 139 | + NEW="${SIMPLE}-alpha" |
| 140 | + else |
| 141 | + NEW="${MAJOR}.${MINOR}.${PATCH}-alpha" |
| 142 | + fi |
| 143 | + fi |
| 144 | + fi |
| 145 | + ;; |
| 146 | +
|
| 147 | + *) |
| 148 | + echo "Unsupported mode: $MODE"; exit 1 ;; |
| 149 | + esac |
| 150 | +
|
| 151 | + echo "new=$NEW" >> $GITHUB_OUTPUT |
| 152 | + echo "Planned new version: $NEW" |
| 153 | +
|
| 154 | + - name: Apply new version (nbgv set-version) |
| 155 | + id: apply |
| 156 | + env: |
| 157 | + NEW: ${{ steps.plan.outputs.new }} |
| 158 | + MSG: ${{ inputs.commit_message }} |
| 159 | + run: | |
| 160 | + set -euo pipefail |
| 161 | + nbgv set-version "$NEW" |
| 162 | + git config user.name "${{ github.actor }}" |
| 163 | + git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 164 | + git add version.json |
| 165 | + if git diff --cached --quiet; then |
| 166 | + echo "No change to version.json (already $NEW)." |
| 167 | + echo "changed=0" >> $GITHUB_OUTPUT |
| 168 | + else |
| 169 | + git commit -m "$MSG: $NEW" |
| 170 | + git push origin HEAD:${{ inputs.target_branch }} |
| 171 | + echo "changed=1" >> $GITHUB_OUTPUT |
| 172 | + fi |
| 173 | +
|
| 174 | + - name: Read new version (outputs for callers) |
| 175 | + id: new |
| 176 | + run: | |
| 177 | + nbgv get-version --format json > new.json |
| 178 | + echo "simple=$(jq -r .SimpleVersion new.json)" >> $GITHUB_OUTPUT |
| 179 | + echo "pre=$(jq -r .PrereleaseVersion new.json)" >> $GITHUB_OUTPUT |
| 180 | + echo "nuget=$(jq -r .NuGetPackageVersion new.json)" >> $GITHUB_OUTPUT |
| 181 | + echo "New: $(cat new.json)" |
0 commit comments