0

I have a jenkins pipeline project which does the checkout from my github repository as shown below Jenkins pipeline SCM

Any changes updated in my github repository should be reflected in github repository of another account with the same folder strcuture using Jenkins pipeline. Got any ideas on how to proceed with this?

I have attached my pipeline script and the console output in the following images Pipeline Script Console Output

james
  • 19
  • 4

1 Answers1

1

Assuming your other GitHub repository:

  • should mirror the history of your first GH repository,
  • has your first account added as a collaborator

You could make your pipeline do a git push "withCredentials", as in here.

withCredentials([usernamePassword(credentialsId: 'fixed',
                 usernameVariable: 'username',
                 passwordVariable: 'password')]){
    sh("git push http://$username:$password@git.corp.mycompany.com/repo")
}

See "Credentials Binding Plugin": you can use variable or dsecret file for the password.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the informataion ,what should be exactly given in usernameVariable and passwordVariable,is it just username,or username of our github account (I have already added the credentials in my jenkins)? Also @git.corp.mycompany.com , I didnt understand significance of that. – james Jun 06 '21 at 20:20
  • 1
    @jamesprakash username: your GitHub account username. password: a PAT (personal access token). And `@git.corp.mycompany.com` can reference a private GitHub enterprise on-premise server. If you are working with github.com, replace it with `@github.com` – VonC Jun 06 '21 at 20:35
  • Normally a github repository is accessed in this fromat : https://github.com/username/repo.git Hences : I gave sh("git push https://$username:$PAT@github.com/username/repo HEAD:main") and it says this : + git push https://****:****@github.com/****/repo HEAD:main remote: Repository not found. fatal: repository 'https://github.com/****/repo/' not found . Username is getting masked after github.com which is actually required – james Jun 07 '21 at 09:19
  • @jamesprakash Did you replace /repo by the actual name of your repository? – VonC Jun 07 '21 at 09:24
  • Yes ,i did,i have added the image of my script in the question raised – james Jun 07 '21 at 09:26
  • 1
    @jamesprakash You seem to have a `$` in front of your PAT (token). A `$xxx` means for a bash session: expand xxx variable and replace t by its value. Since, in your case, you are using the token directly, don't add a `$` in front of it. – VonC Jun 07 '21 at 09:51
  • Tried all kinds of combinations including that .It still says the same result .I have added the image of the console output . 'https://github.com/****/repo/' is shown in console output , the masked one /****/ when replaced by github username gives the correct link. – james Jun 07 '21 at 10:59
  • @jamesprakash Any chance, as suggested in the answer, to use the credentials of the *same* user account as the Jenkins first checked-out repository (to push to the second one)? That way, no need to pass as variable/secret a second account. – VonC Jun 07 '21 at 12:21
  • It's different accounts, jenkins is checking out from my GitHub account and it is pushing to another another GitHub account which is given in credentials inside my jenkins pipeline – james Jun 07 '21 at 15:52