1. 2017-07-20 - Discard old builds in Jenkins; Tags: Discard old builds in Jenkins
    Loading...

    Discard old builds in Jenkins

    How to remove old builds as scripted pipeline in a Jenkinsfile? Not only I fail to see the sexiness in this new approach, but also I dislike the groovy syntax. In the end add I came up with several solutions to accomplish my goal: Keep only the last 10 builds. There are several ways and solutions. It was a huge try and error approach.

    Set properties

    Solution 1: Add before the node instruction the properties. This post help me a lot.

    properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '14', artifactNumToKeepStr: '10', daysToKeepStr: '14', numToKeepStr: '10']]])
    
    def isMaster = (env.BRANCH_NAME == 'master')
    def version = null
    
    node {
      // ..
    }
    

    Define Project Properties

    Solution 2: You can also use the def declaration.

    /* Only keep the 10 most recent builds. */
    def projectProperties = [
        [$class: 'BuildDiscarderProperty',strategy: [$class: 'LogRotator', numToKeepStr: '10']],
    ]
    
  2. 2016-12-24 - Deploy rpm file to Artifactory with maven deploy:deploy-file; Tags: Deploy rpm file to Artifactory with maven deploy:deploy-file
    Loading...

    Deploy rpm file to Artifactory with maven deploy:deploy-file

    The maven deploy plugin allows to deploy artifacts and files to your internal repository. If you happen to have a Maven Repository like Sonatype Nexus or JFrog’s Artifactory the plugin is the right choice. This post covers how to deploy a rpm file to Artifactory. The Maven repository itself is not covered here.

    First you have to define in your maven project (pom.xml) the deploy-file goal. Replace the properties. Following example is for a rpm generated file with Apache Maven.

    <build>
    <plugins>
        <!-- deploy rpm to artifactory -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <!-- must match id of settings.xml of Jenkins -->
                <repositoryId>artifactory</repositoryId>
                <url>${deployment.repository.url}</url>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${rpm.version}-${rpm.release}.noarch</version>
                <file>${rpm.copy.to.location}</file>
            </configuration>
        </plugin>
    </plugins>
    </build>
    

    Artifactory itself may have a snapshot and release repository, which is expressed through two maven profiles (test and prod).

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <deployment.repository.url>https://artifactory.cinhtau.net/artifactory/yum-snapshot-local
                </deployment.repository.url>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <deployment.repository.url>https://artifactory.cinhtau.net/artifactory/yum-release-local
                </deployment.repository.url>
            </properties>
        </profile>
    </profiles>
    

    After the rpm is generated, deploy your rpm within the Jenkinsfile to production if you happen to be on the git master branch, otherwise put it the snapshot repository.

    stage('Build: artifactory') {
    	if (isMaster) {
    		sh "${mvnHome}/bin/mvn rpm:version deploy:deploy-file --activate-profiles prod -Dbuild.number=${BUILD_NUMBER}"
    	} else {
    		sh "${mvnHome}/bin/mvn rpm:version deploy:deploy-file --activate-profiles test -Dbuild.number=${BUILD_NUMBER}"
    	}
    }
    
  3. 2016-12-21 - Parallel steps with Jenkinsfile; Tags: Parallel steps with Jenkinsfile
    Loading...

    Parallel steps with Jenkinsfile

    Pipeline offers a straight-forward syntax for branching your pipeline into parallel steps. Following example demonstrate how to use the parallel steps with Jenkins. The example is based on my use case, Jenkins generates a rpm artifact with custom software. That software is deployed on multiple servers (aws data centers).

    node {
        stage("Build") { // build rpm }
        stage("Deploy") {
            def servers = ["aws-frankfurt", "aws-london", "aws-dublin"]
            def stepsForParallel = [:]
            for (int i = 0; i < servers.size(); i++) {
                def s = servers.get(i)
                def deployStep = "${s}"
                stepsForParallel[deployStep] = transformIntoStep(s)
            }
            parallel stepsForParallel
        }
    }
    def transformIntoStep(String serverName) {
        return {
            node {
                // install rpm
                sh "ssh -q -t vinh@${serverName} \"sudo yum install ${rpmfile} -y\""
            }
        }
    }
    

    The LinkedList servers contains all the host names. We put into the map stepsForParallel for each servername a new jenkins node with the rpm installation directive. Each node is executed for itself. Don’t use stages for a step! All steps are in the stage deploy but are separated by the step identifier.

    Parallel Steps with Jenkins