Azure DNS记录变更时触发Bitbucket Pipeline的解决方案及实现流程咨询
Hey there! Let me break down exactly how to set up an instant trigger for your Bitbucket Pipeline whenever your Azure DNS records change. This setup uses Azure's event-driven tools to connect the dots seamlessly. Here's your step-by-step guide:
We'll leverage Azure Event Grid to monitor changes to your DNS zone, then use Azure Logic Apps (a low-code, easy-to-configure option) to forward those events to Bitbucket's Pipeline API, triggering your pipeline automatically.
1. Prerequisites
- An active Azure subscription with permissions to manage DNS zones, Event Grid, and Logic Apps
- A Bitbucket repository where you can configure pipelines and generate trigger tokens
2. Set Up Azure Event Grid Subscription to Monitor DNS Changes
First, we need to tell Azure to watch for DNS record updates or deletions:
- Log into the Azure Portal, navigate to your DNS zone resource
- Go to the
Eventstab in the left menu, then click+ Event Subscription - Name your subscription something descriptive, like
DNS-Changes-Bitbucket-Trigger - Under
Event Types, check the boxes forMicrosoft.Network.DNSZone.RecordSetUpdatedandMicrosoft.Network.DNSZone.RecordSetDeleted(pick whichever events you want to trigger on) - For
Endpoint Type, selectLogic App, then clickSelect an endpointto either create a new Logic App or use an existing one (I recommend a new one for simplicity)
3. Configure Azure Logic App to Forward Events to Bitbucket
Now we'll build the Logic App workflow to send the trigger request to Bitbucket:
- Open your newly created Logic App and go to the Designer
- Use the
When an Event Grid event is receivedtrigger, and link it to the Event Grid subscription you just made - Add an
HTTPaction to the workflow, and configure these details:- Method:
POST - URI: Bitbucket's pipeline trigger endpoint, formatted as
https://api.bitbucket.org/2.0/repositories/{your-workspace}/{your-repo-name}/pipelines/ - Headers:
- Add
Authorizationwith the valueBearer {your-bitbucket-trigger-token}(we'll generate this next) - Add
Content-Typewith the valueapplication/json
- Add
- Body: Use this JSON structure to trigger your desired pipeline (adjust the branch or custom target as needed):
{ "target": { "ref_type": "branch", "type": "pipeline_ref_target", "ref_name": "main" // Replace with your target branch name } }
- Method:
- Save the Logic App to activate the workflow
4. Generate a Bitbucket Pipeline Trigger Token
Bitbucket requires a token to authenticate API requests that trigger pipelines:
- Log into Bitbucket, go to your repository, then click
Settingsin the left sidebar - Under
Pipelines, selectTrigger Tokens - Click
Generate token, name it something likeAzure-DNS-Event-Trigger, then clickCreate - Copy the generated token immediately (it won't be shown again!)
5. Customize Your Bitbucket Pipeline (Optional)
If you want to run specific actions when the DNS change triggers the pipeline, update your bitbucket-pipelines.yml file. For example:
pipelines: default: - step: name: Respond to DNS Change script: - echo "Detected a change to Azure DNS records!" # Add your custom commands here (e.g., sync configs, send alerts, run tests) # If you want a dedicated custom pipeline for DNS triggers custom: dns-trigger: - step: name: DNS Change Handler script: - echo "Running DNS-specific pipeline steps..."
If using a custom pipeline, update the Logic App's request body to target it:
{ "target": { "type": "pipeline_custom_target", "selector": "dns-trigger" } }
6. Test the Setup
- Make a small change to your Azure DNS zone (e.g., edit an A record or add a CNAME)
- Head over to your Bitbucket repository's
Pipelinestab—you should see a new pipeline run start within a minute or two - Check your Logic App's
Run Historyto confirm the HTTP request was sent successfully (if there's an error, double-check the token, URI, and request body)
Key Notes
- Ensure your Logic App has outbound network access to Bitbucket's API (if using VNet-integrated Logic Apps, configure appropriate network rules)
- Keep your Bitbucket trigger token secure—rotate it periodically and never commit it to code
- You can add filters to your Event Grid subscription to only trigger on specific record types (e.g., A, CNAME) if you don't want every DNS change to start a pipeline
- If you prefer code over low-code, you can replace Logic Apps with an Azure Function (write code in C#, Python, etc.) to handle the Event Grid event and call the Bitbucket API
内容的提问来源于stack exchange,提问作者Abhinav Anand

