Chapter 1 · Azure DevOps as code
Azure DevOps Managed as Code
The Azure side of this platform is all Terraform, but most of what keeps
it safe lives in Azure DevOps: who can push to main, which pipeline can
deploy where, which identity a pipeline signs in as. If those are set by
hand in the portal, they drift, and nobody notices until something gets
through. So they are Terraform too, in a repo called ado_management.
This is where I started, and the reason comes from work. Teams were always asking us, the DevOps team, for new repos, and each one was set up by hand: the repo, its branch policies, permissions, pipeline environments. Every new repo meant remembering all of it again, and it came out slightly different each time. The answer there was a custom scaffolding tool.
Scaffolding is a fair option, and it does make every new repo start the same way. The trade-off comes after it runs. A scaffolding tool creates things and walks away, so nothing keeps track of what each repo is supposed to look like: there's no state of the current configuration, and a repo that drifts looks the same as one that doesn't. Changing something later is a challenge too. A new branch policy for every repo means finding everything the tool ever created and fixing each one, or writing another script to do it.
With Terraform and well-defined modules, a new repo is a few lines of config, usually copied from an existing repo's entry. It can start empty, or be created by importing a template repo, so it arrives with the right structure already in it. The landing zone and shared-services repos here started that way, both imported from the same infra template. Branch policies, permissions and pipeline environments sit in the same entry, so nobody has to remember them. The state is what scaffolding can't give: a plan shows any repo that no longer matches its config, and a change to a shared policy is one edit that every repo picks up on the next apply. Switching a policy off is a pull request, and the history shows who did it and when.
How it's laid out
There is one folder per Azure DevOps project (platform for the shared
repos, rafaelsonego for this site and its infrastructure). Each folder is
mostly data: a set of locals describing that project's repos, groups,
policies, environments, service connections and variable groups. One
shared module turns that data into resources, and it is built from nine
terraform-azuredevops-* modules published to the same
private registry as the Azure ones.
The repo is applied by its own pipeline, which extends the terraform_ado
template from the shared pipeline templates,
so a change to a branch policy goes through a pull request like any other
change. State lives in the same Azure storage account as the
infrastructure state, read with Entra ID rather than an account key.
What it enforces
mainis protected everywhere. All 48 repos require a pull request with at least one reviewer from the project's DevOps group, resolved comments and a passing build. Only squash merges are allowed, somainreads as one commit per change.- Commits come from known addresses. A commit author email policy rejects pushes whose author isn't on the allowed list.
- Deployments go through environments. Each pipeline that changes
something real (the infrastructure apply, this site's deploy) runs in an
Azure DevOps environment with an approval check and a branch check that
only allows
mainor a tag. A feature branch can plan, but it can't apply. - Pipelines sign in to Azure without secrets. Service connections use workload identity federation, one per workload and region, each tied to the service principal the landing zone created for it.
Granting a pipeline exactly enough
When the infrastructure pipelines started opening pull requests with architecture diagrams, the build service needed write access to those repos. The quick way is to add the Project Collection Build Service Accounts group in the portal. That group covers every project's pipelines, and looking it up from Terraform needs collection-level rights.
Instead, the configuration looks up the build service identities
themselves (data "azuredevops_users" filtered to service identities, then
by display name) and grants each one Contribute, Create branch and
Contribute to pull requests on the infra repo only. Both identities are
covered, the project one and the collection one, because which of the two
a run uses depends on a project setting. main's policies still apply to
everything they push.
The same change taught me the author policy's limits: the build service's "email" is its display name, so it could never pass a pattern. Its branch commits now use a fixed address the policy allows, and the pull requests are completed by a person rather than automatically.
What isn't solved yet
The Azure DevOps provider itself still signs in with a personal access token, kept in a pipeline variable group. That's the one place in my pipelines where a token still has to be rotated. Federation covers the Azure side, but not this provider yet.
Current state
Two projects and 48 repos, with their policies, groups, environments, service connections and variable groups, all applied through the pipeline. The projects themselves are looked up rather than created, so the organization's own settings stay outside this repo.