jenkinsci / jenkinsfile-runner

A command line tool to run Jenkinsfile as a function
MIT License
1.13k stars 291 forks source link
cli docker-image faas hacktoberfest jenkins jenkins-pipeline jenkinsfile jenkinsfile-runner

= Jenkinsfile Runner (Incubating project) :toc: :toc-placement: preamble :toclevels: 3

https://github.com/jenkinsci/jenkinsfile-runner/releases/latest[image:https://img.shields.io/github/v/release/jenkinsci/jenkinsfile-runner?include_prereleases&label=changelog[GitHub release (latest by date including pre-releases)]] https://github.com/jenkinsci/jenkinsfile-runner/graphs/contributors[image:https://img.shields.io/github/contributors/jenkinsci/jenkinsfile-runner[GitHub contributors]] link:https://github.com/orgs/jenkinsci/projects/22[image:https://img.shields.io/badge/JFR-roadmap-blue[Roadmap]] https://community.jenkins.io/c/contributing/jenkinsfile-runner/22[image:https://img.shields.io/badge/discourse-forum-brightgreen.svg?style=flat-square[Discourse]] https://gitter.im/jenkinsci/jenkinsfile-runner[image:https://badges.gitter.im/jenkinsci/jenkinsfile-runner.svg[Gitter]]

Jenkinsfile Runner packages Jenkins pipeline execution engine as a command line tool or as a Docker image.

== Use-cases

Target use cases include but not limited to:

== 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.

image:./demo/cwp/recording.gif[Jenkinsfile Runner Demo]

See link:./demo/cwp/[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:

. Download the Jenkinsfile Runner ZIP archive available in https://github.com/jenkinsci/jenkinsfile-runner/releases[GitHub Releases]. There is also a standalone JAR option available there, but this is a preview-only feature (see https://github.com/jenkinsci/jenkinsfile-runner/issues/350[issue #350]). Latest unreleased versions can be also built from source code. See the link:./CONTRIBUTING.adoc[contributing guidelines] if you want to build Jenkinsfile Runner on your own. . Prepare the execution environment . 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:

=== 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 -p -f ....

Sample Jenkinsfile:

[source,groovy]

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 https://picocli.info/[picocli] and https://github.com/kohsuke/args4j[args4j]. To execute a command:

.... jenkinsfile-runner ....

Supported commands:

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:

Advanced arguments:

==== 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:

Advanced options:

==== 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 https://hub.docker.com/r/jenkins/jenkinsfile-runner[jenkins/jenkinsfile-runner] repository on DockerHub.

=== Image Packs

There is a https://github.com/jenkinsci/jenkinsfile-runner-image-packs[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 https://hub.docker.com/r/jenkins/jenkinsfile-runner[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:

[source,bash]

docker run --rm -e JAVA_OPTS="-Xms 256m" -v $PWD/test:/workspace jenkins/jenkinsfile-runner

[source,bash]

$ 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:

== Reporting issues

Please use https://github.com/jenkinsci/jenkinsfile-runner/issues[GitHub Issues] to report defects and improvement ideas. If you see a security issue in the component, please follow the https://jenkins.io/security/#reporting-vulnerabilities[vulnerability reporting guidelines].

== Project roadmap

See the roadmap link:https://github.com/orgs/jenkinsci/projects/22[here].

== Further reading