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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Jenkins Pipeline Groovy Script - Part 1: Creating Gitlab Group
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Mastering Shift-Left: The Ultimate Guide to Input Validation in Jenkins Pipelines
  • Jenkins Pipelines With Centralized Error Codes and Fail-Fast

Trending

  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • How to Merge HTML Documents in Java
  • Building an AI/ML Data Lake With Apache Iceberg
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Jenkins Pipeline Groovy script - Part 2 Add a User to a Gitlab Group

Jenkins Pipeline Groovy script - Part 2 Add a User to a Gitlab Group

Discussing about how to add list of users in newly created Gitlab group or existing group with specific permission granted to them

By 
Kalaiyarasi Durairaj user avatar
Kalaiyarasi Durairaj
·
Aug. 03, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

My earlier post on Jenkins Pipeline Groovy script - Part 1 , discusses about how to create Gitlab group using Jenkins Pipeline Groovy script. In this post, I am discussing about how to add list of Users to newly created Gitlab group or existing group with specific permission granted to them. In companies while handling big projects and maintaining enormous code repositories, it is very much important to know what permissions supposed to be given to each member in the team. 

Because giving wrong permission to team member, opens big path for problems if the team member really unaware about how efficiently the permission can be used for good purpose. Hence giving right permissions to the team member is very serious business for quick and reliable delivery. And yes, this also avoids team member intervening unnecessarily to the projects where they do not actually belong.

1. Gitlab User Restful API

Gitlab exposes it's functionality via Restful APIs which can be consumed via curl script or any programming language like Groovy and Python., etc. To add user to Gitlab group, Gitlab Project members Restful APIs are consumed in this tutorial. 

Adding group members to GitHub

So who are Users in Gitlab Project, Users are team members who work on code level changes as developer or owner to the specific Gitlab project repositories or can even play any role(mentioned section 1.1) depending on the project requirement. 

1.1 Gitlab User Permission Level

Based on the responsibility and the role of the team members assigned in the project level, Gitlab offers following member permissions level.

  • Guest (10)
  • Reporter (20)
  • Developer (30)
  • Maintainer (40)
  • Owner (50)

Though the permission level varies for different role, all the users have permission to clone or download the project code, leave the comments and create the issues in Gitlab Project where they belong. suppose, if any of the team member leaves the project,  merge request and created issues will be unassigned automatically.

2. Pipeline Groovy script to add user to Gitlab group

So how to add users to Gitlab group, my earlier post on Jenkins Pipeline Groovy script - Part 1, discusses on how to create Gitlab group using Pipeline Groovy script. Here I am explaining about how to add team members to a newly created Gitlab group or existing group. Let's create groovy class with name addUserToGitlabGroup.groovy

What is this groovy class does:

  • Checks whether the user has access to Gitlab URL instance or not
  • Only if the user has access, then adds the user to mentioned Gitlab group with given access level
Groovy
xxxxxxxxxx
1
33
 
1
import groovy.transform.Field
2
@Field gitlabUrl
3
@Field gitlabToken
4
               
5
@NonCPS
6
def jsonParseData(jsonObj) {
7
    def slurObj=new groovy.json.JsonSlurper().parseText(jsonObj)
8
    return slurObj
9
}             
10
def addUserToGroup(gitlabUrl, gitlabToken, subgrpId, userIds, userAccessLevel)  {
11
  addUserDetails(userIds, userAccessLevel, subgrpId)
12
}
13
// Add team member to Gitlab group
14
def addUserDetails(userIds, userAccessLevel, subgrpId){
15
 stage("Add Users in Gitlab"){
16
   script{
17
     
18
     for(usrcnt=0; usrcnt<userIds.size(); usrcnt++)
19
     {
20
      def usrGitlabId=sh script: """curl --request GET --header "PRIVATE-TOKEN: ${gitlabToken}" ${gitlabUrl}users?username=${userIds[usrcnt].split("@")[0]}""", returnStdout: true
21
       
22
      if (jsonParseData(usrGitlabId).id.size().equals(1)){
23
             sh """curl -X POST -H "PRIVATE-TOKEN: ${gitlabToken}" -H "Content-Type: application/json" \
24
       ${gitlabUrl}groups/${subgrpId}/members \
25
       -d '{"id":${subgrpId}, 
26
             "user_id":${jsonParseData(usrGitlabId).id[0]},
27
             "access_level": ${userAccessLevel[usrcnt]}
28
            }'"""
29
      }      
30
    }
31
   }
32
 }
33
}


The variable subgrpId is Gitlab group or subgroup Id under which the list of users(userIds) will be added with specified access level defined in the variable userAccessLevel . In Jenkinsfile,  the Groovy function adduserToGroup() is invoked using object instance of addUserToGitlabGroup.groovy by supplying input arguments such as gitlab token,  gitlab URL , group(Id),  list of users and access level.

Groovy
xxxxxxxxxx
1
10
 
1
pipeline{
2
    node('slave') {
3
        def userIds = ["joel@dp.abc.com", "joshu@dp.abc.com", "nithu@kr.abc.com"]
4
        def userAccessLevel = [30, 50, 20]
5
        def subgrpId
6
7
        gitlabObj = load 'addUserToGitlabGroup.groovy'
8
        gitlabObj.addUserToGroup(gitlabURL, gitlabToken, subgrpId, userIds, userAccessLevel)
9
    }
10
}


2.1 Limitation

The limitation of adding user automatically to Gitlab group using Jenkins Pipeline Groovy script by consuming Gitlab Member Restful APIs is,  that the team-member should have access to Gitlab URL instance. Team member who is not having access to the Gitlab instance can not be added to the Gitlab group.

Summary

Gitlab is an open-source distributed version control system, widely used in DevOps Continuous Integration(CI) and Continuous Delivery(CD) culture to maintain the project codes in well organized structure with version control mechanism. In my earlier post on  Jenkins Pipeline Groovy script - Part 1, I have discussed about how to automatically create Gitlab groups. In this post I have explained about how to add users to Gitlab group automatically using Jenkins Pipeline groovy script. I hope this post is useful to a scenario, where big projects, on boarding multiple team members to multiple Gitlab groups with specific permission granted to them. Adding users to Gitlab group manually is a time consuming process and it may lead to manual error either. But using above Pipeline Groovy script this can be simplified by passing user information and permission as an array argument to the Pipeline Groovy function.

GitLab Groovy (programming language) Pipeline (software) Jenkins (software)

Opinions expressed by DZone contributors are their own.

Related

  • Jenkins Pipeline Groovy Script - Part 1: Creating Gitlab Group
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Mastering Shift-Left: The Ultimate Guide to Input Validation in Jenkins Pipelines
  • Jenkins Pipelines With Centralized Error Codes and Fail-Fast

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!