DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Jenkins: Deploying Projects from Git with Submodules

Jenkins: Deploying Projects from Git with Submodules

Here are two possible solutions to a rather counter-intutitive credentials problem.

Duncan Brown user avatar by
Duncan Brown
·
May. 03, 19 · Tutorial
Like (2)
Save
Tweet
Share
35.92K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

To some, the word "submodules" strikes fear into the very heart of those developers who dare speak or hear it.

Ok, maybe it isn't that bad: Submodules can be a great way to tie together and deploy your services, especially in a microservices environment.

There are, of course, various quirks to overcome when dealing with them.  One such nuance I want to address in this post is deploying a project with submodules via Jenkins Pipelines (for those who missed it, be sure to check out my introduction to Jenkins Pipelines in my previous post).

The Issue

Whether you allow Blueocean to help manage your pipeline and Jenkinsfile or not, you still need to associate your Jenkins job with the necessary project in Git (the one referencing submodules, in this case).

You would think that adding in the repository's path along with the necessary credentials would be all that is necessary to connect to the project before moving on and configuring the rest of your Jenkins job.

However, sometimes, when you try to run the Jenkins job, you may notice that Jenkins throws an error and fails the build, saying that the job doesn't have sufficient permissions to pull the linked submodules from the main Git project.

Possible Solutions

As it turns out, in some implementations of Git, the credentials used to log in to the parent project doesn't automatically propagate the same credentials to the referenced submodules. It is often the case that we want to use the same credentials to log in to both the main Git project and its submodules.

One possible solution is as follows:

  1. Go to "Configure" the Jenkins job.

  2. Under "Branch Sources," in the "Git" section, under "Behaviors," click "Add," and then select "Advanced sub-modules behaviors."

  3. Enable "Use credentials from default remote of parent repository."

  4. Save your changes and try to run the job again.

Often, times this is enough to correct the issue.

Sometimes, however, it isn't.

Another potential solution if your Jenkins job is a Jenkins Pipeline project is to edit your Jenkins file and create/update a "Checkout" stage (you can see how to script this kind of behavior in my previous post).

There is a GitSCM class that is essentially the same one used by the GUI-driven solution above.

So, in your Jenkinsfile, try inserting this in the appropriate place, updating any placeholders as necessary:

checkout(
  [
    $class: 'GitSCM', 
    branches: [
      [
        name: '*/master'
      ]
    ], 
    doGenerateSubmoduleConfigurations: false, 
    extensions: [
      [
        $class: 'SubmoduleOption', 
        disableSubmodules: false, 
        parentCredentials: true, 
        recursiveSubmodules: true, 
        reference: '', 
        trackingSubmodules: false
      ]
    ], 
    submoduleCfg: [], 
    userRemoteConfigs: [
      [
        credentialsId: '<Jenkins Credential Id for this reop>', 
        url: 'git@gitlab.com:path/to/project.git'
      ]
    ]
  ]
)


The important parts to note:

  • parentCredentials is what we're primarily after here. Make sure this is set to true!
  • If your submodules also reference submodules, make sure that recursiveSubmodules  is also set to true.
  • Be sure to set the branch name to pull from whichever branch it is you wish to deploy from.
  • Under "userRemoteConfigs," this is where you'll reference the credentials used to connect to Git. If you're using Blueocean, this was likely taken care of for you already and should appear in the Jenkinsfile. If not, be sure to setup credentials in Jenkins (usually found in the left-hand navigation towards the bottom) and reference them here.

Re-run the Jenkins job with the updated Jenkinsfile and see if your permission woes have gone away.

Conclusion

Submodules can be a bit tricky to deal with when you're first making use of them, especially when you see counter-intuitive behavior like this. Hopefully one of the solutions in this post will help get you past this particular headache so you can move on with your deployment!

Jenkins (software) Git

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenVPN With Radius and Multi-Factor Authentication
  • Orchestration Pattern: Managing Distributed Transactions
  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • How To Build a Spring Boot GraalVM Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: