0

I am attempting to move a declarative pipeline from being written in a Jenkins Pipeline Configuration input box, to code hosted in BitBucket so that we have source control for any changes we make to this pipeline.

Due to the pipeline as code steps being written in a Jenkinsfile in a git repository, when I try to initialise a git repository to add my generated files to, I get an error:

+ git remote add origin git@bitbucket.org:X/Y.git
fatal: remote origin already exists.

Is there any way to handle this properly?

Edit:

            sh "git init"
            sh "git add ."
            sh "git commit -m \"Initial commit\""
            sh "git remote rm origin"
            sh "git remote add origin git@bitbucket.org:X/Y.git"
            sh "git remote -v"
            sh "git push origin master"
            sh "cat .git/config"
James
  • 749
  • 1
  • 9
  • 22
  • "declarative pipeline from being written in a Jenkins Pipeline Configuration input box,..." Can you elaborate a bit on this what is your goal and how does this exactly look like? – papanito Oct 24 '19 at 13:04
  • Previously the pipeline was written in the configuration on the jenkins website as a Pipeline Script. I am attempting to instead have it committed in a Bitbucket git repository so that we can have source control for the Jenkinsfile instead having it only live in a Jenkins input box. – James Oct 24 '19 at 13:06
  • Sorry but still don't understand what you mean with input box, where is that? Maybe also share the current code of your pipeline which executes the git commands – papanito Oct 24 '19 at 17:44
  • The current code is as above, it's simply on Jenkins inside the configuration of a multi-branch pipeline in the Pipeline Script input box. I'm moving it into source control so it is a Pipeline as Code. – James Oct 25 '19 at 08:03
  • It’s still ambiguous if your Jenkinsfile resides in a repo that is different from the repo you are trying to create or you are trying to create the repo for Jenkinsfile itself on command line. If the former and the pipeline script had been previously working when saved in the job configuration UI, but fails now after moving to Git, then try after selecting the **Lightweight Checkout** checkbox under **Pipeline script from SCM**. If the latter, then the question should be centered around Git or Bitbucket rather than Jenkins to avoid confusion. – Dibakar Aditya Oct 25 '19 at 18:39

2 Answers2

0

So the error states that theres already a remote with the same name. And sensibly you cant add that twice. A couple of options:

  1. update the remote url:
git remote set-url origin git@bitbucket.org:X/Y.git
  1. remove and re-add the origin
git remote rm origin
git remote add origin git@bitbucket.org:X/Y.git

EDIT

Seems like a duplicate of: Github "fatal: remote origin already exists"

  • Neither of these seem to work. The origin with that name definitely doesn't already exist, I will edit the OP to show the Jenkinsfile git contents. Error with 1: + git push origin master error: src refspec master does not match any. error: failed to push some refs to 'git@bitbucket.org:X/Y.git' Error with 2: + git push origin master error: src refspec master does not match any. error: failed to push some refs to 'git@bitbucket.org:X/Y.git' – James Oct 24 '19 at 13:03
0

It's unclear what you're trying to achieve with the init of the git repo. Each time you run your job, the repo would be initialized and pushed. This is hard to do more than once.

Jenkins knows how to run Jenkinsfile that is stored on the repo. You need to manually create your repo, commit the Jenkinsfile there, and then add a new job in Jenkins telling it that the Jenkinsfile should be brought over from the repo. This is called "Pipeline script from SCM". You then should configure the credentials, the path, the branch etc. in Jenkins. Each time you run the job, Jenkins would check out your Jenkinsfile from the repo, and then run it.

MaratC
  • 6,418
  • 2
  • 20
  • 27
  • It's parameterised, so each time it's run, it initialises and pushes a brand new repository. – James Oct 28 '19 at 08:49
  • How is this explained by your original question, e.g. "I am attempting to move a declarative pipeline from being written in a Jenkins Pipeline Configuration input box, to code hosted in BitBucket so that we have source control for any changes we make to this pipeline"? – MaratC Oct 28 '19 at 11:04
  • Also, your workspace contains the result of your previous run. See if you want to `rm -rf .git`, for example. – MaratC Oct 28 '19 at 11:08