@@ -31,7 +31,7 @@ const RELEASE_NOTES_BASE = 'https://red-hat-developers-documentation.pages.redha
3131// ── Helpers ──────────────────────────────────────────────────────────────────
3232
3333function git ( cwd , ...args ) {
34- const result = execFileSync ( 'git' , args , {
34+ const result = execFileSync ( 'git' , args , { // NOSONAR: git is resolved from PATH in a controlled CI environment
3535 cwd,
3636 stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
3737 timeout : 120_000 ,
@@ -71,7 +71,8 @@ function getPRState(owner, repo, prNumber) {
7171 }
7272 try {
7373 const json = JSON . parse ( data ) ;
74- resolve ( json . state === 'closed' ? ( json . merged ? 'merged' : 'closed' ) : 'open' ) ;
74+ const closedState = json . merged ? 'merged' : 'closed' ;
75+ resolve ( json . state === 'closed' ? closedState : 'open' ) ;
7576 } catch { resolve ( 'unknown' ) ; }
7677 } ) ;
7778 res . on ( 'error' , ( ) => resolve ( 'unknown' ) ) ;
@@ -209,6 +210,26 @@ async function stageAndCommit(deployDir, publishDir, branchDir, message) {
209210 return true ;
210211}
211212
213+ function tryRebaseAndPush ( deployDir , attempt ) {
214+ try {
215+ git ( deployDir , 'pull' , '--rebase' , 'origin' , 'gh-pages' ) ;
216+ } catch {
217+ console . log ( 'Rebase conflict — resetting to remote' ) ;
218+ try { git ( deployDir , 'rebase' , '--abort' ) ; } catch { }
219+ fetchOrCreateGhPages ( deployDir ) ;
220+ return false ;
221+ }
222+ try {
223+ git ( deployDir , 'push' , 'origin' , 'gh-pages' ) ;
224+ console . log ( `Deployed successfully (attempt ${ attempt } , after rebase)` ) ;
225+ return true ;
226+ } catch {
227+ console . log ( 'Push failed after rebase, will rebuild' ) ;
228+ fetchOrCreateGhPages ( deployDir ) ;
229+ return false ;
230+ }
231+ }
232+
212233async function pushWithRetry ( deployDir , publishDir , branchDir , message ) {
213234 for ( let attempt = 1 ; attempt <= MAX_RETRIES ; attempt ++ ) {
214235 if ( attempt > 1 ) {
@@ -224,25 +245,7 @@ async function pushWithRetry(deployDir, publishDir, branchDir, message) {
224245 return ;
225246 } catch {
226247 console . log ( `Push rejected (attempt ${ attempt } /${ MAX_RETRIES } )` ) ;
227- if ( attempt < MAX_RETRIES ) {
228- try {
229- git ( deployDir , 'pull' , '--rebase' , 'origin' , 'gh-pages' ) ;
230- // Rebase succeeded — push immediately without rebuilding
231- try {
232- git ( deployDir , 'push' , 'origin' , 'gh-pages' ) ;
233- console . log ( `Deployed successfully (attempt ${ attempt } , after rebase)` ) ;
234- return ;
235- } catch {
236- console . log ( 'Push failed after rebase, will rebuild' ) ;
237- // Reset and rebuild on next iteration
238- fetchOrCreateGhPages ( deployDir ) ;
239- }
240- } catch {
241- console . log ( 'Rebase conflict — resetting to remote' ) ;
242- try { git ( deployDir , 'rebase' , '--abort' ) ; } catch { }
243- fetchOrCreateGhPages ( deployDir ) ;
244- }
245- }
248+ if ( attempt < MAX_RETRIES && tryRebaseAndPush ( deployDir , attempt ) ) return ;
246249 }
247250 }
248251 throw new Error ( `Deploy failed after ${ MAX_RETRIES } attempts` ) ;
@@ -319,8 +322,10 @@ async function deploy() {
319322 git ( deployDir , 'init' , '-q' ) ;
320323 git ( deployDir , 'config' , 'user.name' , 'github-actions[bot]' ) ;
321324 git ( deployDir , 'config' , 'user.email' , 'github-actions[bot]@users.noreply.github.com' ) ;
322- const remoteUrl = `https://x-access-token:${ process . env . GITHUB_TOKEN } @github.com/${ process . env . GITHUB_REPOSITORY } .git` ;
323- git ( deployDir , 'remote' , 'add' , 'origin' , remoteUrl ) ;
325+ const repoUrl = `https://github.com/${ process . env . GITHUB_REPOSITORY } .git` ;
326+ git ( deployDir , 'remote' , 'add' , 'origin' , repoUrl ) ;
327+ const credentials = Buffer . from ( 'x-access-token:' + process . env . GITHUB_TOKEN ) . toString ( 'base64' ) ;
328+ git ( deployDir , 'config' , `http.${ repoUrl } .extraHeader` , `Authorization: Basic ${ credentials } ` ) ;
324329
325330 // Fetch gh-pages
326331 fetchOrCreateGhPages ( deployDir ) ;
@@ -332,7 +337,9 @@ async function deploy() {
332337 await pushWithRetry ( deployDir , publishDir , branchDir , message ) ;
333338}
334339
335- deploy ( ) . catch ( err => {
340+ try {
341+ await deploy ( ) ;
342+ } catch ( err ) {
336343 console . error ( err . message || err ) ;
337344 process . exit ( 1 ) ;
338- } ) ;
345+ }
0 commit comments