Attempting to provide github credentials to jenkins pipeline job - jenkins

I'm trying to set up the credentials for github in a jenkins pipeline job. I have the following in my pipeline script:
pipeline {
agent any
git([url: 'ssh://git#github.com/user/repname/', branch: 'master', credentialsId: 'xxx-xxx-xxx'])
Where does the credentialsId come from? Is this created elsewhere in Jenkins?
Update:
I pulled the credentials id from this page:
But now I am seeing this error:
Started by user anonymous
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: WorkflowScript: 3: Undefined section "git" # line 3,
column 5.

As you found yourself, it's the credentials id provided in the credentials view.
Now, as for your second problem, you're using declarative pipeline, it requires that you have the following structure:
pipeline {
agent any
stages {
stage('Example') {
steps {
git([url: 'ssh://git#github.com/user/repname/', branch: 'master', credentialsId: 'xxx-xxx-xxx'])
}
}
}
}
E.g. you need to put the git step insde of the stages, stage and steps clauses (documentation of this can be found here).
Alternatively you can use scripted pipeline, then it becomes:
node {
git([url: 'ssh://git#github.com/user/repname/', branch: 'master', credentialsId: 'xxx-xxx-xxx'])
}
However, when you're creating simple pipelines, then declarative pipelines provides a lot of nice to have features. See my answer here for a comparison between declarative and scripted pipelines.

Related

Is it Possible to Run Jenkinsfile from Jenkinsfile

Currently we are developing centralized control system for our CI/CD projects. There are many projects with many branches so we are using multibranch pipeline ( This forces us to use Jenkinsfile from project branches so we can't provide custom Jenkinsfile like Pipeline projects ). We want to control everything under 1 git repo where for every project there should be kubernetes YAMLS's, Dockerfile and Jenkinsfile. When developer presses build button, Jenkinsfile from their project repo suppose to run our jenkinsfile. Is it possible to do this?
E.g. :
pipeline {
agent any
stages {
stage('Retrieve Jenkinsfile From Repo') { // RETRIEVE JENKINSFILE FROM REPO
steps {
git branch: "master",
credentialsId: 'gitlab_credentials',
url: "jenkinsfile_repo"
scripts {
// RUN JENKINSFILE FROM THE REPO
}
}
}
}
}
Main reason we are doing this, there are sensetive context in jenkinsfile like production database connections. We don't want to store jenkinsfile under developers' repo. Also you can suggest correct way to achieve that beside using only 1 repo.
EDIT: https://plugins.jenkins.io/remote-file/
This plugin solved all my problems. I could'not try comments below
As an option you can use pipeline build step.
pipeline {
agent any
stages {
stage ('build another job') {
steps {
build 'second_job_name_here'
}
}
}
}
Try load step
scripts {
// rename Jenkinsfile to .groovy
sh 'mv Jenkinsfile Jenkins.groovy'
// RUN JENKINSFILE FROM THE REPO
load 'Jenkinsfile.groovy'
}

Using Jenkins global credentials in a pipeline (Jenkinsfile)

I am currently automating a project in Jenkins. I am using a pipeline that reads and executes a Jenkinsfile from a Source Management Tool (GIT in my case). For it to happen, I give the git URL and supply credentials with 'Jenkins Credentials Provider'and execute the build. It reads the Jenkinsfile and checks out the code, but fails at the next stage:
pipeline{
agent any
stages{
...
stage('Cloning GIT Repo'){
steps{
echo 'Cloning the repository'
git url: 'http://test.git'
}
}
...
It gives the error:
No credentials specified
It there a way for me to use the global credentials, I specified in the Jenkins UI earlier?
You can use credentialsId param
git(
url: 'http://test.git',
credentialsId: 'jenkins-credentials',
branch: "${branch}"
)
https://jenkins.io/doc/book/pipeline/jenkinsfile/#optional-step-arguments
https://jenkins.io/doc/pipeline/examples/#push-git-repo

Jenkins declarative pipeline upon Bitbucket pull request

I am building a declarative pipeline Jenkinsfile for semantic branching. It has the format:
pipeline {
stages {
stage('develop branch build') {
when {
branch 'develop'
}
// build and deploy to QA environment
}
stage('release branch build') {
when {
branch 'release'
}
// build and deploy to live/preproduction environment
}
}
}
I would like an additional stage to run upon a Bitbucket pull request. It would do a particular PR test and deploy task, and pass or fail the pipeline accordingly.
How might I enhance this script to do that?
I use the generic webhook plugin. This work pretty nice with bitbucket.

Jenkins declarative pipeline with Docker/Dockerfile agent from SCM

With Jenkins using the Declarative Pipeline Syntax how do i get the Dockerfile (Dockerfile.ci in this example) from the SCM (Git) since the agent block is executed before all the stages?
pipeline {
agent {
dockerfile {
filename 'Dockerfile.ci'
}
}
stage ('Checkout') {
steps {
git(
url: 'https://www.github.com/...',
credentialsId: 'CREDENTIALS',
branch: "develop"
)
}
}
[...]
}
In all the examples i've seen, the Dockerfile seems to be already present in the workspace.
You could try to declare agent for each stage separately, for checkout stage you could use some default agent and docker agent for others.
pipeline {
agent none
stage ('Checkout') {
agent any
steps {
git(
url: 'https://www.github.com/...',
credentialsId: 'CREDENTIALS',
branch: "develop"
)
}
}
stage ('Build') {
agent {
dockerfile {
filename 'Dockerfile.ci'
}
steps {
[...]
}
}
}
[...]
}
If you're using a multi-branch pipeline it automatically checks out your SCM before evaluating the agent. So in that case you can specify the agent from a file in the SCM.
The answer is in the Jenkins documentation on the Dockerfile parameter:
In order to use this option, the Jenkinsfile must be loaded from
either a Multibranch Pipeline or a Pipeline from SCM.
Just scroll down to the Dockerfile section, and it's documented there.
The obvious problem with this approach is that it impairs pipeline development. Now instead of testing code in a pipeline field on the server, it must be committed to the source repository for each testable change. NOTE also that the Jenkinsfile checkout cannot be sparse or lightweight as that will only pick up the script -- and not any accompanying Dockerfile to be built.
I can think of a couple ways to work around this.
Develop against agents in nodes with the reuseNode true directive. Then when code is stable, the separate agent blocks can be combined together at the top of the Jenkinsfile which must then be loaded from the SCM.
Develop using the dir() solution that specs the exact workspace directory, or alternately use one of the other examples in this solution.

Github project setup in Jenkins 2.x

I have a Multibranch Pipeline project which configures Jenkins Jobs based on a Jenkinsfile per branch. The sourcecode is hosted on a Github Enterprise Server.
When I view the configuration of a branch which is created by the Jenkinsfile, I noticed that there is a option GitHub project. This option allows to define the URL of the corresponding GitHub project.
I want to define this property via my Jenkinsfile in Pipeline syntax, but I don't know command to use and how.
Relevant parts of my Jenkinsfile:
pipeline {
agent {
docker {
image 'plinzen/android:latest'
label 'android'
}
}
triggers {
githubPush()
}
stages {
stage('build') {
steps {
checkout scm
sh './gradlew clean assembleDebug'
}
}
}
}
How can I define the GitHub project properties via my Jenkinsfile? I use the Jenkins GitHub Plugin in my project.
You can add a new agent node and add this code snippet to do your things. For more info you can refer to this url also. For Additional Info. Hope this helps.
git(
url: 'git#github.com<repo_name>.git',
credentialsId: 'xpc',
branch: '${branch}'
)

Resources