GitLab CI/CD for Puppet + r10k: The Forced Command Pattern
Contents
Most guides for automating r10k with CI show you something like this in the pipeline:
deploy:
script:
- ssh puppet@puppet-master r10k deploy environment -p
That works, but it means your CI runner needs an SSH key that can run arbitrary commands on your puppet master. You’re either giving it a normal shell (dangerous) or writing sudo rules (tedious). There’s a cleaner pattern: let the SSH key itself be the policy.
How We Got Here
Automating r10k has an awkward history. The canonical 2014 reference — Phil Zimmerman’s “An Automated R10K Workflow That Works”1 — required Sinatra, Capistrano, Jenkins, and Ruby just to trigger a deploy. It worked, but it was a lot of machinery to maintain alongside your actual Puppet code.
The next generation used git hooks directly: pre-receive for tests, post-receive to trigger puppet-sync or r10k. This was simpler, but hooks have a well-known problem — they don’t fire when you push through the GitLab web UI. So your CI story worked from the command line and silently didn’t work for everyone else. The SSEC presentation from 20162 documents this evolution clearly and is worth reading for historical context.
GitLab CI/CD made the pipeline a first-class citizen and eliminated the hooks problem. A more recent approach — documented here using GitHub Actions and Webhookd3 — gets event-driven deploys working cleanly but adds another component to the stack.
Most guides, old and new, still carry over the same assumption: the CI script runs r10k, which means the runner needs broad SSH access to the puppet master. That’s the assumption worth discarding.
The Setup
We run a Puppet control repo on GitLab for ~60 nodes — workstations and ML compute. The deploy flow is: merge to production → GitLab CI triggers r10k on the puppet master → r10k deploys the production environment and pulls down all Puppetfile modules.
The key components:
- A GitLab shell runner registered directly on
puppet-master, taggedpuppet-deploy - An ed25519 SSH key owned by the
gitlab-runnerOS user - A forced command in the
puppetuser’sauthorized_keys
The Forced Command Pattern
This is the part that isn’t explained in most tutorials.
In ~puppet/.ssh/authorized_keys:
command="/usr/bin/r10k deploy environment -p",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-ed25519 AAAA... gitlab-runner@puppet-master
When gitlab-runner SSHes as puppet, the server ignores whatever command the client tries to run and executes /usr/bin/r10k deploy environment -p instead. Always. That’s it.
The CI job becomes:
deploy-production:
stage: deploy
tags:
- puppet-deploy
script:
- ssh -T $PUPPET_DEPLOY_USER@$PUPPET_MASTER
only:
- production
No r10k invocation in the CI script. No command passed over SSH. The key determines what happens. If someone compromises the runner, the worst they can do is trigger an r10k deploy — which only pulls from your git repo anyway.
Why a Shell Runner, and Why On the Master
We use a shell runner (not Docker) registered directly on the puppet master. The alternative — a remote runner that SSHes into the master — works fine but adds one more hop and one more place to manage keys. Since the runner lives on the master, the “SSH” in the CI job is a localhost connection, and the forced command still applies.
The runner is registered with a specific tag (puppet-deploy) so deploy jobs only run on this runner. Validation and test jobs use Docker runners and never touch the master.
What -p Does (Don’t Skip It)
r10k deploy environment -p deploys the environment and all modules from the Puppetfile. Without -p, r10k updates your control repo code but doesn’t pull updated module versions. This is the most common thing people get wrong the first time — module changes don’t appear after deploy and there’s no obvious error.
CI Stage Structure
Three stages, deliberately ordered:
- validate — puppet syntax check, r10k puppetfile syntax, YAML lint. Runs on Docker, fast, blocks everything downstream.
- test — rspec-puppet unit tests. Also Docker. MR validation catches regressions here before anything reaches production.
- deploy — only on
productionbranch push, shell runner, forced SSH.
MRs run validate and test but not deploy. The production push runs all three. This means you get fast feedback on feature branches without accidentally deploying them.
Setup Steps
Tested on Debian/Ubuntu. Adjust package manager and paths for RHEL-based systems.
# Install gitlab-runner on the puppet master
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install gitlab-runner
# Register (token from GitLab → repo → Settings → CI/CD → Runners → New project runner)
sudo gitlab-runner register \
--url https://gitlab.com \
--token <token> \
--executor shell \
--description "puppet-deploy" \
--tag-list puppet-deploy
# Generate SSH key for the gitlab-runner user
sudo -u gitlab-runner ssh-keygen -t ed25519 -f /home/gitlab-runner/.ssh/id_ed25519 -N ""
# Add forced command to puppet user's authorized_keys
# (replace <pubkey> with the contents of /home/gitlab-runner/.ssh/id_ed25519.pub)
sudo -u puppet bash -c 'echo "command=\"/usr/bin/r10k deploy environment -p\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding <pubkey>" >> ~/.ssh/authorized_keys'
# Verify — should produce r10k output, not a shell prompt
sudo -u gitlab-runner ssh -T puppet@localhost
GitLab CI/CD variables (Settings → CI/CD → Variables):
PUPPET_MASTER= hostname of your puppet masterPUPPET_DEPLOY_USER=puppet
Conclusion
The forced command is the pattern. Everything else is plumbing. The key determines what happens; the CI script is just a trigger. Once that clicks, the security story writes itself.
http://philzim.com/2014/05/02/an-automated-r10k-workflow-that-works/ The original r10k automation reference. Heavy stack (Sinatra, Capistrano, Jenkins, Ruby) but well-documented and influential. ↩︎
https://www.ssec.wisc.edu/~scottn/puppet_gitlab_r10k.pdf 2016 conference presentation by Scott Nolin at UW Madison’s Space Science and Engineering Center. Covers the evolution from hooks to GitLab CI clearly. ↩︎
https://redat.me/posts/fully-automated-puppet-code-deployment-using-r10k-github-actions-and-webhooks/ A more recent approach using GitHub Actions and Webhookd. Event-driven and clean, but adds Webhookd as an extra component and doesn’t restrict SSH key scope. ↩︎