FluxGlow

How do I make a Jenkins build monitor for a wall screen?

Have each stage of the pipeline record whether it passed, then write a board file in a post step that always runs, and copy it somewhere a browser can read. No Jenkins plugin is involved.

The Jenkinsfile

Jenkinsfile
pipeline {
  agent any

  stages {
    stage('Build') {
      steps {
        sh 'make build'
      }
      post {
        success { script { env.BUILD_STATE = 'ok' } }
        failure { script { env.BUILD_STATE = 'bad' } }
      }
    }
    stage('Test') {
      steps {
        sh 'make test'
      }
      post {
        success { script { env.TEST_STATE = 'ok' } }
        failure { script { env.TEST_STATE = 'bad' } }
      }
    }
    stage('Deploy') {
      steps {
        sh 'make deploy'
      }
      post {
        success { script { env.DEPLOY_STATE = 'ok' } }
        failure { script { env.DEPLOY_STATE = 'bad' } }
      }
    }
  }

  post {
    always {
      // A stage that never ran has no state, so it is drawn idle.
      sh '''
cat > board.json <<EOF
{
  "title": "$JOB_BASE_NAME",
  "tag": "Build # $BUILD_NUMBER",
  "nodes": [
    { "id": "build", "label": "Build", "state": "${BUILD_STATE:-idle}" },
    { "id": "test", "label": "Test", "state": "${TEST_STATE:-idle}", "after": ["build"] },
    { "id": "deploy", "label": "Deploy", "state": "${DEPLOY_STATE:-idle}", "after": ["test"] }
  ]
}
EOF
aws s3 cp board.json s3://your-bucket/board.json --content-type application/json --cache-control no-cache
'''
    }
  }
}

How it works

  • Each stage has its own post block that records ok or bad in an environment variable.
  • When a stage fails, Jenkins skips the stages after it. Their variables are never set, so the shell falls back to idle and those orbs stay grey.
  • The post { always { } } block at the end runs whether the build passed, failed or was aborted. It writes the file and uploads it.

The result is a row of orbs, one for each stage, joined by arrows:

board.json
{
  "title": "checkout",
  "tag": "Build # 3082",
  "nodes": [
    { "id": "build", "label": "Build", "state": "ok" },
    { "id": "test", "label": "Test", "state": "bad", "after": ["build"] },
    { "id": "deploy", "label": "Deploy", "state": "idle", "after": ["test"] }
  ]
}

Where the file goes

The example copies the file to Amazon S3, which needs the AWS command line tool and credentials on the agent. Any of the places to host a board file will do.

The browser on the monitor reads the file from that place, not from Jenkins. Jenkins itself can stay behind your firewall, and the monitor needs no Jenkins login.

Showing more on each orb

Add fields to a node in the same way as state. To show when a stage last ran, set a time in the stage's post block and use it as since with a caption of "Last ran". To show a build as unstable rather than failed, record warn in the stage's unstable block.

One board for many jobs

A board file describes one board, so each pipeline that writes one has its own address. On a multibranch pipeline, put $BRANCH_NAME in the file name to get a board for each branch.

To put many jobs on one wall as a grid, use a small script on a timer instead. It asks Jenkins for the last build of each job, at /job/NAME/lastBuild/api/json, and writes one node for each:

Jenkins saysState
building is truerun, blue
result is SUCCESSok, green
result is UNSTABLEwarn, amber
result is FAILUREbad, red
result is ABORTED or NOT_BUILTidle, grey

Writing a board file in any language has a starting point for that script in six languages.

All questions