Search code examples
gitbitbucketbitbucket-pipelines

Can a Bitbucket pull request show the changes to a file generated by a pipeline


I am working on a Node.js-based project. I have an npm script that generates a text file report of the code (e.g. npm run scriptThatGeneratesReport generates a file report.txt, replacing the contents of any existing file with the same name).

Currently, when we are ready to create a pull request on a given branch, we run this script and commit the change to report.txt. Then when we create the pull request, the reviewers can see the change to this file along with the other code changes included in the branch as compared to the destination branch. However, we don't need this report file in the repository. We only commit it so we can see what changed while reviewing the pull request.

Is there a way to automate this step so we don't have to manually run scriptThatGeneratesReport and commit report.txt to the branch?

I can add the script to our Bitbucket pipeline so it gets run and the file generated with something like this:

image: node:18

definitions:
  steps:
    - step: &run-script
        name: Run script that generates output
        caches:
          - node
        script:
          - npm install
          - npm run scriptThatGeneratesReport

pipelines:
  pull-requests:
    "**":
      - step: *run-script
  branches:
    master:
      - step: *run-script

But I would need to also run it for the destination branch and somehow have Bitbucket show the changes as part of the pull request diff. Is there a way to do this?


Solution

  • By default, Bitbucket will discard any changes it did to the pulled code at the end of the pipeline, including the report you generate. To avoid this, you need to push changes back into your branch. Bitbucket has a detailed article on how you can do this:

    - git add report.txt
    - git commit -m "Commit message"
    - git push
    

    As for running your command on the destination branch, you will have to pull it to a separate folder. It might even be better to have your Bitbucket pipeline hand off the job to a sh script that would have far more scripting functionality than the yaml provided in Bitbucket.