Gitlab integration with Jenkins - jenkins

I have a paid GitLab account where i store my software-project. I have installed Jenkins on my home PC. I have a Jenkins-project configured to build my software-project.
I don't know how to configure Jenkin's and my GitLab account so that Jenkins can
get the latest source of my software-project from my GitLab account.
Any help will be appreciated.
Note: I know what to do once the source code is downloaded from GitLab
Regards
Juby

You can use the Gitlab Jenkins plugin to configure Gitlab in your Jenkins Gitlab Jenkins plugin
otherwise you can create a jenkins Global Credential
user name : gitlab-ci-token
password : the token of your account you find it in the gitlab :
Profile Settings > Account > private token

Related

Jenkins freestyle project using Gitlab API Token never clone the repository, no error, build always succeed

I've followed this gitlab tutorial link, to connect my jenkins server to Gitlab.
Everyting went fine, and I've :
created a personnal access token in my GitLab profile
created a GitLab API Token using the my GitLab access token in jenkins system configuration as stated in the tutorial
create a freestyle jenkins job and Choose my GitLab connection from the dropdown
checked the Build when a change is pushed to GitLab checkbox.
checked the Accepted Merge Request Events and Closed Merge Request Events checkboxes
generated a secret token from the above freestyle project
use the freestyle jenkins project secret token to create a webhook in the GitLab project repository integration settings
Till there everything went fine.
Then I added and push code including a jenkinsFile to my GitLab repository, and get to the Jenkins WebUI to view the build status, but the pipeline shown green saying build success, while nothing happened, no code has been retrieved from GitLab (as shown in the attached console output screenshot), thus no jenkinsFile executed nor error message shown.
I tried to run the buils manually from WebUI but same result, no way to trigger my pipeline on git push events from GitLab
I thought may be I should select Git in Source Code Management section (I left it to None as the tutorial doesn't mention it) but if I choose Git as SCM I cannot select my GitLab API Token credentials, seeming like we cannot use GitLab plugin (API Token) and Git plugin for the same build project.
SO how should I proceed to be able build my jenkins project from GitLab with a jenkinsFile, using GitLab API Token?
Does the GitLab tutorial miss some useful steps?
OK, I think I understand the issue now.
There are two sets of credentials: GitLab API token for access to GitLab Webhooks and a separate one for cloning the git repo during builds.
So we can't use the GitLab API token for cloning the repository. For this you have to use either a SSH key or a Username/Password combination. Furthermore this dropdown is part of the git plugin not the gitlab plugin.
So the gitlab plugin can't tell which credentials are available as credentials for this dropdown.

Trigger Jenkins job on code commit to Azure repository

I have a Jenkins job running on my localhost:8080. I want to trigger this job automatically whenever there is a commit on Azure repos (Azure DevOps).
Any advise on how I can achieve this?
Thanks
Since Jenkins job running on your localhost:8080. You need to create your self-hosted agents on the local machine which your jenkin server can communicate to. Then you need to create a azure pipeline to be triggered on Azure repos commit and run this azure pipeline on your self-hosted agent. You can check out below workarounds:
Enable Trigger builds remotely on Jenkins
Go the the Build Triggers Tab of your jenkins pipeline configure page--> Then check Trigger builds remotely--> Specify a Token (will be used in the URL)
Define a secret variable to host your jenkins password(eg. password) in azure devops pipeline:
Add a bash task in your azure devops pipeline to run the below curl command
#token must be the same with the token you entered in above step
curl -u $(username):$(password) http://localhost:8080/job/myproject/build?token=anytoken
Targeting your self-hosted agent pool to run your azure devops pipeline on self-hosted agent.
There is another workaround using Jenkins queue job task.
Create a API Token in your Jenkin server.
Go your jenkin account configure page. To create a API token.
Add Jenkins queue job task in azure devops pipeline
Click the Manage link to create a jenkins service connection--> In the newly opened page-->Create Service connection-->Select Jenkins--> Next
Enter the required information. Note: url is your local jenkin server. username is your user account for jenkin server, the Password is the API Token You generated in above step.
Another workaround is to configure the Poll SCM build triggers on your jenkins job. So that the jenkin server will periodically poll the source code and queue the job if there is new commit.
See this thread for more information.

Jenkins SSH Agent Plugin shows empty drop down in Jenkins Job

I am using SSH Agent Plugin version 1.17 with Jenkins version 2.190.3
When I create credentials using
Jenkins -> Credentials -> System -> Global credentials (unrestricted) -> Add Credentials
But when I create jenkins job type Freestyle project, I don't see credentials in ssh agent drop down.
Please advise what I am missing
Ref:
https://wiki.jenkins.io/display/JENKINS/SSH+Agent+Plugin
https://support.cloudbees.com/hc/en-us/articles/360029470091-How-to-use-the-SSH-Agent-Plugin
Jenkins plugin ssh-agent showing "ERROR: Failed to run ssh-add"
Update:
Issue is open for credentials (username/password) https://issues.jenkins-ci.org/browse/JENKINS-59807
but good thing is that it works with private key :)
You have to select Kind as SSH Username with private key to be able to specific use credentials in ssh agent:

