项目作者: souravatta

项目描述 :
This project demonstrate how to create and build a Jenkins Jobs using Jenkins CLI. The project has two sample files: a sample Jinja template for jenkins job creation and a sample yaml file for values to be used in creation of jenkins job.
高级语言:
项目地址: git://github.com/souravatta/Jenkins-CLI.git
创建时间: 2020-08-15T08:19:36Z
项目社区:https://github.com/souravatta/Jenkins-CLI

开源协议:

下载


Jenkins CLI to Create and Build job

This project demonstrate how to create and build a Jenkins Jobs using Jenkins CLI. The project has two sample files: a sample Jinja template for jenkins job creation and a sample yaml file for values to be used in creation of jenkins job.

jenkins #jenkinscli #jenkinsjob #pipelinejob


Jenkins CLI (In Action)

jenkins cli

Table of Contents


Using Jenkins CLI for the First Time

Requirements

  1. JAVA (OpenJDK 8 or above)
  2. Jenkins (Version 2.204.0 or newer)
  3. Python (Version 3.0 or later)
  4. Pip (pip2 or pip3)
  5. Jinja2 (Latest version) ```snap install j2```

Downloading and Configuring

The command line interface can be accessed over SSH or with the Jenkins CLI client, a .jar file distributed with Jenkins.

Using the CLI over SSH

  • SSH service is disabled by default for new Jenkins installation.
  • Go to Manage Jenkins → Configure Global Security. Under SSH Server, set the SSHD Port to Fixed (say 53801) or Random.

sshd-port

  • User used for authentication with Jenkins master must have Overall/Read permission in order to access CLI.
  • Add SSH public key for the user (which will be used to run Jenkins CLI commands) by navigating to Manage Jenkins → Manage Users → User Settings.

ssh-pub-key

  • Execute CLI help command, to check ssh service for CLI:
    1. ssh -l <user> -p <ssh-port> <hostname or ipaddress> help
    Example: ssh -l ubuntu -p 53801 localhost help

Using the CLI client

  • Dowload the CLI client jar from jenkins master using url JENKINS_URL/jnlpJars/jenkins-cli.jar
  • General syntax for using the CLI client:
    1. java -jar jenkins-cli.jar [-s JENKINS_URL] [global options...] command [command options...] [arguments...]
  • There are three basic mode for client connection: -http, -websocket and -ssh
  • SSH connection mode syntax:
    1. java -jar jenkins-cli.jar [-s JENKINS_URL] -ssh -user kohsuke command ...
    Example: java -jar jenkins-cli.jar -s http://13.xxx.xxx.xxx:8080/ -ssh -user ubuntu help

Usage

Step 1: Fork or Clone the repository https://github.com/souravatta/Jenkins-CLI.git

Step 2: Download the Jenkins CLI jar inside the cloned repository.

Step 3: Modify the values of pipeline.yaml

Step 4: Run the below command to create a jenkins job using Jenkins CLI.

  1. j2 -f yaml pipeline.j2 pipeline.yaml | java -jar jenkins-cli.jar -s http://13.xxx.xxx.xxx:8080/ -ssh -user ubuntu create-job test6

Step 5: Run the below command to build the jenkins job using Jenkins CLI.

  1. j2 -f yaml pipeline.j2 pipeline.yaml | java -jar jenkins-cli.jar -s http://13.233.173.108:8080/ -ssh -user ubuntu build test6 -f -v

ssh-pub-key

Note: Plugins may also provide CLI commands; in order to determine the full list of commands available in a given Jenkins environment, execute the CLI help command:

  1. ssh -l ubuntu -p 53801 localhost help

OR

  1. java -jar jenkins-cli.jar -s http://13.xxx.xxx.xxx:8080/ -ssh -user ubuntu help

Example (pipeline.yaml)

  1. pipeline_strategy:
  2. build:
  3. allow_concurrent: false
  4. properties:
  5. days_to_keep: -1
  6. num_to_keep: 5
  7. parameters:
  8. - name: GREET_NAME
  9. value: Sourav
  10. description: Name of the person to greet
  11. pipeline:
  12. git_url: https://github.com/souravatta/Install-Configure.git
  13. creds_id: github
  14. branch: master
  15. script_path: Jenkinsfile

Example (Jenkinsfile)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage ('Greeting'){
  5. steps {
  6. echo "Hello ${env.GREET_NAME}"
  7. }
  8. }
  9. }
  10. }