• Stars
    star
    1,137
  • Rank 40,971 (Top 0.9 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A command line tool to run Jenkinsfile as a function

Jenkinsfile Runner (Incubating project)

Use-cases

Target use cases include but not limited to:

  • Using Jenkins in a Function-as-a-Service context.

  • Assist editing and testing Jenkins Pipeline definitions and libraries locally.

  • Integration testing of Pipelines.

Quick Demo

The demo below demonstrates running of a simple Pipeline with Jenkinsfile Runner. It is based on the Vanilla distribution which includes the minimum required set of plugins for running pipelines.

Jenkinsfile Runner Demo

See this directory for the source codes of the demo. There are more demos available in the project.

Usage in command-line

Jenkinsfile Runner can be run in the command line or in Docker. To run it in the command line follow these steps:

  1. Download the Jenkinsfile Runner ZIP archive available in GitHub Releases.

    • There is also a standalone JAR option available there, but this is a preview-only feature (see issue #350).

    • Latest unreleased versions can be also built from source code. See the contributing guidelines if you want to build Jenkinsfile Runner on your own.

  2. Prepare the execution environment

  3. Run the Pipeline

Preparation

Jenkinsfile Runner bundle includes the Jenkins core and a minimum set of plugins. More plugins can be installed on-demand. If it is not needed, just skip this step.

To add plugins, create a directory and assemble all the plugins that you’d like to use with the build. There are two ways to do it:

  • Use the Plugin Installation Manager Tool to install plugins into a new plugin directory.

  • Run the Jenkins setup wizard and install the desired set of plugins. After that, the plugins can be retrieved from JENKINS_HOME/plugins.

Execution

Say you have your Git repository checked out at ~/foo that contains Jenkinsfile and your source code. You can now run Jenkinsfile Runner like this:

jenkinsfile-runner -w <path to war> -p <path to plugins> -f <path to Jenkinsfile>

Sample Jenkinsfile:

pipeline {
    agent any
    stages {
        stage('Print hello') {
            steps {
                echo 'Hello world!'
            }
        }
    }
}

Output:

$ ./app/target/appassembler/bin/jenkinsfile-runner -w /tmp/jenkins -p /tmp/jenkins_home/plugins -f ~/foo/ -a "param1=Hello&param2=value2"
Started
Resume disabled by user, switching to high-performance, low-durability mode.
[Pipeline] node
[Pipeline] {
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Print hello)
[Pipeline] echo
Hello world!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The exit code reflects the result of the build. The demo directory includes a few simple examples of Jenkinsfiles you can try out.

Command Line Interface (CLI)

The Jenkinsfile Runner CLI provides multiple advanced options and commands. The CLI is powered by picocli and args4j. To execute a command:

 jenkinsfile-runner <command>

Supported commands:

  • run - Runs the Jenkinsfile. This command also runs by default if no subcommands specified.

  • lint - Lints the Jenkinsfile without actually running it. Only applicable to Declarative Pipeline.

  • cli - Runs interactive Jenkins CLI from where you can access all standard Jenkins commands provided by the Jenkins core and installed plugins: list-plugins, groovy, groovysh, etc.

  • generate-completion - Generate bash/zsh completion script for Jenkinsfile Runner.

  • version - Shows the Jenkinsfile Runner version.

  • help - Displays help information about the specified command.

All commands provide additional CLI arguments which can be accessed through help methods.

Jenkins Instance Arguments

Some CLI commands, including run require a startup of the Jenkins controller instance within Jenkinsfile Runner.

Common arguments:

  • -w (--jenkins-war) FILE - Path to exploded Jenkins WAR directory. Depending on packaging, it may contain the entire WAR file or just resources to be loaded by the WAR file, for example Groovy hooks or extra libraries.

  • -jv (--jenkins-version) VAL : Jenkins version to use if the WAR file is not specified. Defaults to latest LTS.

  • -p (--plugins) FILE - Plugins required for the run. Should point to either a plugins.txt file or to a /plugins installation directory Defaults to plugins.txt.

Advanced arguments:

  • --jenkinsHome FILE - Path to the empty Jenkins Home directory to use for this run. If not specified a temporary directory will be created. Note that the specified folder will not be disposed after the run.

  • --mirror - Mirror site to be used to download plugins if plugins.txt is specified.

    • NOTE: This option will be reworked in the future once the Plugin Installation Manager tool is integrated

  • --withInitHooks FILE - Path to the Groovy init hooks directory Hooks can be also passed via WEB-INF/groovy.init.d/** directory within the Jenkins WAR resource loader defined in --jenkins-war.

  • --skipShutdown - Skips the Jenkins shutdown logic to improve performance. Jenkinsfile Runner will abort the instance instead of gracefully releasing the resources. For example, agent connections will not be terminated. Also, plugin @Terminator extensions will not be invoked. It may lead to undefined behavior in the system, including potential data loss. This option is considered safe for the Vanilla package with the default plugin set.

  • --httpPort - Port for exposing the web server and Jenkins Web UI from Jenkinsfile Runner. Disabled by default.

  • --httpPath - The root path/prefix for expositng the web server and Jenkins Web UI from Jenkinsfile Runner.

  • --openWebUI - Open Jenkins Web UI in the default browser, --httpPort is expected to be defined together with this option.

  • --waitOnExit - Keep Jenkinsfile Runner running upon job completion without various sleep() hacks in the Pipeline.

  • --agentPort - Port for connecting inbound Jenkins agents (over JNLP or WebSockets). Disabled by default.

Running Jenkinsfiles (run command)

This is the main command in Jenkinsfile Runner. It executes all types of Pipeline definitions supported by Jenkinsfile Runner. Usage:

 jenkinsfile-runner run -w [warPath] -p [pluginsDirPath] -f [jenkinsfilePath] [other options]

In addition to Jenkins instance arguments defined above, it supports the following options:

  • -f (--file) FILE - Path to Jenkinsfile (or directory containing a Jenkinsfile) to run, defaults to ./Jenkinsfile.

  • -a (--arg) - Parameters to be passed to the Pipeline job. Use multiple -a switches for multiple params. All parameters will be recognized by Jenkins as String values.

Advanced options:

  • --runWorkspace FILE - Path to the workspace of the run to be used within the node{} context. It applies to both Jenkins controller and agents if any.

  • -u (--keep-undefined-parameters) - Keep undefined parameters if set, defaults to false.

  • -ns (--no-sandbox) - Run Pipeline job execution without the sandbox environment and script security checks. Use at your own risk.

  • -n (--job-name) VAL - Name of the job the run belongs to, defaults to 'job'

  • -b (--build-number) N- Build number of the run, defaults to 1.

  • -c (--cause) VAL- A string describing the cause of the run. It will be attached to the build so that it appears in the build log and becomes available to plug-ins and pipeline steps.

  • --scm FILE - A YAML file defining the SCM and optional credentials to use with the SCM. If given, the SCM will be checked out into the workspace automatically in Declarative Pipelines, and will be available for use with checkout scm in Scripted Pipelines. Note that an SCM cannot currently be used with Pipeline as YAML. See this doc for more details.

Passing parameters

Any parameter values, for parameters defined on workflow job within parameters statement, can be passed to the Jenkinsfile Runner using -a or --arg switches in the key=value format.

$ ./app/target/appassembler/bin/jenkinsfile-runner \
  -w /tmp/jenkins \
  -p /tmp/jenkins_home/plugins \
  -f ~/foo/ \
  # pipeline has two parameters param1 and param2
  -a "param1=Hello" \
  -a "param2=value2"

Usage in Docker

Containerized Pipeline execution is one of the main Jenkinsfile Runner use-cases. The project provides official Docker images which can be used and extended for custom use-cases.

Vanilla Distribution

This repository provides the Vanilla distribution. This package includes the minimum required set of plugins for running pipelines, but it needs to be extended in order to run real-world pipelines. The image is available in the jenkins/jenkinsfile-runner repository on DockerHub.

Image Packs

There is a Jenkinsfile Runner Image Packs repository. It provides additional Docker images for common use-cases, e.g. for building Java projects with Maven or Gradle. Each image includes a set of Jenkins plugins, configurations and Pipeline libraries which are commonly used in the desired technology stack. Image packs are available in the experimental jenkins4eval/jenkinsfile-runner repository on DockerHub.

Running Jenkinsfile Runner in Docker

Jenkinsfile Runner images can be launched simply as…

    docker run --rm -v $(pwd)/Jenkinsfile:/workspace/Jenkinsfile jenkins/jenkinsfile-runner

Advanced options:

  • JAVA_OPTS environment variable can be passed to pass extra JVM arguments to the image

docker run --rm -e JAVA_OPTS="-Xms 256m" -v $PWD/test:/workspace jenkins/jenkinsfile-runner
  • In the Vanilla Dockerfile the master workspace is mapped to /build. This directory can be exposed as a volume. The Docker image generated with Custom War Packager maps the workspace to /build by default and it can be exposed as well. However it is possible to override that directory if both the -v docker option and the --runWorkspace Jenkinsfile Runner option are specified.

  • By default the JENKINS_HOME folder is randomly created and disposed afterwards. With the --runHome parameter in combination with the -v Docker option it is possible to specify a folder. e.g. docker run -v /local/Jenkinsfile:/workspace/Jenkinsfile -v /local/jenkinsHome:/jenkinsHome ${JENKINSFILE_RUNNER_IMAGE} --runHome /jenkinsHome. This way you can access the build metadata in <jenkinsHome>/jobs/job/builds/1, like the build.xml, logs, and workflow data, even after the container finished.

  • The -ns and -a options can be specified and passed to the image in the same way as the command line execution. Using a non-sandbox environment may pose potential security risks. We strongly encourage you not to use this mode unless it is strictly necessary and always with extreme care and at your own risk.

  • You may pass --cli to obtain an interactive Jenkins CLI session. To get an interactive Jenkins CLI shell in the container, pass -i -e FORCE_JENKINS_CLI=true to docker run as extra parameters.

  • If you want to change default parameters for plugins or workspace, you can get onto the container by overriding entrypoint - binary is placed in /app/bin/jenkinsfile-runner.

$ docker run --rm -it -v $PWD/test:/workspace --entrypoint bash jenkinsfile-runner:my-production-jenkins
root@dec4c0f12478:/src# cp -r /app/jenkins /tmp/jenkins
root@dec4c0f12478:/src# /app/bin/jenkinsfile-runner -w /tmp/jenkins -p /usr/share/jenkins/ref/plugins -f /workspace

Extending Jenkinsfile Runner

Jenkinsfile Runner provides a vanilla image which includes some plugins and configurations. Usually, it is not enough to run a real Jenkins Pipelines. It might be required to install extra plugins and tools, and then to configure Jenkins so that Pipelines can properly operate in the user environment.

There are 2 ways to extend Jenkinsfile Runner:

  • Using low-level management tools, including the --plugins command included in CLI.

  • Docker

    • A simple example that converts a Jenkins image into a JFR can be found here.

    • A more advanced example using the Custom WAR/Docker Packager which automates some build steps and allows managing Jenkinsfile Runner configuration via a single YAML file can be found here.

Reporting issues

Please use GitHub Issues to report defects and improvement ideas. If you see a security issue in the component, please follow the vulnerability reporting guidelines.

Project roadmap

See the roadmap here.

More Repositories

1

jenkins

Jenkins automation server
Java
21,381
star
2

docker

Docker official jenkins repo
Dockerfile
6,144
star
3

pipeline-examples

A collection of examples, tips and tricks and snippets of scripting for the Jenkins Pipeline plugin
Groovy
4,117
star
4

blueocean-plugin

Blue Ocean is a reboot of the Jenkins CI/CD User Experience
Java
2,877
star
5

configuration-as-code-plugin

Jenkins Configuration as Code Plugin
Java
2,521
star
6

kubernetes-plugin

Jenkins plugin to run dynamic agents in a Kubernetes/Docker environment
Java
2,206
star
7

job-dsl-plugin

A Groovy DSL for Jenkins Jobs - Sweeeeet!
Groovy
1,851
star
8

pipeline-plugin

Obsolete home for Pipeline plugins
1,711
star
9

JenkinsPipelineUnit

Framework for unit testing Jenkins pipelines
Groovy
1,426
star
10

gitlab-plugin

A Jenkins plugin for interfacing with GitLab
Java
1,418
star
11

java-client-api

A Jenkins API client for Java
Java
888
star
12

jenkins-scripts

Scripts in Groovy, shell, Ruby, Python, whatever for managing/interacting with Jenkins
Groovy
880
star
13

build-monitor-plugin

Jenkins Build Monitor Plugin
Java
722
star
14

git-plugin

Git repository access for Jenkins jobs
Java
668
star
15

slack-plugin

A Jenkins plugin for posting notifications to a Slack channel
Java
664
star
16

pipeline-model-definition-plugin

Groovy
562
star
17

docker-workflow-plugin

Jenkins plugin which allows building, testing, and using Docker images from Jenkins Pipeline projects.
Java
500
star
18

ghprb-plugin

github pull requests builder plugin for Jenkins
Java
495
star
19

docker-plugin

Jenkins Cloud Plugin that uses Docker
Java
482
star
20

docker-inbound-agent

Docker image for a Jenkins agent which can connect to Jenkins using TCP or Websocket protocols
PowerShell
466
star
21

helm-charts

Jenkins helm charts
Mustache
448
star
22

pipeline-aws-plugin

Jenkins Pipeline Step Plugin for AWS
Java
423
star
23

jenkins.rb

Deprecated, see https://www.jenkins.io/jep/7
Ruby
394
star
24

generic-webhook-trigger-plugin

Can receive any HTTP request, extract any values from JSON or XML and trigger a job with those values available as variables. Works with GitHub, GitLab, Bitbucket, Jira and many more.
Java
377
star
25

email-ext-plugin

Jenkins Email Extension Plugin
Java
338
star
26

dingtalk-plugin

Dingtalk for jenkins
Java
336
star
27

warnings-ng-plugin

Jenkins Warnings Plugin - Next Generation
Java
323
star
28

plugin-installation-manager-tool

Plugin Manager CLI tool for Jenkins
Java
301
star
29

mesos-plugin

Mesos Cloud Jenkins Plugin
Java
290
star
30

github-plugin

Jenkins GitHub plugin
Java
286
star
31

ec2-plugin

Jenkins ec2 plugin
Java
285
star
32

ssh-steps-plugin

Jenkins pipeline steps which provides SSH facilities such as command execution or file transfer for continuous delivery.
Java
273
star
33

ansicolor-plugin

Jenkins ANSI Color Plugin
Java
252
star
34

pipeline-utility-steps-plugin

Small, miscellaneous, cross platform utility steps for Jenkins Pipeline jobs.
Java
237
star
35

docker-agent

Base Docker image for Jenkins Agents
PowerShell
231
star
36

ansible-plugin

Jenkins Ansible plugin
Java
223
star
37

workflow-cps-global-lib-plugin

Java
223
star
38

hashicorp-vault-plugin

Jenkins plugin to populate environment variables from secrets stored in HashiCorp's Vault.
Java
218
star
39

lib-file-leak-detector

Java agent that detects file handle leak
Java
217
star
40

bitbucket-branch-source-plugin

Bitbucket Branch Source Plugin
Java
213
star
41

gerrit-trigger-plugin

Java
212
star
42

remoting

Jenkins Remoting module
Java
212
star
43

workflow-aggregator-plugin

211
star
44

android-emulator-plugin

Android Emulator plugin for Jenkins
Java
207
star
45

docker-slaves-plugin

A Jenkins plugin to run builds inside Docker containers
Java
205
star
46

github-branch-source-plugin

GitHub Branch Source Plugin
Java
205
star
47

pipeline-stage-view-plugin

Visualizes Jenkins pipelines
JavaScript
204
star
48

jenkinsfile-runner-github-actions

Jenkins single-shot pipeline execution in a GitHub Action POC
Shell
199
star
49

amazon-ecs-plugin

Amazon EC2 Container Service Plugin for Jenkins
Java
193
star
50

trilead-ssh2

Patched trilead-ssh2 used in Jenkins
Java
193
star
51

cucumber-reports-plugin

Jenkins plugin to generate cucumber-jvm reports
Java
192
star
52

docker-build-publish-plugin

Java
192
star
53

performance-plugin

Performance Test Running and Reporting for Jenkins CI
Java
190
star
54

jira-plugin

Jenkins jira plugin
Java
169
star
55

gitea-plugin

This plugin provides the Jenkins integration for Gitea.
Java
168
star
56

embeddable-build-status-plugin

Embed build status of Jenkins jobs in web pages
Java
167
star
57

stashnotifier-plugin

A Jenkins Plugin to notify Atlassian Stash|Bitbucket of build results
Java
163
star
58

docker-ssh-agent

Docker image for Jenkins agents connected over SSH
PowerShell
162
star
59

workflow-cps-plugin

Java
160
star
60

http-request-plugin

This plugin does a request to an url with some parameters.
Java
154
star
61

kubernetes-pipeline-plugin

Kubernetes Pipeline is Jenkins plugin which extends Jenkins Pipeline to provide native support for using Kubernetes pods, secrets and volumes to perform builds
Java
154
star
62

stapler

Stapler web framework
Java
154
star
63

tfs-plugin

Jenkins tfs plugin
Java
145
star
64

jep

Jenkins Enhancement Proposals
Shell
144
star
65

kubernetes-cd-plugin

A Jenkins plugin to deploy to Kubernetes cluster
Java
140
star
66

jacoco-plugin

Jenkins JaCoCo Plugin
Java
139
star
67

qy-wechat-notification-plugin

企业微信Jenkins构建通知插件
Java
138
star
68

swarm-plugin

Jenkins swarm plugin
Java
135
star
69

git-client-plugin

Git client API for Jenkins plugins
Java
130
star
70

dependency-check-plugin

Jenkins plugin for OWASP Dependency-Check. Inspects project components for known vulnerabilities (e.g. CVEs).
Java
129
star
71

subversion-plugin

Jenkins subversion plugin
Java
127
star
72

git-parameter-plugin

Jenkins plugin for chosing Revision / Tag before build
Java
121
star
73

role-strategy-plugin

Jenkins Role-Strategy plugin
Java
120
star
74

groovy-sandbox

(Deprecated) Compile-time transformer to run Groovy code in a restrictive sandbox
Java
120
star
75

jenkins-design-language

Styles, assets, and React classes for Jenkins Design Language
TypeScript
116
star
76

acceptance-test-harness

Acceptance tests cases for Jenkins and its plugins based on selenium and docker.
Java
116
star
77

publish-over-ssh-plugin

Java
115
star
78

gitlab-branch-source-plugin

A Jenkins Plugin for GitLab Multibranch Pipeline jobs and Folder Organization
Java
115
star
79

scm-sync-configuration-plugin

Jenkins scm-sync-configuration plugin
Java
115
star
80

pipeline-as-yaml-plugin

Jenkins Pipeline As Yaml Plugin
Java
114
star
81

selenium-plugin

Jenkins selenium plugin
Java
112
star
82

docker-build-step-plugin

Java
111
star
83

jira-trigger-plugin

Triggers a build when a certain condition is matched in JIRA
Groovy
111
star
84

code-coverage-api-plugin

Deprecated Jenkins Code Coverage Plugin
Java
110
star
85

gradle-plugin

Jenkins gradle plugin
Java
109
star
86

cobertura-plugin

Jenkins cobertura plugin
Java
109
star
87

credentials-plugin

Provides Jenkins with extension points to securely store, manage, and bind credentials data to other Jenkins plugins, builds, pipelines, etc.
Java
107
star
88

jira-steps-plugin

Jenkins pipeline steps for integration with JIRA.
Java
105
star
89

artifactory-plugin

Jenkins artifactory plugin
Java
104
star
90

build-flow-plugin

A plugin to manage job orchestration
Groovy
103
star
91

github-oauth-plugin

Jenkins authentication plugin using GitHub OAuth as the source.
Java
102
star
92

throttle-concurrent-builds-plugin

Java
101
star
93

pipeline-graph-view-plugin

Java
99
star
94

promoted-builds-plugin

Jenkins Promoted Builds Plugin
Java
96
star
95

ssh-slaves-plugin

SSH Build Agents Plugin for Jenkins
Java
96
star
96

github-pr-coverage-status-plugin

Nice test coverage icon for your pull requests just from Jenkins
Java
93
star
97

jenkins-test-harness

Unit test framework for Jenkins core and its plugins
Java
92
star
98

localization-zh-cn-plugin

Chinese Localization for Jenkins
HTML
91
star
99

opentelemetry-plugin

Monitor and observe Jenkins with OpenTelemetry.
Java
90
star
100

office-365-connector-plugin

Office 365 Connector plugin sends jobs status notifications to Microsoft Teams or Outlook
Java
89
star