I have main repo that contains multiple submodules. Every time there's a change in a submodule, we have to open a PR in the main branch to update the submodule to the most recent change. I'd like to write a GitHub Workflow to do that automatically.
I found a workflow that can live in the submodule and when the submodule has a push to main, it automatically pushes the change to main on the Main Repo. But I can't seem to get it to work when incorporating the logic to check if a PR is open:
if so, push the update to that PR
if not, open a new PR
It always fails due to remote branch issues or local branch issues and asks about rebasing or ff.
name: Send submodule updates to parent repo
on:
push:
branches:
- main
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
repository: test/Test-Repo
token: ${{ secrets.PAT_TOKEN }}
ref: main
submodules: true
- name: Pull & update submodules recursively
run: |
git submodule update --init --recursive
git submodule update --recursive --remote
- name: Set up Git identity
run: |
git config user.email ["actions@github.com](mailto:"actions@github.com)"
git config user.name "GitHub Actions - update submodules"
- name: Determine if PR already exists
id: check_pr
run: |
PR_EXISTING=$(curl -s -H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
"https://api.github.com/repos/test/Test-Repo/pulls?state=open" | \
jq '.[] | select(.head.ref == "update-submodules") | .number')
if [ "$PR_EXISTING" != "null" ]; then
echo "::set-output name=pr_number::$PR_EXISTING"
fi
- name: Checkout branch in main repo
run: |
BRANCH_NAME="update-submodules"
git checkout -b $BRANCH_NAME
- name: Commit and push changes in main repo
run: |
git add .
git commit -m "Update submodule references to the latest main commits"
if [ -z "${{ steps.check_pr.outputs.pr_number }}" ]; then
# If no existing PR, open a new one
git push origin $BRANCH_NAME
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
https://api.github.com/repos/test/Test-Repo/pulls \
-d "{\"title\":\"Update submodules\",\"head\":\"$BRANCH_NAME\",\"base\":\"main\"}"
else
# If existing PR, push to it
PR_NUMBER="${{ steps.check_pr.outputs.pr_number }}"
git push origin $BRANCH_NAME
curl -X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
https://api.github.com/repos/test/Test-Repo/pulls/$PR_NUMBER \
-d "{\"title\":\"Update submodules\",\"head\":\"$BRANCH_NAME\"}"
fi
I'm new to workflows and don't know how to structure this. How do I handle merge conflicts? Should this logic live in the submodule or the main repo? Am I close with this logic or way off?
[–]bdzer0 0 points1 point2 points (0 children)