# The scheduled job on the Windows agent

> A nightly drift check in Declarative Pipeline and PowerShell: why H in a cron line, exit codes that actually fail a build, and the interpolation bug that cost me an evening.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-01-29
- Tags: Jenkins, CI/CD, Groovy, PowerShell, Windows
- URL: https://deyanpeev.com/posts/the-scheduled-job-on-the-windows-agent

The agent went online a fortnight ago, and the first thing I put on it was a
check instead of a build.

The failures that hurt were never the builds. They were "somebody changed a
box". An execution policy relaxed for one debugging session and left there, a
Defender exclusion that a group policy refresh reverted, a code-signing
certificate that no one was watching. Every one of those shows up as a red
build eventually, at the worst possible moment, and it looks like a code
problem.

So I wrote a scheduled job that runs on the Windows agents every weeknight,
collects what the machines look like, compares that against a baseline in the
repository, and tells somebody when the two disagree.

# The cron line

```groovy
triggers {
  cron('TZ=Europe/Sofia\nH 2 * * 1-5')
}
```

Three things in one line.

**`H` is not "hourly".** It's a hash of the job's name into the allowed
range. `0 2 * * *` across nine jobs means nine jobs starting at exactly
02:00:00 and a controller that falls over on the load spike. `H 2` gives this
job some minute between 02:00 and 02:59, and it's the *same* minute every
time, so the schedule is still deterministic and you can still reason about
it. Use `H` in every field you don't have a specific reason to pin.

**The timezone is the controller's, not the agent's.** Which is fine until your
controller is in UTC and your operations people are not, and "the overnight job"
starts arriving at 04:00 local in summer. The `TZ=` line at the top of the spec
fixes it explicitly.

**`1-5` because the report goes unread at the weekend.** A drift report that
lands on Saturday morning just gets marked read on Monday.

# The skeleton

```groovy
pipeline {
  agent { label 'windows' }

  options {
    timestamps()
    skipDefaultCheckout()
    disableConcurrentBuilds()
    timeout(time: 20, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '60'))
  }

  triggers {
    cron('TZ=Europe/Sofia\nH 2 * * 1-5')
  }

  parameters {
    booleanParam(name: 'FAIL_ON_DRIFT', defaultValue: true,
                 description: 'Uncheck for a dry run against a new baseline')
  }

  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Collect') {
      steps {
        powershell script: 'ci\\collect.ps1 -OutFile facts.json', label: 'collect facts'
      }
    }
    stage('Compare') {
      steps {
        script {
          def facts    = readJSON file: 'facts.json'
          def baseline = readJSON file: "ci/baseline/${env.NODE_NAME}.json"
          compare(facts, baseline)
        }
      }
    }
  }

  post {
    always  { archiveArtifacts artifacts: 'facts.json,drift.json',
                               allowEmptyArchive: true, fingerprint: true }
    unstable { notify('drifted') }
    failure  { notify('failed to run') }
    fixed    { notify('back to baseline') }
  }
}
```

`skipDefaultCheckout()` and then an explicit `checkout scm` looks redundant,
but it isn't. The default checkout happens before `options` has finished being
useful, and I want the checkout to be a stage I can see the timing of.
`timeout` is there because a hung `Get-ChildItem Cert:\` will otherwise sit on
your only Windows agent until somebody notices in the morning.

`disableConcurrentBuilds()` is there because two of these racing on the same
machine will produce two different answers, and you'll believe the wrong one.

# The step that does not fail

```groovy
powershell 'Get-Item C:\\does-not-exist'
```

That is green. `Get-Item` raises a *non-terminating* error, PowerShell writes
it to the error stream, execution continues, the script ends, and the process
exits zero. Durable Task sees zero and reports success. And your nightly check
has been passing for three weeks while checking nothing.

Depending on which version of the PowerShell plugin you have, the step may set
`$ErrorActionPreference` for you. Don't rely on it. Set it yourself, on the
first line of every script, along with strict mode:

```powershell
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
```

That covers cmdlets. It does not cover native executables, which don't raise
PowerShell errors at all - they just set `$LASTEXITCODE` and carry on. So there
is a wrapper, and everything goes through it:

```powershell
function Invoke-Native {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory)][string]   $Exe,
    [Parameter(ValueFromRemainingArguments)][string[]] $Arguments = @()
  )
  & $Exe @Arguments
  if ($LASTEXITCODE -ne 0) {
    throw "$Exe exited with $LASTEXITCODE"
  }
}
```

And when you do want the exit code and not a failure, ask Jenkins for it
instead of inventing something:

```groovy
def status = powershell(returnStatus: true, script: 'ci\\probe.ps1')
if (status == 2) {
  unstable('probe reported degraded')
}
```

# The interpolation bug

I lost an evening to this one, and I've watched two other people lose one
since.

```groovy
powershell """
  Write-Host "running on $env:COMPUTERNAME"
"""
```

Groovy sees a double-quoted string, finds `$env`, and interpolates it,
because `env` is a real object in a pipeline. What reaches PowerShell is the
`toString()` of an `EnvActionImpl` followed by a literal `:COMPUTERNAME`. If
you're luckier, you write `$_` somewhere and get a `MissingPropertyException`
for a property called `_`, and that at least fails loudly.

The rule has no exceptions. **PowerShell lives in single-quoted Groovy
strings.** `'''` for anything multi-line. If you need a Jenkins value on the
inside, it goes through the environment and not through the string:

```groovy
withEnv(["BASELINE=ci/baseline/${env.NODE_NAME}.json"]) {
  powershell '''
    $ErrorActionPreference = 'Stop'
    $baseline = Get-Content -Raw -Path $env:BASELINE | ConvertFrom-Json
  '''
}
```

This is also the security answer. An interpolated parameter is string
concatenation into a shell, the oldest injection in the book, and a build
parameter is attacker-controlled the moment anyone outside your team can
trigger the job.

# Credentials that stay out of the log

```groovy
withCredentials([usernamePassword(credentialsId: 'drift-check-reader',
                                  usernameVariable: 'CHECK_USER',
                                  passwordVariable: 'CHECK_PASS')]) {
  powershell '''
    $ErrorActionPreference = 'Stop'
    $secure = ConvertTo-SecureString $env:CHECK_PASS -AsPlainText -Force
    $cred   = [System.Management.Automation.PSCredential]::new($env:CHECK_USER, $secure)
    Invoke-Command -ComputerName $env:TARGET -Credential $cred -ScriptBlock { ... }
  '''
}
```

The masking is exact-match. Jenkins replaces the literal secret in the console
output, and it can't recognise the same secret after you have base64-encoded
it, URL-escaped it, or interpolated it into a connection string. So the rule
about never echoing a secret is there because the masker only handles the
obvious case.

There's one Windows-specific hazard. If PowerShell script block logging is
enabled by policy on the agent, the body of every script block goes into the
event log, literals included. That is a good control on a workstation and a
secret disclosure on a build agent. Decide which one this machine is.

# Handing data back to Groovy

There are two ways and only one of them survives contact.

You can use `returnStdout: true` and parse the string. It works for a
single value. It also captures everything anyone `Write-Host`ed, every
progress bar, and whatever a module decided to print on import, so as soon as
the output is structured you're writing a parser to strip other people's
chatter out of your data.

Or you write a file:

```powershell
$facts | ConvertTo-Json -Depth 8 | Set-Content -Path $OutFile -Encoding utf8
```

```groovy
def facts = readJSON file: 'facts.json'
```

Two arguments in there matter.

**`-Depth 8`.** `ConvertTo-Json` defaults to a depth of *two*. Anything below
that is silently replaced by the type name as a string. There's no error and no
warning. Your nested object becomes `"System.Object[]"` and your comparison
starts reporting that nothing ever changes. I've been caught by this twice.

**`-Encoding utf8`.** On Windows PowerShell 5.1 that writes a BOM. `readJSON`
copes, but plenty of other things don't, and a BOM in front of a `{` is a
famously baffling parse error. On `pwsh` 7, `utf8` is BOM-less and `utf8BOM`
is the opt-in. That's the right way round, and one more argument for doing
file-producing work in 7.

While you're at it, wrap every collection in `@()` before it goes into the
JSON. PowerShell unrolls a one-element array into the element, so a machine
with a single certificate serialises an object where every other machine
serialises a list, and your comparison code gets to find out at runtime.

# Drift is unstable, not failed

```groovy
def compare(facts, baseline) {
  def findings = evaluate(facts, baseline)
  writeJSON file: 'drift.json', pretty: 2,
            json: [host: facts.computerName, findings: findings]

  if (findings.isEmpty()) { return }

  findings.each { echo "${it.severity}: ${it.check} - ${it.detail}" }

  if (params.FAIL_ON_DRIFT) {
    unstable("${findings.size()} finding(s) against baseline")
  }
}
```

The distinction matters. **Unstable** means the check ran and the machine
isn't what we said it should be. **Failed** means the check itself fell over
and we currently know nothing. Those want different reactions from whoever is
on the receiving end, and if you collapse them into one red circle people
learn to ignore both.

Same reasoning for the notifications. `failure` and `fixed`, never `always`.
A job that emails on every run gets a mail rule within a week, and then the
one that mattered goes into the same folder.

# Fanning out

One agent became three, and a job per agent is three jobs to keep in step.
`nodesByLabel`, from the Node and Label Parameter plugin, turns the label into
the list:

```groovy
stage('Every Windows agent') {
  steps {
    script {
      def names = nodesByLabel label: 'windows', offline: false
      parallel names.collectEntries { name ->
        [(name): {
          node(name) {
            checkout scm
            powershell script: 'ci\\collect.ps1 -OutFile facts.json',
                       label: "collect on ${name}"
            compare(readJSON(file: 'facts.json'),
                    readJSON(file: "ci/baseline/${name}.json"))
          }
        }]
      }
    }
  }
}
```

`collectEntries` over the list is safe because `name` is the closure's own
parameter. The version that isn't safe, and that everybody writes once, is
the `for` loop:

```groovy
// Every branch runs against the last agent in the list.
def branches = [:]
for (name in names) {
  branches[name] = { node(name) { ... } }
}
```

All three closures capture the same variable, and by the time they run it
holds whatever the loop left in it. Copy it into a local inside the loop body,
or use `collectEntries` and stop thinking about it.

The other thing to know is that this is Groovy running under a
continuation-passing transform, and a chunk of what you think you know about
closures and iteration doesn't hold. That needs a post of its own, and it's
the one I am writing next month.

# The first three weeks

Three things, and no one had noticed any of them.

A code-signing certificate with five weeks left on it, which is enough time to
renew calmly and not enough to renew on the day of a release.

An agent whose `C:\j` Defender exclusion had gone. A group policy refresh had
put the machine back to the domain baseline. Build times on that box had gone
up by a third and everyone had decided the build was just slow now.

And one machine that had dropped back to the default TLS settings after a
patch cycle. That's the same failure I wrote about a fortnight ago and had
assumed was fixed permanently. It was, on the machine I fixed it on.

# What it cost

The pipeline is ninety lines of Groovy. The PowerShell behind it is two
hundred and fourteen, in a `.ps1` in the repository, and I can't test any of
it without a Windows box, a Jenkins agent and a night's patience.

That's the next problem, and it's the reason the work on this project stops
being Groovy from February.
