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

Backport workflow: Use access token to run CI on PRs? #9554

Closed
The-Compiler opened this issue Jan 27, 2022 · 14 comments · Fixed by #10346 or #12475
Closed

Backport workflow: Use access token to run CI on PRs? #9554

The-Compiler opened this issue Jan 27, 2022 · 14 comments · Fixed by #10346 or #12475
Labels
type: infrastructure improvement to development/releases/CI structure

Comments

@The-Compiler
Copy link
Member

Regarding CI not running, right - CI doesn't run when triggered this way - this also happens for the create-pull-request actions and similar. Workaround is to close & reopen, then it triggers. It's also possible to use a user token (pytestbot), but that has its own security considerations.

Originally posted by @bluetech in #9430 (comment)

@The-Compiler The-Compiler added the type: infrastructure improvement to development/releases/CI structure label Jan 27, 2022
@webknjaz
Copy link
Member

webknjaz commented Feb 5, 2022

It's also possible to use a user token (pytestbot), but that has its own security considerations.

Instead of using a PAT, I'd recommend making a GitHub App and provisioning its credentials to the workflow via https://github.com/marketplace/actions/github-app-token + use environments feature in workflows so that the secrets are not global.

Another hack I've been using is adding a deployment key to the repo + adding its private key as a secret. Then, in the workflows, add ssh-key to actions/checkout@v2 with that. Then, any subsequent Git interactions in the job will be configured to use that SSH key, enabling the push event propagation (for triggering CI / notifying other listeners).

@The-Compiler
Copy link
Member Author

FWIW this seems like a good overview of possibilities (some of which @webknjaz has already mentioned): https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#workarounds-to-trigger-further-workflow-runs

@webknjaz
Copy link
Member

webknjaz commented Aug 1, 2022

Hey @The-Compiler, have I offered you to use my Patchback GitHub App? I don't remember if I did, but it so happened that a number of projects are using it successfully (some of which I didn't even know existed).
So if you're up to trying it out, it's here https://github.com/apps/patchback. Here's an example config https://github.com/ansible-collections/community.general/blob/main/.github/patchback.yml but it'll work just fine even without one. Example PR: aio-libs/aiohttp#6749 (comment).

@bluetech
Copy link
Member

bluetech commented Oct 6, 2022

Another hack I've been using is adding a deployment key to the repo + adding its private key as a secret. Then, in the workflows, add ssh-key to actions/checkout@v2 with that. Then, any subsequent Git interactions in the job will be configured to use that SSH key, enabling the push event propagation (for triggering CI / notifying other listeners).

I can easily do this one -- I will generate the key pair, add the private key as a secret and throw it away. Any other core dev wants to ack this (or add a key directly given my ack)?

@nicoddemus
Copy link
Member

nicoddemus commented Oct 6, 2022

@bluetech please go ahead! Thanks!

bluetech added a commit to bluetech/pytest that referenced this issue Oct 6, 2022
…cess token

The builtin actions token is not allowed to trigger further workflows.
So when we create a PR using that token, the PR checks don't run, and a
maintainer must close & open the PR to make them run. This is quite
annoying, so switch back to the "machine user" approach.

Fixes pytest-dev#9554.
@bluetech
Copy link
Member

bluetech commented Oct 6, 2022

I realized after the fact what @webknjaz and the peter-evans/create-pr docs say, that a deploy key will only trigger push event but not pull-request event which is what we need. So I created a PR to use @pytestbot key instead (secret already configured).

@webknjaz
Copy link
Member

webknjaz commented Oct 6, 2022

I realized after the fact what @webknjaz and the peter-evans/create-pr docs say, that a deploy key will only trigger push event but not pull-request event which is what we need.

That's not really accurate. If there is a PR for the given branch, the pull_request event will happen too (and will be processed unless GHA workflow is set up to ignore the branch name pattern, for example). This has nothing to do with the deployment key. The problems with non-propagating events are only there if you use ${{ secrets.GITHUB_TOKEN }}. Any other tokens will cause normal events to happen.

@webknjaz
Copy link
Member

webknjaz commented Oct 6, 2022

@bluetech I looked at your PR #10346, and now I see why you've drawn this connection. This is because both PR creation and pushing to Git used the same token. I've done one clever thing in one of my projects — the workflow first pushes an empty commit (push to a new branch happens), then a PR is created using a normal ${{ secrets.GITHUB_TOKEN }} (because there's a branch that exists, it's possible — but this doesn't cause any PR-related events); then, the proper commits are force-pushed to that branch using a deployment key. And this time, both push and pull_request events happen because ${{ secrets.GITHUB_TOKEN }} is not in use. And so this allows the CI to be triggered.

@bluetech
Copy link
Member

bluetech commented Oct 7, 2022

Reopening, see #10354...

@bluetech bluetech reopened this Oct 7, 2022
@bluetech
Copy link
Member

bluetech commented Oct 7, 2022

@webknjaz Heh that's indeed clever. We can try this if nothing else works. Slight downside is that it will create extra notifications for the last push, but that's not too bad.

@webknjaz
Copy link
Member

webknjaz commented Oct 8, 2022

@bluetech have you considered just giving my bot a try instead? You can install Patchback on any test repo under your user account and see if it works for you, no need to experiment on this upstream repo. I have already put a lot of thought into it so you don't have to reinvent the wheel if there's no need.

But if you want to stick with GHA, I'd recommend creating a GitHub App entity under this org and provisioning it's secrets to GHA, scoped to an environment. That would provide you with the most pleasing experience while still allowing you to use GHA is the main runtime (you'd get an API token to use via something like https://github.com/marketplace/actions/app-token).

@nicoddemus
Copy link
Member

nicoddemus commented Oct 9, 2022

@bluetech have you considered just giving my bot a try instead?

Out of curiosity I did take a look at the bot. Seems interesting, is there any documentation on how to use it?

I dug around aio-libs (because it is advertised in the bot's page as a project which uses it) and found aio-libs/aiohttp#6993, which has a backport-3.9 label, and indeed the bot posted a comment and created aio-libs/aiohttp#6994.

Is it a matter of just installing the bot and creating labels in the format backport-<BRANCH>?

@webknjaz
Copy link
Member

webknjaz commented Oct 19, 2022

@nicoddemus sorry, I missed your comment.

Out of curiosity I did take a look at the bot. Seems interesting, is there any documentation on how to use it?

Oh, my... Sorry about that! The project kinda grew naturally and took weird turns. First, I had an idea of doing something like this + had some experiments with writing GitHub Apps. I thought I'd use it for aiohttp. Then, I didn't actually start the project. And, at some point, folks maintaining the community Ansible Collections were complaining about the cherry-picking process being annoying. So I remembered my idea and sketched the bot later that evening. I was testing it on those collection repositories, which is why I've just added the config there as it evolved. And later people were just copying it around so I completely forgot about the documentation.
Projects I never knew existed somehow learned about the bot and started copying the config or just using it with defaults. Nobody ever asked me to document stuff!
But yeah, it's a valid point. I have some issues filed for myself but not about the initial install/setup docs... I guess I didn't foresee it going viral 🤷‍♂️

Is it a matter of just installing the bot and creating labels in the format backport-<BRANCH>?

And yes. You just install it (maybe in your fork or a test repo for testing) and create labels (I'd probably prefer to just auto-create them but never got time to work on such a feature).
Also, you could copy-paste + edit https://github.com/ansible-collections/community.general/blob/main/.github/patchback.yml#L6-L8 if you want custom names for the labels, target branches or the branches that Patchback creates for PRs. I did mention the configuration once at #9555 (comment). That comment also has a link to a failed attempt of an automatic cherry-pick with instructions for the contributors on how to do manual backporting.

P.S. You might want also want to drop in a config for https://github.com/python/cherry-picker — it's originally written for CPython, but then it got integrated into aiohttp and Ansible. I don't use it anymore personally (although, I've contributed a lot at some point) but I know that many people do, and having a config facilitates this. This is an assistive tool specifically for creating backports from the dev machine.

@webknjaz
Copy link
Member

webknjaz commented Mar 9, 2023

@nicoddemus hey, do you want to revisit this?

webknjaz added a commit to webknjaz/pytest that referenced this issue Jun 18, 2024
This patch prepares the project's backporting process to start being
handled by the Patchback GitHub App [[1]].

Ref pytest-dev#9384
Resolves pytest-dev#9385
Resolves pytest-dev#9553
Resolves pytest-dev#9554
Resolves pytest-dev#9555

[1]: https://github.com/apps/patchback
patchback bot pushed a commit that referenced this issue Jun 19, 2024
This patch prepares the project's backporting process to start being
handled by the Patchback GitHub App [[1]].

Ref #9384
Resolves #9385
Resolves #9553
Resolves #9554
Resolves #9555

[1]: https://github.com/apps/patchback

(cherry picked from commit d7b4010)
Glyphack pushed a commit to Glyphack/pytest that referenced this issue Jun 20, 2024
This patch prepares the project's backporting process to start being
handled by the Patchback GitHub App [[1]].

Ref pytest-dev#9384
Resolves pytest-dev#9385
Resolves pytest-dev#9553
Resolves pytest-dev#9554
Resolves pytest-dev#9555

[1]: https://github.com/apps/patchback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: infrastructure improvement to development/releases/CI structure
Projects
None yet
4 participants