How to use Jenkins pipeline to update a website on another server? - jenkins

I've setup and connected a Jenkins (2.249) server to my GitHub account, so it has access to my repos and I've setup the GitHub webhook.
But I am having problems trying to understand how to create a multibranch pipeline job to detect when a push to my master branch happens and then I want to run SSH commands on another host to update a web server with the new code changes.
With Jenkins pipelines, I can't see how to detect when a push to master happens and then trigger the build? Is this possible with Jenkins? I have Blue Ocean installed as well.

Multi-branch Pipeline jobs periodically check the server for updates. It sets an environment variable BRANCH_NAME with the current branch during execution.
If you only care about the master branch, you should use a regular Pipeline job that only watches master.
See the docs

Related

Jenkins does not run the building process after a push

I have the following initial situation:
I have a Docker container running Jenkins 2.379
This Jenkins has the Bitbucket Server Integration and the Bitbucket Branch Sourch Plugin installed
The connection to the Bitbucket server seems to work
I also set up a multibranch pipeline that listens to the repository in the connected Bitbucket server instance
And I have set the Scan Multibranch Pipeline Trigger to All pushes.
Finally, it seems that Jenkins has correctly implemented the webhook in the corresponding Bitbucket project.
Changes to some configurations of the multibranch pipeline trigger the scan process, which works properly. If there are changes in the code of the corresponding branch during a push, the build is triggered.
Clicking the "Scan Multibranch Pipeline Now" button has the same result.
But if I just push some code changes into a branch, nothing happens. My pipeline does not start automatically and no build process is started with the changes made.
Goal: Every push a developer does in a branch of this project should trigger the scan for new branches in Jenkis and the build process for new branches or those where something has changed.
I have found the problem/solution. As I said, my Jenkins runs in a Docker container and is hosted locally on my PC (localhost:8080).
Through the credentials, the path from Jenkins into Bitbucket worked and so did creating the webhook. However, for this webhook, the Bitbucket server plugin entered its address (localhost:8080). So now when a push into a Bitbucket repo happens, this webhook was triggered on Bitbucket's localhost:8080 (so presumably Bitbucket itself and not Jenkins).
The solution was now quite simple. I used a tool called ngrok to make my localhost:8080 (on which Jenkins runs) accessible via a URL from the internet.
I then only had to store this URL in Jenkis in system configurations and adapt the webhook in my Bitbucket repository.

Auto create Jenkins job from source code repo - no jenkins interaction

I am looking to auto create jobs in jenkins upon pull request , branches, master push etc similar to what we do in Gitlab. My SCM is butbucket here.
I have so far setup docker based agent integration with Jenkins and butbucket, when I create a job and configure it to use repo it all works fine , but I just want to remove altogether a step of job creation in jenkins and want the workflow like this:
In butbucket source code repo to keep all pipeline configuration for and branch and tag to trigger Jenkins pipeline without touching Jenkins for job creation or any config creation. Just want to drive all via the script in code repo for pipeline .
Any recommendations or help for workflow would be appreciated
I got the answer to my Question , hence listing the steps for very simple use case for how it would work.
Steps:
Go to bitbucket server repository to which you want to enable pull request based trigger. Add Post Recieve Hook to this repository "Webhook to Jenkins for Bitbucket Server" , Enable this hook to have connection to jenkins. Enabling this plugin will issue POST request to jenkins each time a new Pull request is opened.
On Jenkins Server this will work with Blue Ocean Pipeline which by default will pick the change for pull request branch and trigger the job on each pull request.
Blue Ocean pipeline will by default create multi branch pipeline job to work with bitbucket repository.

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.

Run Jenkins build for whichever branch was checked into on Gitlab

I recently made the transition from Subversion to Git for all my repos at work. However, with svn we had commit hooks in place so that our Jenkins job would run for whichever branch was checked into. Now, I'm trying to set this up using Gitlab and there appears to only be one place to add a web hook. It looks like any time something is checked into ANY branch, the web hook will run. Meaning if I have a branch_A associated with jenkins_job_A, something could be checked into branch_B and the commit hook for jenkins_job_A will still run. Is there a branch by branch way to configure these web hooks? Or is there some kind of script I can check into each branch that will act as a commit hook? Or (my fear) is this feature not supported in Gitlab?
I guess you set up GitLab to do a post commit request to http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>? In theory this should trigger the polling on all jobs that are configured with that URL, and in the polling step the jobs should decide whether they should build or not. In practice this will unfortunately cause all jobs to fire.
We worked around this issue by moving the Job configuration into a Jenkinsfile and then use a Multibranch Pipeline.
As an alternative you could also install the GitLab plugin for Jenkins and use the Jenkins integration in GitLab. This will allow you to trigger the correct jobs when commits are pushed on a branch. The downside is that it requires a per-job configuration.

Jenkins pipeline automatic branch detection

I am migrating my old Jenkins free-style job to multi-branch pipeline. I also want to use GitLab hook with them.
My problem is the branch detection. I am doing it manually but I want it to be automatic: when a new branch is pushed to git, GitLab trigger a Jenkins job that trigger the branch detection if the branch parameter from GitLab is not known for Jenkins at the moment. Is this possible to do it or doesn't this exist?
FYI: I tried to launch the multi-branch pipeline job but Jenkins says:
ERROR: No parameterized job named XXX found.
Enable "Build Periodcally" in your multibranch job configuration and the branch indexing will automatically started.
What you really need is a branch source plugin for GitLab with webhook integration, which is tracked as an RFE in JIRA.
Failing that, use a plain Git branch source and configure GitLab to send Jenkins notifications to /git/notifyCommit (IIRC) as documented on the Git plugin wiki. Need specify only a url, no other details. The branch indexing this triggers should both detect new or removed branches, and changes to the head of an existing branch, and schedule builds accordingly.
You can set webhook in GitLab for push events and URL like http://<yourserver>/git/notifyCommit?url=<URL of the Git repository>.
See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
GitLab notifies Jenkins on push events which should trigger branch detection also for multibranch pipeline.
I didn't receive the answer I wanted and I ran into this issue today that answered the question :
https://github.com/jenkinsci/gitlab-plugin/issues/298
TLDR: Multi-branch pipeline are not supported yet to be triggered by gitlab commit easily. There is a workaround. Look at the link above.

Resources