Skip to content

Instantly share code, notes, and snippets.

@ojacques
Created July 22, 2021 11:47
Show Gist options
  • Save ojacques/0b7939d67f2baa303eccca2e1f3a94ac to your computer and use it in GitHub Desktop.
Save ojacques/0b7939d67f2baa303eccca2e1f3a94ac to your computer and use it in GitHub Desktop.
Debugging Jenkinsfile

How to execute locally Jenkinsfile

One time setup: Jenkins

  • Install a local Jenkins (avoid Docker to avoid to have port mapping, volume and other issues)
  • https://jenkins.io/download/ -> get the war file
  • Start Jenkins:
wget [jenkins.war]
mkdir jenkins_home
JENKINS_HOME=$PWD/jenkins_home java -jar jenkins.war
  • Open localhost:8080 in chrome
  • Enter the temporary key / password
  • Do not install any plugin
  • Go to Manage Jenkins / plugins / proxy: setup the web proxy
  • Install pipeline and GIT plugin
  • Get Jenkins CLI: curl -OL http://localhost:8080/jnlpJars/jenkins-cli.jar

cd ~
mkdir jenkinsfile-dev
cd jenkinsfile-dev
git init
echo jenkins_home > .gitigore
echo jenkins.war >> .gitignore
git add .gitignore
git commit -m "Initial commit"

Edit Jenkinsfile:

pipeline {
  agent any
  stages {
    stage ("Do It") {
       steps { echo "testing" }
    }
  }
}

The repository is local (it does not have to be on GitHub). So, all the changes in Jenkinsfile are local and avoid a round trip.

We create a small script which

New Jenkins Job

  • Job type: pipeline or multibranch pipeline
  • In the Pipeline section: pipeline script from SCM
    • Repository URL: file:///tmp/jenkinsfile-dev

Script to commit/run Jenkins job

#!/bin/bash
git diff --quiet || git commit -am "x"
java -jar jenkins-cli.jar -s http://localhost:8080 \ 
  - auth admin:admin -http -noKeyAuth \
  build -s -v jenkinsfile-nightmare-demo

Clean up GIT repo

Now, let's get rid of X intermediary commits.

git rebase -i [commit hash]

for each X line, use f (fixup)

git commit --amend
git rebase --continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment