Kubernetes Cron Job to trigger a Jenkins pipeline - jenkins

My team to to create a cron in Kubernetes/OpenShift that will trigger our Jenkins pipeline that we have set up. We tried doing the triggers{} syntax and the build periodically option on the Jenkins UI, however these are unreliable for us since whenever Jenkins restarts, those build triggers on Jenkins get removed.

There is two approaches you could use here:
Make your Jenkins stateless with the Configuration as Code plugin
Use a curlimages/curl container to trigger a job via the Jenkins REST API

Related

How to decouple Jenkins CI and gitlab CI pipelines?

I've only been working with Jenkins so far. We have configured a Multibranch Pipeline job to automatically build and test software. The tasks are written in Groovy and stored as Jenkinsfile in the root directory of our git repository.
Recently, we have decided to add another mechanism to automatically generate documentation. The generation of documentation (but this could be any other task) has been realized using GitLab CI.
Both pipelines are practically independent - and both are triggered by a git commit/push. What I do not understand is: why and how is the Jenkins pipeline execution associated with the GitLab CI pipeline? In the following screenshot a new column "External" appears - representing the Jenkins pipeline job.
That's not really a big issue. But as both pipelines should be independent - the results of the runs should not influence each other. However, it seems that when the Jenkins job fails, i.e. "External", the GitLab CI pipeline also fails:
Is there a way to better decouple those pipelines, i.e. let them fail or succeed individually?
This is because the Gitlab Branch Source Plugin automatically notify Gitlab about then Jenkins pipeline status. This allow you to see the result of a build directly in Gitlab. If you want to have only the result of the Gitlab CI pipeline in Gitlab, you can disable this feature :
Additional Traits:
These traits can be selected by selecting Add in
the Behaviours section.
[...]
Skip pipeline status notifications - Disable notifying GitLab server
about the pipeline status.
[...]
So in yout Gitlab group, just go the Configure > Projects > Gitlab Group > Add and select Skip pipeline status notifications.
why and how is the Jenkins pipeline execution associated with the GitLab CI pipeline? In the following screenshot a new column "External" appears - representing the Jenkins pipeline job.
In general, "External" statuses are created using the commit build status API -- Jenkins uses this API to report the Jenkins pipeline build status to GitLab CI.
This external status for Jenkins appears in your GitLab pipeline because you have configured your Jenkins server/project to report build statuses to GitLab or you have setup a webhook integration with Jenkins in GitLab (note these may be set at the group level or by an administrator, not necessarily the project level)
To remove this from your pipeline, you should disable any existing integration configurations and setup your Jenkins project independently of any GitLab integration. e.g. using git polling to trigger jenkins builds and remove any updateGitlabCommitStatus calls in your groovy scripts / build stages.

Ephemeral Jenkins Pipeline Jobs from Github and Jenkinsfile

I have automated Jenkins master and slaves deployment and redeployment successfully.
I know how to manually create pipeline jobs and add github repos to use their Jenkinsfiles for the steps.
my issue is how can I automate the pipeline jobs addition to jenkins after its been destroyed and redeployed without having to manually create the pipeline jobs and point to Jenkinsfile each time.
I have seen this done before in a container environment with chef and docker when redeployed or updated it re-adds all the pipelines automatically again.
I want to not use the UI at all only to confirm job status progress and verify settings.
I would recommend looking at the JobDSL Plugin to create jobs, using a seed job to create them on initial Jenkins startup. The Jenkins Configuration-as-Code plugin can be used to setup any other configuration outside the jobs.

Jenkins skip some jobs in chain of freestyle jobs

We got a requirement to implement CICD using Jenkins.
Here, Jenkins is running in windows machine and application server running in linux machine and build activity should happen in Linux system. So, We are connecting to linux machine using Jenkins's SSH plugin and executing jobs.
I have created list of freestyle jobs to checkout code from CVS, cleanup activity, Build , stop server, start server, Run Junit, run sonar. all these jobs are chained using 'build other projects' option in post build Action section.
Here, all jobs executes in sequential manner. But, sometimes I need to execute only few jobs like stop and start server.
So, please help me how we can randomly pick jobs which need to be run before triggering build.
Thanks,
Ganesha

How Can I trigger a jenkins job after successful completion of another job and both the jobs are running in different Jenkins server

I need to trigger a job in server1 after the successful completion of a job in Server2. Both the servers are under same domain.
Please have a look to the URLTrigger plugin for Jenkins.
Install this plugin on the server1.
On your server1/job, please configure the build trigger like below:
When the job will be completed on the server2, the server1/job will see a change on the buildNumber attribute.
It will trigger a new build on server1.
If you have a jenkins master, you could add the other servers as jenkins slaves and use the node('[slave name]') { ... } feature of the workflow plug in to start a build on that slave.

run a Jenkins job on another Jenkins instance from the Jenkins job

I want to create a Jenkins job that starts other Jenkins jobs. That would be quite easy, because Jenkins Template Project Plugin allows us to create a build step of a type "use builders from another project". However, what makes my situation harder is that I have to start Jenkins jobs on other machines. Is there any standard way to do that?
In case you want only to trigger new build of Job You Have plenty of ways to accomplish it
you can use remote access API and Trigger a request to build target job from source Job.
https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
Or you can use https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin
which is handy in handling server details and other stuff. you shoukld ensure ssh keys shared by both servers.

Resources