Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change jenkins pipeline to use github instead of gitlab

Tags:

github

jenkins

I have a set of automated tests that are ran in a jenkins pipeline, testcode is located in gitlab. The section where I pull code from gitlab looks like this:

enter image description here

I use gitlab credentials that were already present there (since other project use the same gitlab credentials).

I use a simple jenkinsfile that is located in the test codebase to run the script from here. This is roughly how it looks:

  agent {
    kubernetes {
      defaultContainer 'jnlp'
      yaml """
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            application: auto_ish
        spec:
            containers:
              - name: node
                image: node:12.14.1
                command:
                - cat
                tty: true
      """
    }
  }
  stages {
    stage('Build') {
      steps {
        container('node') {
            sh '''
                npm install
            '''
        }
      }
    }

    stage('Test') {
      steps {
        container('node') {
          sh 'node_modules/.bin/wdio ./test/config/wdio.conf.acc.js --suite SmokeTest --mochaOpts.grep=@smoke' 
         }
      }
    }

My problem:

The codebase of my automated tests has recently been moved to github, and I have trouble getting it to work in jenkins. For github I have a personal access token that I need to use, so no private key like I had for gitlab. I have tried adding this token to the credentials manager, but after adding it doesnt show up in the dropdown.

I followed some walkthroughs that told me to install github plugins for jenkins and then set my personal access token in jenkins configuration like this:

enter image description here

I tested the connection,and it worked.

From here on, I have no idea how to proceed. I just want to pull the code from the codebase to run the project. No need to trigger builds when code is pushed to github, since my tests are triggered when other jobs finish.

But since my credentials are not in the credentialsmanager, I cannot just add the new repo here. This also means I cannot refer to my jenkinsfile here.

I read somewhere I had to refer to my github project here: enter image description here I did this, butI think this will not be enough. I figure I need to pull the code from the new repo somewhere, but I have no idea where.

Which brings me to my question: where and how do I pull the code from my github repo, using the personal acces token/github server I specified?.

like image 537
Martijn Avatar asked Apr 01 '20 13:04

Martijn


2 Answers

You can configure Jenkins instance with Github with the help of SSH key

You Just have to create SSH public and private keys and past public key in

Github > Settings > SSH/GPC key > Add public key Make sure you will not add any space and new line

Save and exit from Github

Now go to Jenkins * Start to configure your project, or Go to credentials > System > Global credentials > Add credentials a page will open

  • In Kind drop-down select SSH Username with private key
  • Check private key radio button and then press Add key button, a textarea will open paste your private key in that textarea, Make sure you copy the private key and while pasting not adding any space in it. Make sure you select the whole key, Begin and End text of key also.

enter image description here

enter image description here

  • Now Save and while Configuring Project Source Code Management tab, you will find credentials and a drop-down, select the new configured key from that dropdown jenkinsSSH.

  • Make sure your clone your Github repo using SSH not HTTPS

enter image description here

and build the application. this will work

for more reference watch this video tutorial https://www.youtube.com/watch?v=mGXGIOpKAos&list=PLhW3qG5bs-L_ZCOA4zNPSoGbnVQ-rp_dG&index=9

[Update]

To clone git repository using Personal Access Token, you can use following format

https://user:[email protected]/org/repo.git like

git clone https://user:[email protected]/org/repo.git

there is one more question same as this, and he provided one solution, might help you

Git Clone in Jenkins with Personal Access Token idles forever

please have a look

like image 168
Dupinder Singh Avatar answered Oct 17 '22 21:10

Dupinder Singh


After some intense googling I found the answer, which proved to be a lot easier then I thought:

Apparently a personal access token can be used like a password, as far as jenkins is concerned atleast. I added new credentials to the credential manager, chose type 'username and password', put in a non existing username ('user') and put the personal access token in the password field.

This way I could choose the credentials from the dropdown like I did before, and the project was cloned without issues

like image 30
Martijn Avatar answered Oct 17 '22 19:10

Martijn