Search code examples
bitbucketbitbucket-pipelines

prevent the bitbucket pipline from tirggering when bitbucket-pipelines.yml is updated


I am new to bitbuckt pipeline. To my node project I have added bitbucket-pipelines.yml in the pipeline I have a step to build and push container to ECR and another step to deploy.

Now each time I make a change to bitbucket-pipelines.yml it build and pushes a new image to ECR and deploys.

I do not what the piepline to trigger when I make changes to bitbucket-pipelines.yml. I only want the pipeline to trigger when I make changes to my application. Am I setting up the project wrong?

my project structure.

.
├── bitbucket-pipelines.yml
├── Dockerfile
├── index.js
├── node_modules
├── package.json
├── package-lock.json
└── README.md

Solution

  • There are a few possible options:

    1. Add [skip ci] to your git commit message

    Whenever you change the bitbucket-pipelines.yml on its own, add "[skip ci]" (without quotes) somewhere in your Git commit message. This will prevent the pipeline from running when you push to the Bitbucket remote.

    Sources and more info:

    Advantages:

    • It's easy and simple.

    Disadvantages:

    • You have to remember to manually write the "[skip ci]" text. It's easy to forget, or perhaps a new team member will not know about it.

    2. Use a Git Hook to automatically modify your git commit message

    Write a Git Hook script that will automatically insert the "[skip ci]" text into the Git commit message. The script will have to do something like this:

    1. After a local commit, check the latest commit to see which files were changed. Use something like git diff --name-only HEAD~0 HEAD~1
    2. If bitbucket-pipelines.yml was the only file changed, modify the commit to insert "[skip ci]" into the commit message.

    More info about Git Hooks:

    Advantages:

    • It's fully automatic. No need to manually tag your commit messages.

    Disadvantages:

    3. Make the bitbucket-pipelines.yml check for the file changes

    Add a section in the yml build script to check which file was changed in the latest commit.

    The script in the yml will have to do something like this:

    1. Check the latest commit to see which files were changed. Use something like git diff --name-only HEAD~0 HEAD~1
    2. If bitbucket-pipelines.yml was the only file changed, abort the CI build immediately, with an exit 0 statement.

    Advantages:

    • It's fully automatic. No need to manually tag your commit messages.
    • No need to write Git Hook scripts.

    Disadvantages:

    • The Docker image of your CI build will take 1-5 minutes to load, and then abort itself. This is a bit ineffecient and it will consume some of your build minutes.
    • Because the CI build will still run for a few minutes, it will pollute your CI build history with build runs that didn't do anything.

    4. Use a Conditional Step with "changesets" and "includePaths"

    Define a changesets with an includePaths to execute a step only if one of the modified files matches the expression in includePaths.

    pipelines:
      default:
        - step:
            name: build-frontend-artifact
            condition:
              changesets:
                includePaths:
                  # only xml files directly under resources directory
                  - "src/main/resources/*.xml"
                  # any changes in frontend directory
                  - "src/site/**"
            script:
              - echo "Building frontend artifact"
    

    Source and more info: