Coder Social home page Coder Social logo

jenkins's Introduction

Jenkins Jobs

These templates are imported as a DSL job in Jenkins and then new jobs are created from these templates.

TOC

Jenkins Job DSL API

The job-dsl-plugin allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.

Summary

Jenkins is a wonderful system for managing builds, and people love using its UI to configure jobs. Unfortunately, as the number of jobs grows, maintaining them becomes tedious, and the paradigm of using a UI falls apart. Additionally, the common pattern in this situation is to copy jobs to create new ones, these "children" have a habit of diverging from their original "template" and consequently it becomes difficult to maintain consistency between these jobs.

The Jenkins job-dsl-plugin attempts to solve this problem by allowing jobs to be defined with the absolute minimum necessary in a programmatic form, with the help of templates that are synced with the generated jobs. The goal is for your project to be able to define all the jobs they want to be related to their project, declaring their intent for the jobs, leaving the common stuff up to a template that were defined earlier or hidden behind the DSL.

More info at: Jenkins Job DSL API and Job DSL Plugin.

Updating Credentials

In some cases we will find yourcredentials inside our Groovy files. These credentials need to be updated with a new ones. For that we will go to Jenkins.

  • Go to credentials
  • Click Jenkins (under 'Stores scoped to Jenkins')
  • Click Global credentials (unrestricted) (under 'System')
  • On the left side click on Add Credentials

Once our credentials are created a new ID will be generated. This ID should be replaced on yourcredentials.

Plugins

Active Choices (Official Site)

The Active Choices plugin allows the creation of dynamic and interactive parameters for freestyle Jenkins jobs. Active Choice parameters can be dynamically updated and can be rendered as combo-boxes, check-boxes, radio-buttons or rich HTML.

AnsiColor (Official Site)

This plugin adds support for ANSI escape sequences, including color, to Console Output

Configure projects to run with specified authorization.

CloudBees Docker Build and Publish (Official Site)

This plugin provides the ability to build projects with a Dockerfile, and publish the resultant tagged image (repo) to the docker registry.

Docker Plugin (Official Site)

This plugin allows slaves to be dynamically provisioned using Docker.

Email Extension (Official Site)

This plugin allows you to configure every aspect of email notifications. You can customize when an email is sent, who should receive it, and what the email says.

Environment Injector (EnvInject) (Official Site)

EnvInject plugin provides the following features:

  • Removes inherited environment variables by the Jenkins Java process

  • Injects environment variables at node (master/slave) startup

  • Executes a setup script before or/and after a SCM checkout for a run

  • Injects environment variables before or/and after a SCM checkout for a run

  • Injects environment variables as a build step for a run

  • Injects password values for a run

  • Exports environment variables at the end of the build in order to to know the set of environment variables used for each build

  • DSL information

Extra Columns (Official Site)

This plugin is supposed to group multiple extra columns in one convenient plug-in. Maybe other simple column plug-ins can be merged into this one in the future.

Git Parameter (Official Site)

Adds ability to choose branches, tags or revisions from git repository configured in project.

InfluxDB (Official Site)

Sends Jenkins build metrics into InfluxDB.

JaCoCo (Official Site)

This plugin allows you to capture code coverage report from JaCoCo. Jenkins will generate the trend report of coverage and some other statistics. It also includes functionality to include columns in Dashboards which displays the latest overall coverage numbers and links to the coverage report.

Jenkins user build vars (Official Site)

This plugin provides a set of environment variables that describe the user who started the build.

Job Configuration History (Official Site)

Saves copies of all job and system configurations.

Job DSL (Official Site)

The job-dsl-plugin allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.

Allows JUnit-format test results to be published.

Locale (Official Site)

This plugin controls the language of JenkinsNormally, Jenkins honors the browser's language preference if a translation is available for the preferred language, and uses the system default locale for messages during a build.

MSBuild (Official Site)

This plugin allows you to use MSBuild to build .NET projects.

MSTest (Official Site)

This plugin converts MSTest TRX test reports into JUnit XML reports so it can be integrated with Jenkin's JUnit features. This plugin converts the .coveragexml files found in the project workspace to the EMMA format.

MSTestRunner (Official Site)

This plugin allow you to execute test using MSTest command line tool.

Furter information: https://www.automatetheplanet.com/configure-jenkins-mstest-execution/

Test Results Analyzer (Official Site)

A plugin that shows history of test execution results in a tabular format.

Parameterized Scheduler plugin (Official Site)

This plugin is for configuring a cron style timer schedule for parameterized builds.

Further information:

Pipeline Utility Steps(Official Site)

Small, miscellaneous, cross platform utility steps for Pipeline Plugin jobs

PostBuildScript (Official Site)

This plugin allows you to run the following actions after a build:

  • Batch or shell scripts
  • Groovy scripts
  • Build steps

You can configure these actions depending on the build status (i.e., only run when build is successful). Scripts can also be executed on the master, on slaves or both. On matrix projects, the build can be executed on each axis.

Please refer to the plugin description for further information.

PowerShell (Official Site)

Provides Jenkins integration with Windows PowerShell

Prometheus metrics (Official Site)

Exposes a Prometheus metrics endpoint.

Warnings Next Generation (Official Site)

This plugin collects compiler warnings or issues reported by static analysis tools and visualizes the results. Jenkins' Warnings Next Generation Plugin collects compiler warnings or issues reported by static analysis tools and visualizes the results. It has built-in support for almost hundred static analysis tools (including several compilers), see the list of supported report formats. It replaces the Jenkins Static Analysis Suite that has been part of the Jenkins plugin eco-system for more than ten years now. I.e. it makes the following Jenkins plugins obsolete: Android Lint, CheckStyle, Dry, FindBugs, PMD, Warnings, Static Analysis Utilities, Static Analysis Collector Plugins, Task Scanner, etc.

Sidebar Link (Official Site)

Add links in the sidebar of the Jenkins main page, view tabs and project pages.This simple plugin adds an Additional Sidebar Links section in the main Jenkins configuration page, with settings for link URLs, texts and icons. These links will be shown in the top-level Jenkins pages (main page, user list, build history, My Projects and other project view tabs). Sidebar links for particular jobs may also be added in the job configuration pages.

Simple Theme (Official Site)

This plugin allows to customize Jenkin's appearance with custom CSS and JavaScript. It also allows to replace the Favicon.

Add the following under -> Jenkins > Configure System > Theme

More info here.

Slack Notification (Official Site)

Provides Jenkins notification integration with Slack or Slack compatible applications like RocketChat and Mattermost.

SonarQube Scanner (Official Site)

This plugin allow easy integration of SonarQube™, the open source platform for Continuous Inspection of code quality.

/userContent in Git (Official Site)

Jenkins has a mechanism known as "User Content", where administrators can place files inside $JENKINS_HOME/userContent, and these files are served from http://yourhost/jenkins/userContent. This can be thought of as a mini HTTP server to serve images, stylesheets, and other static resources that you can use from various description fields inside Jenkins.

This plugin exposes this this $JENKINS_HOME/userContent directory as a Git repository, allowing administrators to use git to push/pull changes and manage them with history.

Once this plugin is installed, see http://yourserver/jenkins/userContent.git in your browser for how to access this repository.

This plugin makes it possible to publish the test results of an execution of a testing tool in Jenkins.

Jenkins useful folder paths

definition description path
jenkins_home jenkins default installation path /var/lib/jenkins
jobs place where builds status are stored $jenkins_home/jobs
userContent place to store static files and serve them as a url $jenkins_home/userContent
workspace place where projects are cloned $jenkins_home/workspace
plugins place where plugins are installed $jenkins_home/plugins
jenkins.war place where jenkins.war file is located as a service ($> whereis jenkins ) /usr/share/jenkins
jenkins In order to pass Java arguments to Jenkins you need to change the Jenkins service configuration file /etc/default/jenkins
jenkins.log log file of jenkins /var/log/jenkins

Using docker as a build executor

  1. Install Docker Plugin.
  2. Configure Jenkins to work with docker: Manage Jenkins > Configure system > Cloud > docker like the image below. cloud_docker_configuration
  3. Now that you have the slave configurations ready, you can create a job, select “Restrict where this project can be run” option and select the docker host as slave using the label as shown below. docker_slave_agent.jpg "docker_slave_agent.jpg")

Enable Windows PSRemoting

Enable-PSRemoting -Force
winrm set winrm/config/service/Auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}'

Delete Jenkins workspace @tmp files using crontab and scheduler

Linux

To edit or create your own crontab file (for specific user), type the following command at the UNIX / Linux shell prompt:

$> crontab -e

Then type in the file:

# Delete Jenkins @tmp folders in /var/lib/jenkins/workspace. From mon-fri at 09:00
0 9 * * 1-5 rm -rf /var/lib/jenkins/workspace/*@tmp

After saving a new file (username) will be created in: /var/spool/cron/crontabs/

To check if the cron has been run we perform the following command:

$> tailf -n 100 /var/log/syslog | grep CRON

Windows

Open "Task Scheduler" and add the "delete_jenkins" task.

We will start Powershell program with arguments:

Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: Remove-Item -Path C:\Jenkins\workspace\* -Filter *@tmp

We add the desired trigger, for example everyday at 09:00 am.

Custom scripts:

Linux (Python) - Cloning docker images from one repo to another

# This progam clones docker images from one repo (harbor) to another (azure container registry)

import datetime
import time
import docker

fromrepo = ('<user>', '<password>', '<origin_repo>')
fromrepoproject = '/cloudmaster'
torepo = ('<user>', '<password>', '<destination_repo>')
images = {'/jenkins':'1.1.0',
        '/nginx':'2.0.0'
        }

client = docker.from_env()

start = datetime.datetime.now()
start_time = time.time()

for repo in fromrepo,torepo:
    client.login(username=repo[0], password=repo[1], registry=repo[2])
    print('Login in -> '+ repo[2] + ' <- succeeded!')

for image,tag in sorted(images.items()):
    origin = fromrepo[2]+fromrepoproject+image+':'+tag
    destination = torepo[2]+image+':'+tag

    print('Pulling the following image: ' + origin)
    client.images.pull(origin)
    print('Tagging the following image: ' + origin)
    client.images.get(origin).tag(torepo[2]+image, tag)
    print('Pushing the following image: ' + destination)
    client.images.push(torepo[2]+image, tag)
    print('Removing generated images: ' + origin + ' and ' + destination )
    client.images.remove(origin, True)
    client.images.remove(destination, True)

client.images.list()

finish = datetime.datetime.now()

print("The process started at: " + start.strftime("%Y/%m/%d %H:%M:%S") + " and finished at: " + finish.strftime("%Y/%m/%d %H:%M:%S"))
print("The execution took: %s seconds." % (time.time() - start_time))

Linux (bash) - Change file encoding from CRLF to LF

#!/bin/bash

# This script changes the line enconding to Linux LF
for file in $(find $directory -type f -name "*.sls")
do
	dos2unix $file
done

Linux (bash) - Change file encoding from CRLF to LF (path is passed as argument)

#!/bin/bash

displayError()
{
  echo "Error!"
  echo "Usage: $0 <path>"
  exit 1
}

if (($# < 1)); then
  displayError
fi

# This script changes the line enconding to Linux LF
for file in $(find $1 -type f -name "*.sls")
do
  dos2unix $file
done

Linux (bash) - Change file encoding from CRLF to LF with prompt (path is passed as argument)

#!/bin/bash

# This script changes the line enconding to Linux LF

displayError()
{
  echo "Error!"
  echo "Usage: $0 <path>"
  exit 1
}

if (($# < 1)); then
  displayError
fi

echo "The following files will be changed:"
echo "------------------------------------"
for file in $(find $1 -type f -name "*.sls")
do
  ls $file
done
echo "------------------------------------"

read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
  for file in $(find $1 -type f -name "*.sls")
  do 
    dos2unix $file
  done
else
    echo "Aborted"
fi

Linux (bash) - Display all characters between 'Version' and ']' characters

version=$(sed -n '1p' changelog.txt | sed 's/.*Version \(.*\)].*/\1/')
# ------------------------------- #
Example:
changelog.txt
[Version 1.0.0] 2018/01/01
Output: 1.0.0

Windows (Powershell) - Display version number from a file

# --- Getting service version --- #
$file = "$env:WORKSPACE/changelog.txt"
$version=Get-Content $file -TotalCount 1
$version -Match '(?s)\[.*?]+' | Out-Null
$version1 = $Matches.Values
$version2=$version1.Remove(0,9)
$finalversion=$version2 -replace ".{1}$"
echo $finalversion
# ------------------------------- #
Example:
changelog.txt
[Version 1.0.0]
Output: 1.0.0

Linux (bash) - Date format

echo Date: $(date +"%Y/%m/%d at %T")

Windows (Powershell) - Date format

Get-Date -UFormat "%Y/%m/%d at %H:%M:%S"

Additional links:

jenkins's People

Contributors

joudaon avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

p2a8t4a5a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.