Skip to content

Instantly share code, notes, and snippets.

@Plasmoxy
Last active December 16, 2023 14:44
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Plasmoxy/0069f6eac31e44209691bc79a5c725ae to your computer and use it in GitHub Desktop.
Save Plasmoxy/0069f6eac31e44209691bc79a5c725ae to your computer and use it in GitHub Desktop.
How to setup Continous Integration for GitLab with Jenkins (works for private projects)

How to setup Continous Integration for GitLab with Jenkins

Hi, I'd like to share with you how I got my Jenkins CI set up for my private GitLab projects. This is more like a sheet of notes of mine, but I hope it'll help someone as I haven't found any good tutorial for Jenkins+Gitlab on the web yet :D

Here we go:

  • generate ssh keys on own server -> PUBLIC and PRIVATE ( no passphrase )
  • go to GitLab profile settings -> SSH keys -> add PUBLIC ssh key to GitLab = this will allow your Jenkins server to access your GitLab
  • also create full access GitLab API access token for Jenkins on your gitlab profile settings and keep it

  • install Git + GitLab plugin on Jenkins ( make sure they are enabled (ticked) in Jenkins !!!)
  • setup git username and mail in manage Jenkins
  • restart jenkins

  • go to manage Jenkins -> Gitlab section
  • create new gitlab connection
    • set connection name
    • set gitlab server (https://gitlab.com)
    • add gitlab API credentials (create them with the API access token from beginning)

  • create/choose GitLab project
  • create Jenkins job:
    • GitLab connection = should be automatically selected to the connection you've just created
    • Source Code Management -> select Git
      • Repository URL = copy the SSH url from GitLab project page ( not the https !)
      • Credentials = now we need to give the SSH PRIVATE key to Jenkins -> Add new Credentials
        • Kind = SSH Username with private key
        • Username = your username for git
        • Private Key -> tick "Enter directly" and enter your PRIVATE key i.e.:
        -----BEGIN OPENSSH PRIVATE KEY-----
        asfkapokfrpa2kL2K3K32323
        -----END OPENSSH PRIVATE KEY-----
        
        • set description like Gitlab ssh key or something
        • click Add and make sure the SSH Credentials are selected
    • Build Triggers -> Build when a change is pushed to GitLab. :
      • this will also show you the webhook URL, keep it we'll need it later
      • probably retarded note ignore this if yo got no errors or running Jenkins on 8080 : notice that if you have wrong Jenkins address set up in Jenkins, it will use the set up Jenkins address, not the real URL one so make sure the domain is right in the webhook url
      • IMPORTANT : expand the setting by clicking on Advanced and generate a Secret token = keep this Secret token, we'll use it for gitlab webhook
    • Build = add your build steps ( for example I use Invoke Gradle Script)
    • Add post-build action -> Publish build status to GitLab ( this will complete the Continous Integration cycle and show build status on commits on GitLab)
    • Save the job

  • finally we set the gitlab webhook up - this will allow gitlab to notify Jenkins of new commits, pull requests etc.

  • go to your gitlab project -> Settings -> Integrations :

    • enter the webhook URL that you got from Jenkins job setup
    • enter the Secret token that you generated on Build Triggers section in job setup
    • tick Merge request events ( and any other stuff you want to trigger Jenkins with)
    • Leave SSL verification enabled, if you don't have SSL or something and have problems then untick it
    • Add webhook
    • Now you should have a webhook set up
    • Test it by clicking on Test -> Push requests
  • try to push to your gitlab project and see the CI ;)

  • it may look really complicated at first but it's very logical and Jenkins is really useful for both CI and Continuous Delivery as you can customize it as you want

  • Bonus note for myself: if new Jenkins can't keep a single gradle daemon alive, execute gradle like this -> execute shell command task -> BUILD_ID=dontKillMe gradle build, which won't kill gradle daemon. ( or set BUILD_ID=dontKillMe as environment variable in configure jenkins )

  • Also branch filtering (separate Jenkins project for dev and master branch ) can be done within the GitLab plugin for Jenkins !

@AsherShoshan
Copy link

"go to GitLab profile settings -> SSH keys -> add PUBLIC ssh key to GitLab = this will allow your Jenkins server to access your GitLab
"
Which user are you referring in "profile setting"? Jenkins? how to do that?

@SBarkovskiy
Copy link

SBarkovskiy commented Jun 29, 2022

"go to GitLab profile settings -> SSH keys -> add PUBLIC ssh key to GitLab = this will allow your Jenkins server to access your GitLab " Which user are you referring in "profile setting"? Jenkins? how to do that?

Hey!
Any user can be used within the key.
The main idea is to create public and private keys and use them (does not matter which user is inside of them)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment