Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to create a pull request from another repo Github Action Workflows #3557

Closed
iamnothere101 opened this issue Dec 4, 2024 · 6 comments

Comments

@iamnothere101
Copy link

Subject of the issue

I have a CI setup where it will create an image and uploads it to the Docker Registry and checkout another GitHub repo and updates the kustomize file with the updated image tag. Then create a PR on the remote repo.

Steps to reproduce

Here is my workflow on the source repo:

  • name: Create Pull Request
    uses: peter-evans/create-pull-request@v4
    with:
    token: ${{ secrets.GITHUB_TOKEN }}
    repository: MyORG/MyPrivate_repo
    head: update-image-sha-${{ github.sha }}
    base: main
    title: Update image SHA to $IMAGE_SHA
    body: This PR updates the image SHA in the kustomization.yaml file to
    $IMAGE_SHA.

However it seems its treating it as child repo:

?? MyPrivate_repo/
Uncommitted changes found. Adding a commit.
/usr/bin/git add -A
warning: adding embedded git repository: MyPrivate_repo
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.

I guess how can I create PR from the source repo's workflow on another GitHub repo?

Cheers! 🙋🏻‍♂️ 🫡

@peter-evans
Copy link
Owner

@iamnothere101 Please show me the complete workflow. What you've posted is not enough to figure out what is happening.

Just some things I can see right off the bat:

  • There is no head input, I think that should be branch
  • When accessing a remote repository you cannot use the default GITHUB_TOKEN. Please see the documentation for details.

@iamnothere101
Copy link
Author

Hi @peter-evans , I really appreciate the reply here. Yes, I modified the "branch" from head and I am using a PAT_Token.

Here is the full workflow:

jobs:
build_and_trigger_job:
runs-on: ubuntu-20.04
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
submodules: true

  - name: Build and push to container registry
    uses: azure/docker-login@v1
  - run: >
      docker build . -t *******SHA*********

  - name: Checkout target repo
    uses: actions/checkout@v3
    with:
      repository: MyOrg/My_Destination_Repo

  - name: Commit and push changes
    run: >
      {{Run whole bunch of commands}}
      

  - name: Create Pull Request
    uses: peter-evans/create-pull-request@v4
    with:
        token: ${{ secrets.PAT_TOKEN }}
        repository: MyOrg/My_Destination_Repo
  	head-repo:: MyOrg/My_Destination_Repo
        branch: "based on SHA that gets created in the above step"
        base: main
        title: Update image SHA to env variable
        body: This PR suppose to fix the universe

In the job, it's trying to create the PR in the source repo (where this workflow resides). It suppose to create a PR on destination repo not on the source repo itself.
It's doing fine until creating the pull request portion.

@peter-evans
Copy link
Owner

Please follow the documentation here.

"Commit and push changes"
You haven't shown me what commands you are running here, but this is a red flag. Don't commit changes. Just make the change and let the action commit for you. Please look at the examples in the documentation. You should be checking out the base and making changes to files. You don't need to execute git commands.

Some other things:

  • Your checkout is missing the token
  • Use latest action versions for checkout and create-pull-request
  • The action has no head-repo input

@iamnothere101
Copy link
Author

Alright here you go:

I updated to use the latest actions version, I am using the PAT_TOKEN and using a head-repo input.
Please see below:

      - name: Checkout target repo
        uses: actions/checkout@v4
        with:
          repository: MyOrg/secondary_repo
          token: ${{ secrets.PAT_TOKEN }}
          path: secondary_repo_folder_name

      - name: Commit and push changes
        run: >
          set -e
          
          cd secondary_repo_folder_name

          git config --global user.name 'GitHub User Name'

          git config --global user.email 'GitHub User Email'

          git checkout -b {{branch-name variable}}
          
          cd path/of/kustomize/module || exit 1
          
          kustomize edit set image xxxxxxxxxxxxxxxxxxxx${IMAGE_SHA}

          git add kustomization.yaml

          git commit -m "Update image to ****SHA"

          git push origin {{ new branch name }}

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
            token: ${{ secrets.PAT_TOKEN }}
            repository: MyOrg/secondary_repo
            head-repo: MyOrg/primary_repo (source repo where this workflow resides)
            path: secondary_repo_folder_name
            branch: {{branch-name variable}}
            base: main
            title: Update image ****** $IMAGE_SHA
            body: This PR updates the image SHA in the kustomization.yaml file to
              $IMAGE_SHA.

The goal here is:

  1. Workflow in Source repo will pull the secondary_repo
  2. Update kustomization.yaml file
  3. Create new branch
  4. Do a git push
  5. Then create a PR in the secondary repo.

Its doing fine until step 5, However when I checked the logs, its trying to do a PR in the Source Repo, I even changed the head-repo to secondary repo and this step is still trying to do PR for the source repo instead of secondary repo.

And one more weird thing I noticed here is, if the if and only step 6 fails, then only its updating my new branch with an updates SHA ( I am super confused here with this logic :-) )

@peter-evans
Copy link
Owner

      - name: Checkout target repo
        uses: actions/checkout@v4
        with:
          repository: MyOrg/secondary_repo
          token: ${{ secrets.PAT_TOKEN }}
+         ref: main
-         path: secondary_repo_folder_name

-     - name: Commit and push changes
+     - name: Make changes
        run: >
          set -e

-         cd secondary_repo_folder_name

-         git config --global user.name 'GitHub User Name'

-         git config --global user.email 'GitHub User Email'

-         git checkout -b {{branch-name variable}}
          
          cd path/of/kustomize/module || exit 1
          
          kustomize edit set image xxxxxxxxxxxxxxxxxxxx${IMAGE_SHA}

-         git add kustomization.yaml

-         git commit -m "Update image to ****SHA"

-         git push origin {{ new branch name }}

      - name: Create Pull Request
-       uses: peter-evans/create-pull-request@v4
+       uses: peter-evans/create-pull-request@v7
        with:
            token: ${{ secrets.PAT_TOKEN }}
-           repository: MyOrg/secondary_repo
-           head-repo: MyOrg/primary_repo (source repo where this workflow resides)
-           path: secondary_repo_folder_name
            branch: {{branch-name variable}}
-           base: main
            title: Update image ****** $IMAGE_SHA
            body: This PR updates the image SHA in the kustomization.yaml file to
              $IMAGE_SHA.

@iamnothere101
Copy link
Author

Truly appreciate it @peter-evans . Your fix worked like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants