JenkinsCI - henk52/knowledgesharing GitHub Wiki

Jenkins Cookbook

Introduction

  • ACL: Anti corruption layer

References

Other CI servers

Notes on CI/CD

Get the Jenkins cli jar file

  • wget --no-proxy http://localhost:8080/jnlpJars/jenkins-cli.jar
  • java -jar jenkins-cli.jar -s http://localhost:8080/ help

Execute via jenkins-cli.jar on a server with authentication

  • generate the ssh keys: ssh-keygen -trsa
  • Create the user you are executing as, in the Jenkins web server
  • cat ~/.ssh/id_rsa.pub
  • copy the public key content to the http://localhost:8080/user/YOUR_USER/configure

Jobs

Create a job

See:

  • java -jar jenkins-cli.jar -s http://localhost:8080/ get-job MyTemplateJob > template.xml
  • Create a job.xml file.
  • java -jar jenkins-cli.jar -s http://localhost:8080/ create-job SmokeTest < smoketest.xml

Nodes

Add a node via the Web interface

Add the node names to the /etc/hosts

  • Click: Manage Jenkins
  • Click: Manage Nodes
  • Click: New Node
    • Node name: XXX
    • Dumb ????
  • Click: Ok
  • fill in
    • remote root directory: /var/lib/jenkins
    • Labels: nightly
    • Usage:
    • Launch method: Launch slave agent ... ssh
    • x
      • Kind: ssh username with private key
      • Scope: Global
      • Username: jenkins
      • description:
      • Private Key: From the Jenkins master ~/.ssh
      • Click Add.
    • Availability:

Add node via CLI

Please note, you have to have set the credentials for jenkins, prior to running this.

  • java -jar jenkins-cli.jar -s http://localhost:8080/ create-node < node_tst3.xml
<?xml version="1.0" encoding="UTF-8"?>
<slave>
  <name>NODE_NAME</name>
  <description>NODE DESCRIPTION.</description>
  <remoteFS>/var/lib/jenkins</remoteFS>
  <numExecutors>3</numExecutors>
  <mode>EXCLUSIVE</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="[email protected]">
    <host>NODE_HOST_NAME</host>
    <port>22</port>
    <credentialsId>CREDENTIAL_ID_PREVIOUSLY_CREATED</credentialsId>
    <maxNumRetries>0</maxNumRetries>
    <retryWaitTime>0</retryWaitTime>
  </launcher>
  <label>OPTIOANL_LABELS</label>
  <nodeProperties/>
</slave>

Adding more heap space to Java

  • See also:

Add '-Xmx1048m' to the JENKINS_JAVA_OPTIONS in /etc/sysconfig/jenkins

Then restart the jenkins service.

Data transfer

Copy data from test machine to Jenkins Job

Copy data from jenkins agent job to parent job

There are two ways to do this:

  • Slave copies to a repo and master copies from that repo.
    • All slaves and Master must have credentials to access the repo
  • Master copies from slave.
    • The Master must know where the slave stores the data
  • Slave copy to master.
    • The slave must have write access

Slave copies to a repo and master copies from that repo

  • How can slave copy data to repo:

    • scp (which requires credentials)
    • nfs mount
    • web interface
    • sftp
  • Slave copies data to repo (with a predefined naming convention)

  • Master copies data from repo.

  • Master removes data from repo.

Setup:

  • On repo
    • Create user: cimstr
    • create ~cimstr/.ssh/authorizedkey with public key from jenkins master
    • create user+group: cislave
    • create ~cislave/.ssh/authorizedkey with public key from jenkins slave(s)
    • mkdir /var/cirepo
    • chown -R cimstr:cislave /var/cirepo
  • On slave(s)
    • write the common pub+priv key pair from puppet repo.
  • On Master
    • Nothing to be done.

Jenkins Pipeline operations

  • - List of all steps/plugins for Pipeline

  • Declarative: You use the plugins and base language functions.

  • Scripted: You write groovy scripting of execution.

Telemetry

Grafana data:

  • jenkins.job.waiting.duration.m1_rate
  • jenkins.job.queuing.duration.m1_rate
  • jenkins.plugins.withUpdate (alert when > 0)

Junit output

<?xml version="1.0" ?>
<testsuites>
    <testsuite errors="0" failures="0" name="first test suite" tests="1">
        <testcase classname="some.class.name" name="Test1" time="123.456000">
            <system-out>
                standard out of the test run
            </system-out>
            <system-err>
                standard error of the test run
            </system-err>
        </testcase>
    </testsuite>
</testsuites>

Troubleshooting

Jenkins can't access node, after IP address was changed on node

Stopping and starting the jenkins server fixed this.

  • service jenkins stop
  • service jenkins start

Host key verification failed

Fix: update jenkins' ~/.ssh/known_hosts, with the remote key (perhaps interactively ssh to the host).

Host key verification failed.
fatal: Could not read from remote repository.

See also: You can set the node for manual verification and then switch to automatic afterwards...

Guidelines

  • Put in breakers so that if e.g. build statistics can't be created, then it should not break the pipeline.

Concourse

⚠️ **GitHub.com Fallback** ⚠️