Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bitbucket pipelines - Run or skip step based on Condition and Schedule

Abdul Rehman June 17, 2022

Scheduled Pipeline:
I'm looking for a solution in which we need to run the pipeline at a Scheduled time, like after every 8 hours.
image.png

Condition:
On the other hand, we need to skip the pipelines if there is no change in code, either by commit or by PR Merged.
But if the pipeline got kicked through Scheduled Time even if there is a Condition to skip pipelines without any change. Pipelines shouldn't skip it should run the CI process without skipping any steps

- step:

            name: 1X-Chrome

            condition:

              changesets:

                  includePaths:

                    # Any changes in Cypress Directories

                    - "fixture/**"                    

                    - "integration/**"

                    - "plugins/**"

                    - "support/**"

                    - "videos/**"

            <<: *step-chrome

            deployment: prod

With the above configuration as there is a condition to run only there is change in the mentioned paths. Because of that our Scheduled Jobs are also skips the pipeline step due to condition in it. 
Is there anyway we can add some sort of rules in which we will use both options like once the pipeline run with Schedule Job pipelines avoids the condition and runs the pipeline including all the step even with the condition steps. And when the pipeline run with the commit or PR Merged if there is no change in the conditon includePaths, pipeline skips the step  

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 20, 2022

Hello @Abdul Rehman ,

Thank you for reaching out to Atlassian Community!

I'm afraid that is not possible to configure a rule to have the same pipeline skip/not skip a step based on is it was triggered by a schedule or a commit.

However to achieve the behavior you are looking for, I think you could create an additional custom triggered pipeline that you can use to configure your schedule, and it can contain the same steps as the pipeline you run for your commits/PRs, just excluding the condition includePaths.

Please find the example YML below : 

definitions: 
  steps:
    - step: &my-step-anchor
        name: Build and test
        script:
          - echo "this is my step's script"

pipelines:
  default: # This pipeline will automatically be triggered for any commit to a branch not elsewhere defined
    - step:
          <<: *my-step-anchor
          condition:
              changesets:
                 includePaths:
                   - "fixture/**"                    
                   - "integration/**"
                   - "plugins/**"
                   - "support/**"
                   - "videos/**"
  custom: 
   customPipelineForSchedule: # This custom pipeline is to be used in your schedule pipelines
      - step: *my-step-anchor

So in the above example, I created a YML anchor for the step, so you can reference it multiple times in your YML code without having to copy/paste the same code multiple times.
The 'default' pipelines will be triggered when there's any commit to any branch that is not elsewhere defined in your YML file. We are essentially getting the step defined in the anchor &my-step-anchor, and overriding the condition value of the step to check for the changes in the paths defined. If in the commit no file was modified in any of those paths, pipelines will skip this step.

As for the custom pipeline called customPipelineForSchedule, as the name says, it will be used to create your schedule pipeline. It is essentially the same step we have in the 'default' pipeline, but without the condition includePaths. So this is the pipeline you will choose when setting up your 8 hours schedule under the Pipelines page. Since this pipeline has no condition, it will run the step regardless of the path of the changed files.

Hope that helps to address your questions! Let me know in case you have any doubt.

Thank you, @Abdul Rehman .

Kind regards,

Patrik S

Abdul Rehman June 21, 2022

@Patrik S Thank you so much for your response, that's great we can create custom pipelines which we can use to schedule pipeline on main branch and that working fine. But I'm getting another issue even if there is a change in the directories which are mentioned in the condition section it still stops the pipeline and skips the pipeline. Can you please guide me on this as well why this condition is unable to detect the changes which I'm doing in the files in those directories 
This the pipeline which I have created on main branch




main
:

      - step:
          name: Preparing Dependencies
          caches:
            - npm
            - node
            - cypress
          script:
            - npx @bahmutov/print-env BITBUCKET
            - npm ci
      - parallel:
        - step:
            name: 1X-Chrome
            # Condition to stop pipeline if there are no changes in code
            condition:
                changesets:
                    includePaths:
                        # Any changes in Cypress Directories
                        - "fixtures/*.json"                    
                        - "integration/**/**/**/*.js"
                        - "plugins/*.js"
                        - "support/**/**/*.js"
                        # - "videos/**"
            <<: *step-chrome

 

            deployment: prod
         
        - step:
            name: 1X-Firefox
            # Condition to stop pipeline if there are no changes in code
            condition:
                changesets:
                    includePaths:
                        # Any changes in Cypress Directories
                        - "fixtures/*.json"                    
                        - "integration/**/**/**/*.js"
                        - "plugins/*.js"
                        - "support/**/**/*.js"
                        # - "videos/**"
            <<: *step-firefox
        - step:
            name: 1X-Edge
             # Condition to stop pipeline if there are no changes in code
            condition:
                changesets:
                    includePaths:
                        # Any changes in Cypress Directories
                        - "fixtures/*.json"                    
                        - "integration/**/**/**/*.js"
                        - "plugins/*.js"
                        - "support/**/**/*.js"
                        # - "videos/**"
            <<: *step-edge
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 22, 2022

Hey @Abdul Rehman ,

Could you please give us an example of the full path of a file in your repository that was changed and was supposed to run the step with the condition, but it skipped the pipeline instead? 

This way I can check if there's an issue/missconfiguration with your current conditions that have not considered that particular file.

Kind regards,

Patrik S

Like Abdul Rehman likes this
Abdul Rehman June 22, 2022

@Patrik S Thanks you for your respond here is my file-structure which I'm trying to apply condition on

Inside my repo named cypress_automation on bitbucket


cypress/fixtures/file.json

cypress/integration/folder-1/file.js

cypress/integration/folder-2/sub-folder2/file.js

cypress/plugin/file.js

cypress/support/folder1/sub-folder1/file.js

cypress/support/folder1/file.js

cypress/support/file.js

cypress/videos/folder1/file.mp4 nevermind about this folder I will remove this from the bitbucket-pipelines.yml file

Abdul Rehman June 23, 2022

I got my solution I was giving wrong path the correct path was above which I have mentioned already but I was not adding that to my file lolol

Before:

condition:

              changesets:

                  includePaths:

                    # Any changes in Cypress Directories

                    - "fixture/**"   ❌                  

                    - "integration/**" ❌

                    - "plugins/**" ❌

                    - "support/**" ❌

                    - "videos/**" ❌


After:

condition:

            changesets:

                includePaths:

                    # Any changes in Cypress Directories

                    - cypress/fixtures/** ✔️

                    - cypress/integration/** ✔️

                    - cypress/plugins/** ✔️

                    - cypress/support/** ✔️

Thanks @Patrik S  for helping

Like Patrik S likes this
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 24, 2022

Hey @Abdul Rehman ,

You're very welcome!

Happy to hear that it worked and thanks for sharing the solution!

Best regards,

Patrik S

Like Abdul Rehman likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events