This repository contains a GitHub Actions workflow for managing Cloudflare resources using Terraform in a GitOps pattern.
The workflow (terraform-cloudflare.yml
) provides automated Terraform operations with the following features:
- Pull Requests: Validates and plans changes
- Push to main: Applies changes automatically
- Manual Dispatch: Allows manual plan/apply/destroy operations
- Runs on all triggers
- Performs format checking, initialization, and validation
- Outputs results for other jobs
- Runs on pull requests and manual dispatch
- Creates execution plan
- Posts plan results as PR comments
- Uploads plan artifact for later use
- Runs on push to main or manual apply
- Requires production environment approval
- Uses saved plan from PR when available
- Creates deployment summary
- Only runs on manual dispatch with destroy action
- Requires production environment approval
- Creates destruction summary
- Runs Checkov security scanning
- Uploads results to GitHub Advanced Security
Add these secrets to your GitHub repository:
# Repository Settings > Secrets and variables > Actions
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token
Set up a production
environment in your repository:
- Go to Settings > Environments
- Create a new environment called
production
- Configure protection rules:
- Required reviewers (recommended)
- Deployment branches (main only)
- Environment secrets (if needed)
Configure branch protection for main
:
- Go to Settings > Branches
- Add rule for
main
branch - Enable:
- Require pull request reviews
- Require status checks to pass
- Require branches to be up to date
- Include administrators
-
Create Feature Branch
git checkout -b feature/my-changes
-
Make Terraform Changes
- Edit
.tf
files - Test locally if needed
- Edit
-
Create Pull Request
- Workflow runs validation and planning
- Plan results are posted as PR comment
- Security scan runs automatically
-
Review and Merge
- Review the plan in PR comments
- Merge to main when approved
-
Automatic Deployment
- Push to main triggers automatic apply
- Requires production environment approval
# Go to Actions tab > Terraform Cloudflare GitOps > Run workflow
# Select "plan" action
# Go to Actions tab > Terraform Cloudflare GitOps > Run workflow
# Select "apply" action
# Go to Actions tab > Terraform Cloudflare GitOps > Run workflow
# Select "destroy" action
The workflow uses the Cloudflare API token for authentication. The token is configured in multiple ways:
- Environment Variable:
CLOUDFLARE_API_TOKEN
- Terraform Variable:
TF_VAR_cloudflare_api_token
- Go to Cloudflare Dashboard
- Click "Create Token"
- Use "Custom Token" template
- Configure permissions:
- Account:
Cloudflare Tunnel:Edit
- Zone:
Zone:Edit
,DNS:Edit
- Include: All accounts or specific account
- Account:
- Copy the token and add to GitHub secrets
- Scans Terraform code for security misconfigurations
- Results uploaded to GitHub Advanced Security
- Soft fail mode (won't block deployment)
- Production environment requires approval
- Limits who can deploy to production
- Audit trail of all deployments
- Workflow only has necessary permissions
- API tokens scoped to required resources
- View detailed logs in Actions tab
- Each step provides specific output
- Terraform plans and applies show full output
-
Authentication Errors
- Verify
CLOUDFLARE_API_TOKEN
secret is set - Check token permissions and expiration
- Verify
-
Terraform State Issues
- Ensure state file is properly configured
- Check for state locking conflicts
-
Plan/Apply Mismatches
- May occur if changes happen between plan and apply
- Manual apply will create new plan
- Small Changes: Make incremental changes for easier review
- Test Locally: Use
terraform plan
locally before pushing - Review Plans: Always review plan output in PR comments
- Monitor Deployments: Watch deployment logs for issues
- Regular Updates: Keep Terraform and providers updated
Update the TF_VERSION
environment variable in the workflow file.
Modify TF_WORKING_DIR
if your Terraform files are in a different location.
Adjust the on
section to change when the workflow runs.
Configure Checkov rules by adding a .checkov.yml
file to your repository.
For issues with this workflow:
- Check the Actions logs for specific error messages
- Verify all secrets and environment settings
- Review Terraform and Cloudflare documentation
- Check GitHub Actions documentation for workflow syntax