List all branches of a git repository dynamically in Jenkins pipeline (new automation system – part 14)

    1. Preface
      Hi all,
      I now got into a situation where I need to have a pipeline in my Jenkins server that is capable to run the same “task” but with different branches of the same repository (we are working in branching-mode methodology, so it is desired most of the times actually).
      In this post I will describe the settings required to compose a Jenkins pipeline that “polls” (list) dynamically all branches of a particular repository, which then lets the user run the pipeline with a specific branch.

      1. Logistics:
        1. The Jenkins server is 2.204.2 (hosted on Ubuntu 18.04)
        2. The repository is hosted in a BitBucket.
    2. Preparations
      1. First thing I need to do is to provide Jenkins credentials to connect (and “fetch”) to the private repository in BitBucket. This can be done by creating an SSH key pair to “link” between the jenkins (!!) user on the machine that hosts the Jenkins server and the (private) BitBucket repository.
          1. First thing is to create an SSH key to the jenkins user (which is the user that runs the Jenkins server – it is most likely created by default upon the installation):
          2. guya@ubuntu_jenkins:~$ sudo su jenkins 
            [sudo] password for guya: 
            jenkins@ubuntu_jenkins:/home/guya$ ssh-keygen 
            Generating public/private rsa key pair.
            Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
            Created directory '/var/lib/jenkins/.ssh'.
            Enter passphrase (empty for no passphrase): 
            Enter same passphrase again: 
            Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
            Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
            The key fingerprint is:
            SHA256:q6PfEthg+74QFwO+esLbOtKbwLG1dhtMLfxIVSN8fQY jenkins@ubuntu_jenkins
            The key's randomart image is:
            +---[RSA 2048]----+
            |    . .. o.E.    |
            |   . . .o... o   |
            |    . o..   o    |
            |    +.oo         |
            | . ooX..S        |
            |..+.Bo*  .       |
            |.++oo* o.        |
            |..+*..*o         |
            | .=+o==+.        |
            +----[SHA256]-----+
            jenkins@ubuntu_jenkins:/home/guya$
            
          3. Now the content of this SSH keys needs to be set in the BitBucket repository as follows:
            1. Create (add) an SSH key in the BitBucket repository by going to: Settings –> Access keys –> Add key.
              1. Give the key Read permissions and copy the content of the PUBLIC key to the “body” of the key. The content of the key can be displayed by running: cat /var/lib/jenkins/.ssh/id_rsa.pub
          4. After that the SSH key was set in the BitBucket repository, we need to “tell” Jenkins to actually USE it when it tries to fetch (read in this case) the content of the repository.NOTE that by letting Jenkins know, actually means let user jenkins this “privilege”. This can be done by adding a new  SSH User name with private key to the Jenkins –> Credentials –> System –> Global Credentials –> Add credentials.
            1. In the ID section put any descriptive name to the key.
            2. In the Username section put the user name of the Jenkins server which is jenkins.
            3. Tick the Private key section and paste the content of the PRIVATE key that was generated earlier by copy-paste the content of: ~/.ssh/id_rsa. This is the private key which start with the string:”—–BEGIN RSA PRIVATE KEY—–” and ends with the string (you guessed right): “—–END RSA PRIVATE KEY—–“. Note that this entire “block” should be copied-paste into the above section.
            4. Once you done you should have the following:
      2. Install the Git Parameter plugin that can be found in its official page here
    3. Implementing the pipeline

        1. The very minimum pipeline that is required to list (dynamically) all the branches of a given repository is as follows:
          pipeline
          {
              agent any
              parameters 
              {
                  gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
              }
          
              stages
              {
                  stage("list all branches")
                  {
                      steps
                      {
                          git branch: "${params.BRANCH}", credentialsId: "SSH_user_name_with_private_key", url: "ssh://git@myCompanyBitBucketSite.com:port/myRepository.git"
                      }
                  }
              }
          }
          
        2. NOTES:
          – The defaultValue is set to master so that if no branches exist – it will be displayed in the “drop list” of the pipeline.
          – The credentialsId has the name of the credentials configured earlier.
          – In this case I used the SSH URL of the repository in the url parameter.
    4.  Testing the pipeline
      1. After I have created the mentioned pipeline I will now add a new branch to my repository named myFeatureBranch2.
        $ git checkout -b myFeatureBranch2
        $ git push origin myFeatureBranch2
      2. Now when running the pipeline by using the Build with Parameters option the following is displayed:
        list_branches_2Resources:
        a) Official Git Parameter plugin page
        b) Excellent tutorial about adding SSH key to “link” BitBucket and Jenkins

        The picture:
        Nazca lines, near the city Nazca, Peru.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s