<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged jenkins</title>
    <link href="https://passingcuriosity.com/tags/jenkins/jenkins.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/jenkins/jenkins.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2021-06-30T00:00:00Z</updated>
    <entry>
    <title>Aborting supersede Jenkins builds</title>
    <link href="https://passingcuriosity.com/2021/aborting-superseded-jenkins-builds/" />
    <id>https://passingcuriosity.com/2021/aborting-superseded-jenkins-builds/</id>
    <published>2021-06-30T00:00:00Z</published>
    <updated>2021-06-30T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Even small teams can find that their continuous integration servers get bogged
down with redundant builds. Some developers like to “commit early, commit often”
and if they also “open a PR early and leave it sitting there in draft mode” then
you can quite easily have multiple builds linting and building and testing code
that has already been supersede.</p>
<p>Happily, you can use the <a href="https://plugins.jenkins.io/pipeline-milestone-step/">Pipeline: Milestone Step</a> plugin to have Jenkins
terminate already running builds of the same job. The goal here is
straighforward:</p>
<blockquote>
<p>My multibranch pipeline build is notified of PR-123. It creates a new job
called PR-123 and starts build 1.</p>
<p>I push another commit to the branch. GitHub notifies Jenkins and Jenkins
starts PR-123 build 2. I now have two running builds – build 1 and build 2
– but the outcome of build 1 is no longer useful.</p>
<p>Suppose I notice a type and immediately push a fix. This would result in
<em>three</em> running builds, <em>two</em> of them useless.</p>
</blockquote>
<p>Milestones can be useful in a range of circumstances but I mostly want to take
advantage of one feature:</p>
<blockquote>
<p>When a build passes a milestone, any older build that passed the previous
milestone but not this one is aborted.</p>
</blockquote>
<p>If we use the <code>BUILD_ID</code> as the milestone, then we can use this to abort old
jobs when a new one starts.</p>
<pre><code>node {
    stage(&quot;Prepare&quot;) {
        milestone label: '', ordinal: Integer.parseInt(env.BUILD_ID) - 1
        milestone label: '', ordinal: Integer.parseInt(env.BUILD_ID)
        checkout scm
    }
    stage(&quot;One&quot;) {
        sh &quot;&quot;&quot;sleep 60&quot;&quot;&quot;
    }
    stage(&quot;Two&quot;) {
        sh &quot;&quot;&quot;sleep 120&quot;&quot;&quot;
    }
    stage(&quot;Three&quot;){
        sh &quot;&quot;&quot;sleep 180&quot;&quot;&quot;
    }
}</code></pre>
<p>Like everything involving Jenkins, there are bound to be heaps of interactions
with other features and scenarios where it doesn’t work reliably. Good luck.</p>]]></summary>
</entry>
<entry>
    <title>Making GitHub pull request comments in a Jenkins pipeline</title>
    <link href="https://passingcuriosity.com/2021/jenkins-pipeline-update-pull-request-comments/" />
    <id>https://passingcuriosity.com/2021/jenkins-pipeline-update-pull-request-comments/</id>
    <published>2021-05-04T00:00:00Z</published>
    <updated>2021-05-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Sometimes all you want from your continuous integration setup is one bit: red vs
green. In other circumstances, you might like more detailed feedback. For
configuration management scenarios I like to see a [summary of] the planned
changes as a comment on my pull request.</p>
<p>If you use the <a href="https://plugins.jenkins.io/pipeline-github/">Pipeline: GitHub</a>
Jenkins plugin you can use code like this to:</p>
<ol type="1">
<li>Delete any previous comments made by your CI/CD pipeline on that PR.</li>
<li>Post a comment to the PR.</li>
</ol>
<pre><code>node {
    stage(&quot;Prepare&quot;) {
        checkout scm
    }

    stage(&quot;Build&quot;) {
        echo &quot;...&quot;
    }

    stage(&quot;Comment&quot;) {
        if (env.CHANGE_ID) {
            for (comment in pullRequest.comments) {
                /* Where &quot;automation-user&quot; is the scm account. */
                if (comment.user == &quot;automation-user&quot;) {
                    pullRequest.deleteComment(comment.id)
                }
            }
            def date = sh(returnStdout: true, script: &quot;date -u&quot;).trim()
            pullRequest.comment(&quot;Build ${env.BUILD_ID} ran at ${date}&quot;)
        }
    }
}</code></pre>]]></summary>
</entry>

</feed>
