33set -e
44
55# Configuration
6- GITHUB_REPO=" zast-ai/zast- blog"
6+ GITHUB_REPO=" zast-ai/blog"
77GITHUB_BRANCH=" main"
88CURRENT_TAG=" $CI_COMMIT_TAG "
99
@@ -25,7 +25,9 @@ get_previous_tag() {
2525 done
2626
2727 # Get previous tag (if exists)
28- if [[ $current_index -gt 0 ]]; then
28+ # Previous tag is the next item in the reverse sorted array
29+ # Only the oldest tag (last in array) should not have a previous tag
30+ if [[ $current_index -lt $(( ${# tag_array[@]} - 1 )) ]]; then
2931 prev_tag=" ${tag_array[$((current_index + 1))]} "
3032 fi
3133
@@ -35,13 +37,13 @@ get_previous_tag() {
3537# Function to generate commit message from tag annotation
3638generate_commit_message () {
3739 local tag=" $1 "
38- local annotation=$( git tag -l --format=' %(contents)' " $tag " 2> /dev/null)
40+ # local annotation=$(git tag -l --format='%(contents)' "$tag" 2>/dev/null)
3941
40- if [[ -n " $annotation " ]]; then
41- echo " $annotation "
42- else
42+ # if [[ -n "$annotation" ]]; then
43+ # echo "$annotation"
44+ # else
4345 echo " Release $tag "
44- fi
46+ # fi
4547}
4648
4749# Function to generate changelog
@@ -59,59 +61,192 @@ generate_changelog() {
5961# Main release process
6062echo " π Starting release process for tag: $CURRENT_TAG "
6163
64+ # Validate current tag exists
65+ if ! git rev-parse " $CURRENT_TAG " > /dev/null 2>&1 ; then
66+ echo " β Error: Tag '$CURRENT_TAG ' does not exist"
67+ exit 1
68+ fi
69+
6270# Get previous tag
6371PREVIOUS_TAG=$( get_previous_tag)
6472echo " Previous tag: ${PREVIOUS_TAG:- None} "
6573
74+ # Check if previous tag and current tag are the same
75+ if [[ -n " $PREVIOUS_TAG " && " $PREVIOUS_TAG " == " $CURRENT_TAG " ]]; then
76+ echo " β οΈ Previous tag and current tag are the same. Nothing to release."
77+ exit 0
78+ fi
79+
80+ # Validate required environment variables for commit author
81+ if [[ -z " $GIT_AUTHOR_NAME " || -z " $GIT_AUTHOR_EMAIL " ]]; then
82+ echo " β Error: GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL must be set"
83+ echo " Current values:"
84+ echo " GIT_AUTHOR_NAME: ${GIT_AUTHOR_NAME:- <not set>} "
85+ echo " GIT_AUTHOR_EMAIL: ${GIT_AUTHOR_EMAIL:- <not set>} "
86+ exit 1
87+ fi
88+
6689# Generate commit message from tag annotation
6790COMMIT_MESSAGE=$( generate_commit_message " $CURRENT_TAG " )
6891
6992# Generate changelog
7093CHANGELOG=$( generate_changelog " $PREVIOUS_TAG " " $CURRENT_TAG " )
7194
7295# Create full commit message
73- FULL_COMMIT_MESSAGE=" $COMMIT_MESSAGE
96+ FULL_COMMIT_MESSAGE=" $COMMIT_MESSAGE "
97+
98+ # Ensure we have all tags and branches
99+ echo " Fetching all tags and references..."
100+ git fetch --all --tags || echo " Warning: Could not fetch from remote (may be running locally)"
74101
75- Changes since ${PREVIOUS_TAG:- initial commit} :
76- $CHANGELOG "
102+ # Switch to main branch (even if it might be outdated)
103+ echo " Switching to main branch..."
104+ git checkout main
77105
78- # Create temporary branch for the release
79- RELEASE_BRANCH=" release-$CURRENT_TAG "
80- git checkout -b " $RELEASE_BRANCH "
106+ # Create a temporary branch based on current tag commit
107+ TEMP_BRANCH=" temp-merge-$( date +%s) "
108+ echo " Creating temporary branch based on $CURRENT_TAG : $TEMP_BRANCH "
109+ git checkout -b " $TEMP_BRANCH " " $CURRENT_TAG "
81110
82- # Reset to create a clean commit
111+ # Apply changes from previous tag to current tag
83112if [[ -n " $PREVIOUS_TAG " ]]; then
113+ echo " Applying changes from $PREVIOUS_TAG to $CURRENT_TAG ..."
114+
115+ # Reset to previous tag state to get a clean base
84116 git reset --hard " $PREVIOUS_TAG "
117+
118+ # Get all files that changed between previous tag and current tag
119+ echo " Extracting file changes between tags..."
120+ git checkout " $CURRENT_TAG " -- .
121+
85122else
86- # First release - start from empty commit
87- git rm -rf . || true
88- git clean -fdx
89- echo " Initial release" > README.md
90- git add README.md
91- git commit -m " Initial commit" || true
123+ # First release - use all files from current tag
124+ echo " First release - applying all files from $CURRENT_TAG "
125+ git reset --hard " $CURRENT_TAG "
92126fi
93127
94- # Merge all changes since previous tag (or all changes for first release)
95- if [[ -n " $PREVIOUS_TAG " ]]; then
96- git merge --squash " $CURRENT_TAG "
97- else
98- git merge --squash " $CURRENT_TAG "
128+ # Stage all changes
129+ git add .
130+
131+ # Check if there are any changes to commit
132+ if git diff --cached --quiet; then
133+ echo " No changes to commit between ${PREVIOUS_TAG:- initial} and $CURRENT_TAG "
134+ exit 0
99135fi
100136
101137# Commit with unified author information
102- git add .
138+ echo " Creating release commit with unified author "
103139git commit -m " $FULL_COMMIT_MESSAGE " --author=" $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL >"
104140
105- # Add GitHub remote
106- git remote add github " https://$GITHUB_USERNAME :$GITHUB_TOKEN @github.com/$GITHUB_REPO .git"
141+ # Cherry-pick the new commit to main branch
142+ RELEASE_COMMIT=$( git rev-parse HEAD)
143+ echo " Release commit created: $RELEASE_COMMIT "
144+
145+ git checkout main
146+ echo " Cherry-picking release commit to main branch..."
147+ git cherry-pick " $RELEASE_COMMIT " || git commit -m " $FULL_COMMIT_MESSAGE " --author=" $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL >"
148+
149+ # Clean up temporary branch
150+ git branch -D " $TEMP_BRANCH "
151+
152+ # Step 1: Push to GitHub first (if GITHUB_TOKEN is available)
153+ GITHUB_PUSH_SUCCESS=false
154+
155+ if [[ -n " $GITHUB_TOKEN " ]]; then
156+ echo " π€ Step 1: Pushing main branch to GitHub $GITHUB_BRANCH branch..."
107157
108- # Force push to GitHub main branch
109- echo " π€ Pushing to GitHub $GITHUB_BRANCH branch..."
110- git push --force github " $RELEASE_BRANCH :$GITHUB_BRANCH "
158+ # Debug GitHub configuration
159+ echo " π GitHub Configuration Debug:"
160+ echo " GITHUB_REPO: $GITHUB_REPO "
161+ echo " GITHUB_BRANCH: $GITHUB_BRANCH "
162+ echo " GITHUB_USERNAME: ${GITHUB_USERNAME:- <not set>} "
163+ echo " GITHUB_TOKEN: ${GITHUB_TOKEN: 0: 8} ... (${# GITHUB_TOKEN} chars total)"
111164
112- # Clean up
113- git remote remove github
165+ # Validate required variables
166+ if [[ -z " $GITHUB_USERNAME " ]]; then
167+ echo " β Error: GITHUB_USERNAME is not set"
168+ echo " π‘ Add GITHUB_USERNAME to GitLab CI/CD variables"
169+ exit 1
170+ fi
171+
172+ if [[ ${# GITHUB_TOKEN} -lt 10 ]]; then
173+ echo " β Error: GITHUB_TOKEN appears to be invalid (too short)"
174+ echo " π‘ Check GITHUB_TOKEN in GitLab CI/CD variables"
175+ exit 1
176+ fi
177+
178+ # Add GitHub remote
179+ GITHUB_URL=" https://$GITHUB_USERNAME :$GITHUB_TOKEN @github.com/$GITHUB_REPO .git"
180+ echo " π Adding GitHub remote: https://$GITHUB_USERNAME :***@github.com/$GITHUB_REPO .git"
181+ git remote add github " $GITHUB_URL " 2> /dev/null || true
182+
183+ # Test GitHub connectivity
184+ echo " π§ͺ Testing GitHub connectivity..."
185+ if git ls-remote github > /dev/null 2>&1 ; then
186+ echo " β
GitHub remote access verified"
187+ else
188+ echo " β Failed to access GitHub remote"
189+ echo " π‘ Possible issues:"
190+ echo " 1. GITHUB_TOKEN has expired or lacks repository access"
191+ echo " 2. Repository $GITHUB_REPO doesn't exist or is private"
192+ echo " 3. GITHUB_USERNAME is incorrect"
193+ exit 1
194+ fi
195+
196+ if git push --force github " main:$GITHUB_BRANCH " ; then
197+ echo " β
Successfully pushed to GitHub"
198+ GITHUB_PUSH_SUCCESS=true
199+ else
200+ echo " β Failed to push to GitHub"
201+ echo " π Troubleshooting steps:"
202+ echo " 1. Verify GITHUB_TOKEN has 'repo' or 'public_repo' scope"
203+ echo " 2. Check if token belongs to user with write access to $GITHUB_REPO "
204+ echo " 3. Ensure repository exists: https://github.com/$GITHUB_REPO "
205+ echo " Aborting GitLab sync due to GitHub failure"
206+ exit 1
207+ fi
208+ else
209+ echo " β οΈ GITHUB_TOKEN is not set. Skipping GitHub sync."
210+ echo " β οΈ GitLab sync will also be skipped as it depends on GitHub success."
211+ echo " β
Release completed locally! Release commit added to main branch."
212+ echo " π Commit message:"
213+ echo " $FULL_COMMIT_MESSAGE "
214+ exit 0
215+ fi
216+
217+ # Step 2: Push to GitLab origin (only if GitHub was successful)
218+ if [[ " $GITHUB_PUSH_SUCCESS " == " true" ]]; then
219+ echo " π€ Step 2: Pushing main branch to GitLab origin..."
220+
221+ # Try using CI_JOB_TOKEN if available (GitLab CI environment)
222+ if [[ -n " $CI_JOB_TOKEN " ]]; then
223+ echo " Using CI_JOB_TOKEN for GitLab authentication"
224+ # Set up authentication using CI_JOB_TOKEN
225+ git remote set-url origin " https://gitlab-ci-token:${CI_JOB_TOKEN} @gitlab.tailf2fef.ts.net/eng/zast-blog.git" 2> /dev/null || true
226+ fi
227+
228+ if git push origin main --force; then
229+ echo " β
Successfully pushed to GitLab origin"
230+ else
231+ echo " β οΈ Failed to push to GitLab origin"
232+ echo " This might be due to insufficient permissions."
233+ echo " "
234+ echo " π‘ To fix GitLab push permissions:"
235+ echo " 1. Ensure CI/CD job token has write access in project settings"
236+ echo " 2. Or add a deploy key with write permissions"
237+ echo " 3. Or use a personal access token with write_repository scope"
238+ echo " "
239+ echo " β
GitHub sync was successful, manual GitLab sync may be needed"
240+ fi
241+ else
242+ echo " β Skipping GitLab sync due to GitHub failure"
243+ exit 1
244+ fi
114245
115246echo " β
Release completed successfully!"
247+ echo " π― Summary:"
248+ echo " - Release commit created on main branch"
249+ echo " - β
Synced to GitHub repository (Step 1)"
250+ echo " - β
Pushed to GitLab origin (Step 2)"
116251echo " π Commit message:"
117252echo " $FULL_COMMIT_MESSAGE "
0 commit comments