DEV Community

Discussion on: Fetch several branches locally in GitHub actions

Collapse
 
cicirello profile image
Vincent A. Cicirello • Edited

You can actually use the checkout action multiple times in the same workflow. Here are a couple examples. In this workflow the first checkout is for the default branch, but then the second uses checkout for a branch named badges where I store coverage badges. In that one you'll notice the path input to the checkout action allows specifying a directory for the result of the checkout.

In this second example, the first checkout is again the default branch. But the second is for the gh-pages branch so the workflow can deploy documentation to GitHub Pages served from that branch.

In both of those examples I'm nesting the second checkout inside the other. The docs for the checkout action also explain how to do the same thing but with the branches side by side in different directories rather than nested, if you'd prefer that.

Collapse
 
martinratinaud profile image
Martin Ratinaud

Thanks a lot for your answer

I have tested this using

- uses: actions/checkout@v2
   with:
     ref: main
- uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

Which from what I understand should fetch main and then my current branch but as you can see here, it does not work as main branch is not present in the repo.

Or am I mistaking ?

Collapse
 
cicirello profile image
Vincent A. Cicirello

I reread your post for the reason you are checking out multiple branches, to do a git diff on a file. You might need to use your current approach for that. My prior suggestion works well for a case where your workflow uses contents of one branch to produce contents for another (e.g., running javadoc on sourcecode from main branch but pushing the html generated by javadoc to gh-pages branch instead of back to main).

But I now don't think there is a way to adapt to your use-case. Without using the path input to the checkout action, the second checkout will clobber the first. I believe it will be as if you only did the second one. And checking out each branch to different paths probably won't help you do what you want to do.

Thread Thread
 
martinratinaud profile image
Martin Ratinaud

Indeed

I tweaked a bit my solution as it was not working in all cases but now it does.

Thanks for your reply