|
| 1 | +name: CLI Release Staging |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: ['main'] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: cli-staging-release |
| 9 | + cancel-in-progress: false |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + prepare-and-commit-staging: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: contains(github.event.pull_request.title, '[codebuff-cli]') |
| 18 | + outputs: |
| 19 | + new_version: ${{ steps.bump_version.outputs.new_version }} |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + ref: ${{ github.event.pull_request.head.sha }} |
| 25 | + |
| 26 | + - name: Set up Bun |
| 27 | + uses: oven-sh/setup-bun@v2 |
| 28 | + with: |
| 29 | + bun-version: '1.2.16' |
| 30 | + |
| 31 | + - name: Cache dependencies |
| 32 | + uses: actions/cache@v4 |
| 33 | + with: |
| 34 | + path: | |
| 35 | + node_modules |
| 36 | + */node_modules |
| 37 | + key: ${{ runner.os }}-deps-${{ hashFiles('**/bun.lockb', '**/package.json') }} |
| 38 | + restore-keys: | |
| 39 | + ${{ runner.os }}-deps-${{ hashFiles('**/bun.lockb') }} |
| 40 | + ${{ runner.os }}-deps- |
| 41 | +
|
| 42 | + - name: Install dependencies |
| 43 | + run: bun install --frozen-lockfile |
| 44 | + |
| 45 | + - name: Calculate staging version |
| 46 | + id: bump_version |
| 47 | + env: |
| 48 | + GITHUB_TOKEN: ${{ secrets.CODEBUFF_GITHUB_TOKEN }} |
| 49 | + run: | |
| 50 | + cd cli/release-staging |
| 51 | +
|
| 52 | + BASE_VERSION=$(node -e "console.log(require('./package.json').version)") |
| 53 | + echo "Base version: $BASE_VERSION" |
| 54 | +
|
| 55 | + echo "Fetching latest CLI prerelease from GitHub..." |
| 56 | + RELEASES_JSON=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 57 | + "https://api.github.com/repos/CodebuffAI/codebuff/releases?per_page=100") |
| 58 | +
|
| 59 | + LATEST_BETA=$(echo "$RELEASES_JSON" | jq -r '.[] | select(.prerelease == true and (.name // "" | test("Codebuff CLI v"))) | .tag_name' | sort -V | tail -n 1) |
| 60 | +
|
| 61 | + if [ "$LATEST_BETA" = "null" ]; then |
| 62 | + LATEST_BETA="" |
| 63 | + fi |
| 64 | +
|
| 65 | + if [ -z "$LATEST_BETA" ]; then |
| 66 | + echo "No existing CLI beta releases found, starting with beta.1" |
| 67 | + NEW_VERSION="${BASE_VERSION}-beta.1" |
| 68 | + else |
| 69 | + echo "Latest CLI beta tag: $LATEST_BETA" |
| 70 | + LATEST_VERSION=${LATEST_BETA#v} |
| 71 | + LATEST_BASE=$(echo "$LATEST_VERSION" | sed 's/-beta\..*$//') |
| 72 | + LATEST_BETA_NUM=$(echo "$LATEST_VERSION" | sed 's/.*-beta\.//') |
| 73 | +
|
| 74 | + if [ "$LATEST_BASE" = "$BASE_VERSION" ]; then |
| 75 | + NEXT=$((LATEST_BETA_NUM + 1)) |
| 76 | + NEW_VERSION="${BASE_VERSION}-beta.${NEXT}" |
| 77 | + else |
| 78 | + echo "Base version changed, resetting beta counter" |
| 79 | + NEW_VERSION="${BASE_VERSION}-beta.1" |
| 80 | + fi |
| 81 | + fi |
| 82 | +
|
| 83 | + echo "New staging version: $NEW_VERSION" |
| 84 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 85 | +
|
| 86 | + node -e " |
| 87 | + const fs = require('fs'); |
| 88 | + const path = require('path'); |
| 89 | + const version = '$NEW_VERSION'; |
| 90 | + const stagingPath = path.join(process.cwd(), 'package.json'); |
| 91 | + const stagingPkg = JSON.parse(fs.readFileSync(stagingPath, 'utf8')); |
| 92 | + stagingPkg.version = version; |
| 93 | + fs.writeFileSync(stagingPath, JSON.stringify(stagingPkg, null, 2) + '\n'); |
| 94 | + const rootPkgPath = path.join(process.cwd(), '..', 'package.json'); |
| 95 | + const rootPkg = JSON.parse(fs.readFileSync(rootPkgPath, 'utf8')); |
| 96 | + rootPkg.version = version; |
| 97 | + fs.writeFileSync(rootPkgPath, JSON.stringify(rootPkg, null, 2) + '\n'); |
| 98 | + " |
| 99 | +
|
| 100 | + - name: Configure git |
| 101 | + run: | |
| 102 | + git config --global user.name "github-actions[bot]" |
| 103 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 104 | +
|
| 105 | + - name: Commit staging release snapshot |
| 106 | + run: | |
| 107 | + git add -A |
| 108 | + git commit -m "Staging CLI Release v${{ steps.bump_version.outputs.new_version }} [codebuff-cli] |
| 109 | +
|
| 110 | + Captures the staged state for the CLI prerelease, including the version bump. |
| 111 | +
|
| 112 | + 🤖 Generated with Codebuff |
| 113 | + Co-Authored-By: Codebuff <noreply@codebuff.com>" |
| 114 | +
|
| 115 | + - name: Create and push staging tag |
| 116 | + run: | |
| 117 | + git tag "v${{ steps.bump_version.outputs.new_version }}" |
| 118 | + git push origin "v${{ steps.bump_version.outputs.new_version }}" |
| 119 | +
|
| 120 | + - name: Upload staging metadata |
| 121 | + uses: actions/upload-artifact@v4 |
| 122 | + with: |
| 123 | + name: cli-staging-metadata |
| 124 | + path: cli/release-staging/ |
| 125 | + |
| 126 | + build-staging-binaries: |
| 127 | + needs: prepare-and-commit-staging |
| 128 | + uses: ./.github/workflows/cli-release-build.yml |
| 129 | + with: |
| 130 | + binary-name: codebuff-cli |
| 131 | + new-version: ${{ needs.prepare-and-commit-staging.outputs.new_version }} |
| 132 | + artifact-name: cli-staging-metadata |
| 133 | + checkout-ref: ${{ github.event.pull_request.head.sha }} |
| 134 | + env-overrides: '{}' |
| 135 | + secrets: inherit |
| 136 | + |
| 137 | + create-staging-release: |
| 138 | + needs: [prepare-and-commit-staging, build-staging-binaries] |
| 139 | + runs-on: ubuntu-latest |
| 140 | + steps: |
| 141 | + - uses: actions/checkout@v4 |
| 142 | + with: |
| 143 | + ref: ${{ github.event.pull_request.head.sha }} |
| 144 | + |
| 145 | + - name: Clean up old CLI prereleases |
| 146 | + run: | |
| 147 | + ONE_WEEK_AGO=$(date -d '7 days ago' -u +%Y-%m-%dT%H:%M:%SZ) |
| 148 | + echo "Removing CLI prereleases older than: $ONE_WEEK_AGO" |
| 149 | +
|
| 150 | + RELEASES=$(curl -s -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \ |
| 151 | + "https://api.github.com/repos/CodebuffAI/codebuff/releases?per_page=100") |
| 152 | +
|
| 153 | + if echo "$RELEASES" | jq -e . >/dev/null 2>&1; then |
| 154 | + OLD=$(echo "$RELEASES" | jq -r '.[] | select(.prerelease == true and .created_at < "'$ONE_WEEK_AGO'" and (.tag_name | test("^v[0-9].*-beta\\.[0-9]+$"))) | "\(.id):\(.tag_name)"') |
| 155 | +
|
| 156 | + if [ -n "$OLD" ]; then |
| 157 | + echo "Deleting old prereleases:" |
| 158 | + echo "$OLD" |
| 159 | + echo "$OLD" | while IFS=: read -r RELEASE_ID TAG_NAME; do |
| 160 | + curl -s -X DELETE \ |
| 161 | + -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \ |
| 162 | + "https://api.github.com/repos/CodebuffAI/codebuff/releases/$RELEASE_ID" |
| 163 | + done |
| 164 | + else |
| 165 | + echo "No stale prereleases found." |
| 166 | + fi |
| 167 | + else |
| 168 | + echo "Failed to parse releases response:" |
| 169 | + echo "$RELEASES" | head -20 |
| 170 | + fi |
| 171 | +
|
| 172 | + - name: Download all binary artifacts |
| 173 | + uses: actions/download-artifact@v4 |
| 174 | + with: |
| 175 | + path: binaries/ |
| 176 | + |
| 177 | + - name: Download staging metadata |
| 178 | + uses: actions/download-artifact@v4 |
| 179 | + with: |
| 180 | + name: cli-staging-metadata |
| 181 | + path: cli/release-staging/ |
| 182 | + |
| 183 | + - name: Create GitHub prerelease |
| 184 | + env: |
| 185 | + VERSION: ${{ needs.prepare-and-commit-staging.outputs.new_version }} |
| 186 | + run: | |
| 187 | + CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 188 | + RELEASE_BODY=$(cat <<EOF |
| 189 | +## Codebuff CLI v${VERSION} (Staging) |
| 190 | + |
| 191 | +**⚠️ This is a staging build intended for internal testing.** |
| 192 | + |
| 193 | +### Included Binaries |
| 194 | +- \`codebuff-cli-linux-x64.tar.gz\` |
| 195 | +- \`codebuff-cli-linux-arm64.tar.gz\` |
| 196 | +- \`codebuff-cli-darwin-x64.tar.gz\` |
| 197 | +- \`codebuff-cli-darwin-arm64.tar.gz\` |
| 198 | +- \`codebuff-cli-win32-x64.tar.gz\` |
| 199 | + |
| 200 | +After downloading, extract the tarball, add the binary to your PATH, and run \`codebuff-cli --help\` for usage. |
| 201 | +EOF |
| 202 | +) |
| 203 | + |
| 204 | + curl -s -X POST \ |
| 205 | + -H "Accept: application/vnd.github.v3+json" \ |
| 206 | + -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \ |
| 207 | + -H "Content-Type: application/json" \ |
| 208 | + https://api.github.com/repos/CodebuffAI/codebuff/releases \ |
| 209 | + -d "{ |
| 210 | + \"tag_name\": \"v${VERSION}\", |
| 211 | + \"name\": \"Codebuff CLI v${VERSION} (Staging)\", |
| 212 | + \"body\": \"${RELEASE_BODY//$'\n'/\\n}\", |
| 213 | + \"prerelease\": true, |
| 214 | + \"published_at\": \"$CURRENT_TIME\" |
| 215 | + }" |
| 216 | + |
| 217 | + - name: Upload release assets |
| 218 | + env: |
| 219 | + VERSION: ${{ needs.prepare-and-commit-staging.outputs.new_version }} |
| 220 | + run: | |
| 221 | + RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \ |
| 222 | + "https://api.github.com/repos/CodebuffAI/codebuff/releases/tags/v${VERSION}" | jq -r '.id') |
| 223 | +
|
| 224 | + if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then |
| 225 | + echo "Failed to resolve release ID for v${VERSION}" |
| 226 | + exit 1 |
| 227 | + fi |
| 228 | +
|
| 229 | + for file in binaries/*/codebuff-cli-*; do |
| 230 | + if [ -f "$file" ]; then |
| 231 | + FILENAME=$(basename "$file") |
| 232 | + echo "Uploading $FILENAME" |
| 233 | + curl -s -X POST \ |
| 234 | + -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \ |
| 235 | + -H "Content-Type: application/octet-stream" \ |
| 236 | + --data-binary @"$file" \ |
| 237 | + "https://uploads.github.com/repos/CodebuffAI/codebuff/releases/$RELEASE_ID/assets?name=$FILENAME" |
| 238 | + fi |
| 239 | + done |
0 commit comments