Skip to content

Instantly share code, notes, and snippets.

@abiydv
Created November 14, 2021 17:24
Show Gist options
  • Save abiydv/b432d444f10ce6c1ba5de926adf7c789 to your computer and use it in GitHub Desktop.
Save abiydv/b432d444f10ce6c1ba5de926adf7c789 to your computer and use it in GitHub Desktop.
Bitbucket Pipelines workaround for PR target branch checks
# Bitbucket pipelines currently do not offer a way to specify a condition
# to only trigger jobs based on the target branch.
#
# See this - https://jira.atlassian.com/browse/BCLOUD-17859
#
# You need to use '**' filter to trigger the pipeline on every PR, and
# then filter for your specific branch in the pipeline
#
# Another problem with Bitbucket pipelines is there is no ability to "reject"
# a pipeline execution, so a manual trigger step is not very helpful as a
# gate. But, that's a topic for another day.
#
image: abiydv/terragrunt:1.0.9
options:
max-time: 15
definitions:
scripts:
- script: &terraform-cmd
export TF_INPUT=0 TF_IN_AUTOMATION=1;
terraform init -input=false;
terraform validate;
terraform $TFCMD $TFCMD_OPT -input=false
steps:
- step: &pr-plan
name: pr-plan
script:
- TFCMD="init"; TFCMD_OPT=""
- >-
if [ $BITBUCKET_PR_DESTINATION_BRANCH == "develop" ]; then
cd /dev; TFCMD="plan"; TFCMD_OPT="-var-file=dev.tfvars";
elif [ $BITBUCKET_PR_DESTINATION_BRANCH == "main" ]; then
cd /prod; TFCMD="plan"; TFCMD_OPT="-var-file=prod.tfvars";
else
echo "Ignoring PR, not generating plan";
fi
- *terraform-cmd
- step: &prod-apply
name: prod-apply
deployment: production
script:
- cd /prod
- TFCMD="apply"; TFCMD_OPT="-var-file=prod.tfvars -auto-approve"
- *terraform-cmd
- step: &dev-apply
name: dev-apply
deployment: test
script:
- cd /dev
- TFCMD="apply"; TFCMD_OPT="-var-file=dev.tfvars -auto-approve"
- *terraform-cmd
pipelines:
branches:
main:
- step: *prod-apply
develop:
- step: *dev-apply
pull-requests:
'**':
- step: *pr-plan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment