Task 04: Create Environments
DOCUMENT CATEGORY: Runbook Step
SCOPE: Deployment environment creation
PURPOSE: Create deployment environments for infrastructure automation MASTER REFERENCE: Azure Pipelines Environments
Status: Active
Objective
Create deployment environments to manage infrastructure using IaC and automation. Environments provide deployment targets with optional protection rules and approval gates.
Prerequisites
- Project settings configured (Task 03)
- Admin / Maintainer role on the project
Environment Naming Convention
Use consistent naming across all platforms:
| Cloud/Service | Example Names |
|---|---|
| Azure Production | azr-prod-us, azr-prod-eu |
| Azure Development | azr-dev-us |
| Azure Staging | azr-staging |
Procedure
- GitHub
- GitLab
- Azure DevOps
Create Environments
- Navigate to Settings → Environments
- Click New environment
- Enter the environment name (e.g.,
azr-prod-us) - Click Configure environment
Configure Protection Rules
For production environments, add protection rules:
| Setting | Value |
|---|---|
| Required reviewers | ✅ Add 1–2 required reviewers |
| Wait timer | Optional — add delay before deployment (minutes) |
| Deployment branches | main only (select Selected branches → add main) |
Repeat for each environment in the naming convention table above.
GitHub Environments integrate with GitHub Actions via the environment: key in workflow YAML. Protection rules automatically gate deployments.
References: GitHub — Using environments for deployment
Create Environments
- Navigate to the project in GitLab
- Select Operate → Environments
- Click New Environment
- Enter the environment name (e.g.,
azr-prod-us) - Leave External URL blank
- Click Save
Repeat for each environment in the naming convention table above.
Configure Protection (Optional)
For production environments:
- Navigate to Settings → CI/CD → Protected Environments
- Select the environment
- Set Allowed to deploy to the appropriate role or group
GitLab Environments are referenced in .gitlab-ci.yml via the environment: keyword. Protected environments require approval before deployment jobs run.
References: GitLab — Environments and deployments
Create Environments
- Navigate to Pipelines → Environments
- Click New environment
- Enter the environment name (e.g.,
azr-prod-us) - Select None for Resource (environments are used as deployment targets)
- Click Create
Repeat for each environment in the naming convention table above.
Configure Approvals and Checks
For production environments:
- Open the environment → click ⋮ (more options) → Approvals and checks
- Add Approvals:
- Add 1–2 required approvers
- Set timeout (e.g., 72 hours)
- Optionally add Branch control:
- Allowed branches:
refs/heads/main
- Allowed branches:
Azure DevOps Environments are referenced in YAML pipelines via the environment: key in deployment jobs. Approvals and checks automatically gate deployments.
The toolkit's Azure DevOps pipelines read the environment names from pipelines/azure-devops/variables/approval-environments.yml when they are compiled, not from a run parameter. The person who queues a run cannot choose an environment without approvals. Task 05 lists the name to write there for each pipeline. Add the Approvals check to each environment before the first run: a deployment job in an environment with no check runs without an approval.
Also add a Branch control check to each environment, and to the service connection and agent pool the pipelines use: allowed branches refs/heads/<your default branch>, with "Verify branch protection" on. The run form lets the person who queues a run pick a branch, and the pipeline is compiled from that branch's files, so without this check a pushed branch could name another environment. A branch cannot change a check, because checks are set in Azure DevOps, not in YAML. See Approvals and checks.
References: Azure DevOps — Create and target an environment
Verification
- Environment created for each deployment target
- Environment names follow naming convention
- Production environments have protection rules / approval gates configured
Next Steps
Configure Environment Variables
References
- GitHub — Using environments for deployment
- GitLab — Environments and deployments
- Azure DevOps — Create and target an environment
Values from infrastructure.yml
The script reads devops.environments[] (name, approval required, approvers, wait minutes, deployment branch) of infrastructure.yml (schema 5). Each key, with its type, default and example, is in the variables reference.
The pipeline files of the toolkit name no environment. devops.pipelines.environments (schema 5.6.25) says which entry of devops.environments[] gates which pipeline: os_configuration, os_configuration_clear_storage, arm_deployment, cicd_infra, terraform_deployment and operations. Each value must name an entry with approval_required: true. Task 05 turns them into pipeline variables on GitHub and GitLab. On Azure DevOps it lists the value of each one to write in pipelines/azure-devops/variables/approval-environments.yml, once, in a pull request.
devops.pipelines.environments.operations (schema 5.6.28, the variable AZL_ENVIRONMENT_OPERATIONS) is the one environment of every validation and day 2 job that changes the cluster or puts load on it: the update, the node maintenance, the changing cluster operations and the storage load test. It is one environment on purpose. On Azure DevOps its Exclusive lock check then keeps an update and a node reboot from running on the cluster at the same time. Give it the approvers of your change process, and on Azure DevOps add the Approvals, Exclusive lock and Branch control checks. See Validation and day 2 pipelines.
While an AZL_ENVIRONMENT_* value is empty, no pipeline runs a change without an approval. A refusal fails with a message that names the variable: the job approval-environment-not-set on GitHub, the job <pipeline>-approval-environment-not-set on GitLab, the stage ApprovalEnvironmentNotSet on Azure DevOps. Nothing is sent to any machine.
The toolkit script for this task is PowerShell 7. It reads infrastructure.yml through the mandatory -InfrastructureFile parameter and never prompts. It ends with exit code 0, 1 or 2. Run it with -WhatIf first: the script reads what exists and lists what it would do.
Script
| Script | What it does |
|---|---|
scripts/deploy/01-cicd-infra/phase-01-cicd-setup/task-04-create-environments/powershell/New-CicdEnvironments.ps1 | Task 04. Creates the deployment environments with their approval gates. Supports -WhatIf. |
Requires: PowerShell 7.
Run
./New-CicdEnvironments.ps1 -InfrastructureFile ./infrastructure.yml -WhatIf
Alternatives
The procedures in this task use the methods shown in the tabs above. The toolkit is PowerShell 7 only and has no Bash scripts. A few tasks that act on Azure also have an Azure CLI script written in PowerShell. The script status page of the toolkit lists which task has which script.
Navigation
| Previous | Up | Next |
|---|---|---|
| ← Task 03: Configure Project Settings | Phase 01: CI/CD Setup | Task 05: Configure Environment Variables -> |
Troubleshooting
| Issue | Cause | Resolution |
|---|---|---|
| Environment creation fails | Insufficient permissions | Verify user has environment creation rights |
| Approval gates not configuring | Environment policy restrictions | Check organization-level environment policies |
Version Control
| Version | Date | Author | Changes |
|---|---|---|---|
| 1.0.0 | 2025-03-25 | Azure Local Cloud | Initial release |