Set Up GitOps Workflow
Implement a PR-based infrastructure workflow where all changes go through code review, automatic plans appear as comments, and merging triggers deployment.
Scenario
Your team wants to:
- Require code review for all infrastructure changes
- See Terraform plans before approving PRs
- Automatically deploy when PRs are merged
- Have a complete audit trail in Git history
This guide sets up a complete GitOps workflow with GitHub integration.
Prerequisites
Step 1: Install ops0 GitHub App
Permissions Requested
| Permission | Purpose |
|---|---|
| Read code | Clone repositories |
| Write pull requests | Post plan comments |
| Write commit status | Update check status |
| Read/write webhooks | Receive PR events |
Step 2: Connect Your IaC Project to a Repository
Branch Configuration
| Setting | Recommended Value | Purpose |
|---|---|---|
| Default Branch | main | Where production code lives |
| Working Directory | / or /terraform | Path to Terraform files in repo |
| Auto-sync | Enabled | Pull changes from GitHub automatically |
Step 3: Initial Sync
If your ops0 project already has code, push it to GitHub:
main)If GitHub already has code, click Pull from GitHub instead.
Step 4: Configure PR Automation
Set up what happens when PRs are created:
Automation Options
terraform plan automatically when a PR is opened or updated.terraform apply when PR is merged. Enable with caution.Step 5: Set Up Branch Protection (GitHub)
To enforce the GitOps workflow, configure GitHub branch protection:
main branchRecommended Settings
| Setting | Value |
|---|---|
| Require pull request before merging | Yes |
| Required approvals | 1 (or more for production) |
| Require status checks | Yes |
| Required checks | ops0/terraform-plan, ops0/policy-check |
| Require branches to be up to date | Yes |
Step 6: Test the Workflow
Let's verify everything works:
Create a Test PR
mainWhat You Should See
Within 30-60 seconds:
✓ ops0/terraform-plan - Plan succeeded: 0 to add, 1 to change, 0 to destroy
✓ ops0/policy-check - All policies passed
### Terraform Plan
Changes: 0 to add, 1 to change, 0 to destroy
Cost Impact: +$0.00/month
~ aws_instance.web_server
~ tags.Environment: "dev" -> "development"
View full plan in ops0Step 7: Configure Auto-Apply (Optional)
If you want changes to deploy automatically when PRs are merged:
main)With Approval Workflow
For production environments, add a human checkpoint:
PR Merged
│
▼
Auto-plan runs
│
▼
Approval request sent (Slack/Email)
│
▼
Approver clicks "Approve" in ops0
│
▼
Terraform apply executes
│
▼
Result posted to PR/Slack
See Create Approval Workflows for setup details.
Daily Workflow
Once set up, here's how your team works:
Troubleshooting
Plan Not Running on PR
| Check | Solution |
|---|---|
| GitHub App installed? | Verify in Settings > Integrations |
| Correct repository? | Confirm repo is connected in Project Settings |
| Webhooks working? | Check GitHub repo > Settings > Webhooks for delivery status |
| Working directory correct? | Ensure path matches where Terraform files are |
Status Checks Not Appearing
- Ensure "Update commit status" is enabled
- Check that branch protection is configured correctly
- Verify the ops0 check names match exactly
Merge Conflicts
If ops0 and GitHub have diverged:
- Pull latest from GitHub in ops0
- Resolve conflicts in the editor
- Push merged result back