Skip to content

Commit 807d034

Browse files
saikumarrsCopilot
andauthored
chore(release): create remote branches via API (#2816)
* ci(release): create release branch on remote via API - Add GH_TOKEN to Create release branch step for gh api - Create remote branch with POST to git/refs before later steps - Ensures branch exists for signed-commit action Made-with: Cursor * ci(release): create hotfix remote branch via gh api in script - Move remote branch creation from workflow into create-hotfix-branch.sh - Script uses gh api (POST/PATCH refs) with GH_TOKEN and REPO env - Workflow passes GH_TOKEN and REPO; remove duplicate API step Made-with: Cursor * ci(release): reset to ORIG_HEAD, drop early remote ref create - Use git reset --hard ORIG_HEAD so release branch is from source (e.g. develop) only - Remove create-remote-ref step from Create release branch; rely on Create release branch on remote Made-with: Cursor * chore: remove redundant change * chore: use github app token * chore: Update scripts/create-hotfix-branch.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9410769 commit 807d034

3 files changed

Lines changed: 51 additions & 23 deletions

File tree

.github/workflows/create-hotfix-branch.yml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@ jobs:
2121
name: Create a new hotfix branch
2222
runs-on: [self-hosted, Linux, X64]
2323
needs: validate-actor
24+
permissions:
25+
contents: write # to create branch ref via API
2426
steps:
2527
- name: Harden the runner (Audit all outbound calls)
2628
uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0
2729
with:
2830
egress-policy: audit
2931

32+
- name: Generate GitHub App Token
33+
id: generate-token
34+
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
35+
with:
36+
app-id: ${{ vars.RELEASE_APP_ID }}
37+
private-key: ${{ secrets.RELEASE_PRIVATE_KEY }}
38+
permission-contents: write # to create branch ref via API
39+
3040
- name: Checkout source code
3141
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3242
with:
3343
fetch-depth: 0
34-
35-
# In order to make a commit, we need to initialize a user.
36-
# You may choose to write something less generic here if you want, it doesn't matter functionality wise.
37-
- name: Initialize mandatory git config
38-
run: |
39-
git config user.name "GitHub actions"
40-
git config user.email noreply@github.com
44+
token: ${{ steps.generate-token.outputs.token }}
4145

4246
- name: Create hotfix branch
47+
env:
48+
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
49+
REPO: ${{ github.repository }}
4350
run: |
4451
chmod +x scripts/create-hotfix-branch.sh
4552
./scripts/create-hotfix-branch.sh "${{ github.event.inputs.hotfix_branch_name }}"

.github/workflows/draft-new-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
9595
npm run bump-version:monorepo
9696
new_version=$(jq -r .version package.json)
97-
git reset --hard
97+
git reset --hard ORIG_HEAD
9898
9999
branch_name="${release_type}/${new_version}-${{ github.event.inputs.release_ticket_id }}"
100100

scripts/create-hotfix-branch.sh

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,49 @@ if [ ${#FULL_BRANCH_NAME} -gt 255 ]; then
3838
error "Full branch name ($FULL_BRANCH_NAME) is too long (maximum 255 characters)"
3939
fi
4040

41-
# Check if branch already exists
41+
# Check if branch already exists locally
4242
if git show-ref --verify --quiet refs/heads/$FULL_BRANCH_NAME; then
43-
error "Branch ($FULL_BRANCH_NAME) already exists"
43+
error "Branch ($FULL_BRANCH_NAME) already exists locally"
4444
fi
4545

46-
# Create the branch
46+
# Create the branch locally
4747
git checkout -b $FULL_BRANCH_NAME
4848

49-
# Push the branch and handle potential errors
50-
if ! git push origin $FULL_BRANCH_NAME; then
51-
error "Failed to push branch ($FULL_BRANCH_NAME) to remote. Please check your permissions and try again."
49+
# Ensure required environment variables for GitHub API are present
50+
if [ -z "$GH_TOKEN" ]; then
51+
error "GH_TOKEN environment variable is required to create remote branch via GitHub API"
5252
fi
5353

54-
# Verify the branch exists remotely
55-
if ! git ls-remote --heads origin $FULL_BRANCH_NAME | grep -q $FULL_BRANCH_NAME; then
56-
error "Branch ($FULL_BRANCH_NAME) was not pushed to remote successfully"
54+
# Determine repository slug (owner/repo)
55+
REPO_SLUG="${REPO:-$GITHUB_REPOSITORY}"
56+
if [ -z "$REPO_SLUG" ]; then
57+
# Fallback to parsing from git remote if not provided
58+
REMOTE_URL=$(git config --get remote.origin.url)
59+
REMOTE_URL=${REMOTE_URL#git@github.com:}
60+
REMOTE_URL=${REMOTE_URL#https://github.com/}
61+
REPO_SLUG=${REMOTE_URL%.git}
5762
fi
5863

59-
# Get the repository URL
60-
REPO_URL=$(git config --get remote.origin.url)
61-
REPO_URL=${REPO_URL#git@github.com:}
62-
REPO_URL=${REPO_URL%.git}
64+
if [ -z "$REPO_SLUG" ]; then
65+
error "Unable to determine repository slug for GitHub API"
66+
fi
67+
68+
# Create or update the remote branch ref via GitHub API before any future commits
69+
sha=$(git rev-parse HEAD)
70+
if gh api "repos/${REPO_SLUG}/git/ref/heads/${FULL_BRANCH_NAME}" >/dev/null 2>&1; then
71+
gh api "repos/${REPO_SLUG}/git/refs/heads/${FULL_BRANCH_NAME}" \
72+
--method PATCH \
73+
-f "sha=${sha}" \
74+
-F "force=true"
75+
else
76+
gh api "repos/${REPO_SLUG}/git/refs" \
77+
--method POST \
78+
-f "ref=refs/heads/${FULL_BRANCH_NAME}" \
79+
-f "sha=${sha}"
80+
fi
81+
82+
# Get the repository URL for success message
83+
REPO_URL="https://github.com/$REPO_SLUG"
6384

64-
success "Successfully created hotfix branch!"
65-
echo "Branch URL: https://github.com/$REPO_URL/tree/$FULL_BRANCH_NAME"
85+
success "Successfully created hotfix branch locally and ensured remote branch via GitHub API."
86+
echo "Branch URL: $REPO_URL/tree/$FULL_BRANCH_NAME"

0 commit comments

Comments
 (0)