Gitlab OAuth configuration with Jenkins

I have configured Jenkins using Gitlab OAuth configuration for authentication purpose
Gitlab API token is configured in Jenkins
Role based authentication is given for Gitlab user
Web hook configuration is done for the project in Gitlab
Jenkins init method is given like in below format
var jenkins = jenkinsapi.init('http://Gitlabuser:password#IP');
when navigate to http://Gitlabuser:password#IP link it goes to Gitlab login page
The question is
why the link navigates to gitlab login page even though Gitlab
username and password provided in link
How to handle Jenkins Init method for Gitlab OAuth configuration
please let me know if any issues in the configuration also suggest a way to achieve gitlab OAuth configuration in Jenkins

GKE/Kubernetes CI/CD Pipelines With Jenkins: Gcloud Authentication Issue in Deploy stage

As part of a Jenkins pipeline to build and deploy an app to Google's Kubernetes service (GKE), I've created a script to carry out the following deployment to GKE:
checkout code
setup authentication to gcloud and
create the deployment and service using kubectl:
Detailed steps implemented by the script are as follows:
a) Create the docker registry authentication file (.json)
b) login to the google docker registry using the authentication file
c) initialise a git repo in the current directory
d) add the remote origin in prep for code pull
e) pull the source code for the microservice container
f) Create a kubectl configurtion file and directory to authenticate to the kubernetes cluster in Gcloud
g) Create a keyfile for a Gcloud service account that needs to authenticate to the container service
h) Activate the service account
i) Get the credentials for the container cluster from Gcloud
j) Run kubectl apply to create the kubernetes services
Full, tested, script at: https://pastebin.com/sZPrQuzD
If I put this sequence of steps in a scripts on an AWS EC2 instance and run it manually it works. However,the Jenkins build step fails at the the point kubectl is invoked to run the service, with the following error:
gcloud container clusters get-credentials jenkins-cd --zone europe-west1-b --project noon-prod
Fetching cluster endpoint and auth data.
ERROR: (gcloud.container.clusters.get-credentials) ResponseError: code=403, message=Request had insufficient authentication scopes.
Build step 'Execute shell' marked build as failure
The full error dump from the Jenkins run is as follows:
https://pastebin.com/pSWPQ5Ei
My questions:
a) How to fix this? Surely it can't be that difficult to get authentication running from Jenkins?
b) Is this the correct way to authenticate to the gcloud container service from a Jenkins system which is not on Gcloud infrastructure at all?
Many thanks in advance for any help!
Traiano
We're working on an open source project called Jenkins X which is a proposed sub project of the Jenkins foundation aimed at automating CI/CD on Kubernetes using Jenkins and GitOps for promotion.
We worked around some of the issues you've been having by running the Jenkins pipelines inside the kubernetes cluster; so there's no need to authenticate with GKE.
When you merge a change to the master branch, Jenkins X creates a new semantically versioned distribution of your app (pom.xml, jar, docker image, helm chart). The pipeline then automates the generation of Pull Requests to promote your application through all of the Environments via GitOps.
Here's a demo of how to automate CI/CD with multiple environments on Kubernetes using GitOps for promotion between environments and Preview Environments on Pull Requests - using Spring Boot and nodejs apps (but we support many languages + frameworks).

Resources