|
1 | | -# :electric_plug: `probot-actions-adapter` |
| 1 | +# :electric_plug: `@probot/adapter-github-actions` |
2 | 2 |
|
3 | | -> An adapter that takes a [Probot](https://probot.github.io/) app and makes it compatible with [GitHub Actions](https://github.com/features/actions) |
| 3 | +> Adapter to run a [Probot](https://probot.github.io/) application function in [GitHub Actions](https://github.com/features/actions) |
4 | 4 |
|
5 | | -<a href="https://github.com/probot/actions-adapter"><img alt="GitHub Actions status" src="https://github.com/probot/actions-adapter/workflows/Build/badge.svg"></a> |
6 | | - |
7 | | -## Contents |
8 | | - |
9 | | -- [Installation](#installation) |
10 | | -- [Usage](#usage) |
11 | | -- [Authentication](#authentication) |
12 | | -- [Caveats](#caveats) |
13 | | - |
14 | | -## Installation |
15 | | - |
16 | | -```shell |
17 | | -npm i -S probot-actions-adapter |
18 | | -``` |
| 5 | +[](https://github.com/probot/adapter-github-actions/actions) |
19 | 6 |
|
20 | 7 | ## Usage |
21 | 8 |
|
22 | | -1. Add an `action.js` to your Probot project, like [the one shown below](#example-actionjs) |
23 | | -1. Add an `action.yml` to your Probot project, like [the one shown below](#example-actionyml) |
24 | | -1. _Vendor in_ your `node_modules`, as [recommended by the official Actions documentation](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github) |
25 | | -1. Optional, but recommended, update your project's README with an example workflow showing how to consume your action |
| 9 | +Create your Probot Application as always |
26 | 10 |
|
27 | | -### Example `action.js` |
| 11 | +```js |
| 12 | +// app.js |
| 13 | +module.exports = (app) => { |
| 14 | + app.on("issues.opened", async (context) => { |
| 15 | + const params = context.issue({ body: "Hello World!" }); |
| 16 | + await context.octokit.issues.createComment(params); |
| 17 | + }); |
| 18 | +}; |
| 19 | +``` |
28 | 20 |
|
29 | | -```javascript |
30 | | -// Require the adapter |
31 | | -const runProbot = require('probot-actions-adapter'); |
| 21 | +Then in the entrypoint of your GitHub Action, require `@probot/github-action` instead of `probot` |
32 | 22 |
|
33 | | -// Require your Probot app's entrypoint, usually this is just index.js |
34 | | -const app = require('./index'); |
| 23 | +```js |
| 24 | +// index.js |
| 25 | +const { run } = require("@probot/github-action"); |
| 26 | +const app = require("./app"); |
35 | 27 |
|
36 | | -// Adapt the Probot app for Actions |
37 | | -// This also acts as the main entrypoint for the Action |
38 | | -runProbot(app); |
| 28 | +run(app).catch((error) => { |
| 29 | + console.error(error); |
| 30 | + process.exit(1); |
| 31 | +}); |
39 | 32 | ``` |
40 | 33 |
|
41 | | -### Example `action.yml` |
| 34 | +Then use `index.js` as your entrypoint in the `action.yml` file |
42 | 35 |
|
43 | 36 | ```yaml |
44 | | -name: 'Probot app name' |
45 | | -description: 'Probot app description.' |
| 37 | +name: "Probot app name" |
| 38 | +description: "Probot app description." |
46 | 39 | runs: |
47 | | - using: 'node12' |
48 | | - main: 'action.js' |
| 40 | + using: "node12" |
| 41 | + main: "action.js" |
49 | 42 | ``` |
50 | 43 |
|
51 | | -See [the documentation](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions) for `action.yml` syntax details. |
| 44 | +**Important**: Your external dependencies will not be installed, you have to either vendor them in by committing the contents of the `node_modules` folder, or compile the code to a single executable script (recommended). See [GitHub's documentation](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github) |
52 | 45 |
|
53 | | -## Authentication |
| 46 | +For an example Probot App that is continuously published as GitHub Action, see https://github.com/probot/example-github-action#readme |
54 | 47 |
|
55 | | -Authentication is via [the `GITHUB_TOKEN` secret _automatically_ provided by GitHub](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token), which should be exposed as an environment variable, `GITHUB_TOKEN`. This can be achieved by including the appropriate [`env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#env), [`jobs.<job_id>.env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idenv), or [`jobs.<job_id>.steps.env`](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsenv) value in your workflow file. |
| 48 | +## How it works |
56 | 49 |
|
57 | | -### Example via `jobs.<job_id>.steps.env` |
| 50 | +[Probot](https://probot.github.io/) is a framework for building [GitHub Apps](docs.github.com/apps), which is different to creating [GitHub Actions](https://docs.github.com/actions/) in many ways, but the functionality is the same: |
58 | 51 |
|
59 | | -Include the following in your workflow file, when calling your Probot action: |
| 52 | +Both get notified about events on GitHub, which you can act on. While a GitHub App gets notified about a GitHub event via a webhook request sent by GitHub, a GitHub Action can receive the event payload by reading a JSON file from the file system. We can abstract away the differences, so the same hello world example app shown above works in both environments. |
60 | 53 |
|
61 | | -```yaml |
62 | | -... |
63 | | -steps: |
64 | | - - name: My probot action |
65 | | - ... |
66 | | - env: |
67 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
68 | | -... |
69 | | -``` |
| 54 | +Relevant differences for Probot applications: |
70 | 55 |
|
71 | | -## Caveats |
| 56 | +1. You cannot authenticate as the app. The `probot` instance you receive is authenticated using a GitHub token. In most cases the token will be set to `secrets.GITHUB_TOKEN`, which is [an installation access token](https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret). The provided `GITHUB_TOKEN` expires when the job is done or after 6 hours, whichever comes first. You do not have access to an `APP_ID` or `PRIVATE_KEY`, you cannot create new tokens or renew the provided one. |
| 57 | +2. `secrets.GITHUB_TOKEN` is scoped to the current repository. You cannot read data from other repositories unless they are public, you cannot update any other repositories, or access organization-level APIs. |
| 58 | +3. You could provide a personal access token instead of `secrets.GITHUB_TOKEN` to workaround the limits of a repository-scoped token, but be sure you know what you are doing. |
| 59 | +4. You don't need to configure `WEBHOOK_SECRET`, because no webhook request gets sent, the event information can directly be retrieved from environment variables and the local file system. |
72 | 60 |
|
73 | | -This adapter is designed to work well with Probot apps that are essentially stateless webhook handlers. |
| 61 | +For a more thorough comparison, see [@jasonetco's](https://github.com/jasonetco) posts: |
74 | 62 |
|
75 | | -A few other limitations exist: |
| 63 | +1. [Probot App or GitHub Action](https://jasonet.co/posts/probot-app-or-github-action/) (Jan 2019) |
| 64 | +2. [Update from April 2020](https://jasonet.co/posts/probot-app-or-github-action-v2/) |
76 | 65 |
|
77 | | -1. The adapter will _only_ work for Probot apps that receive webhook events that Actions also receives: https://docs.github.com/en/actions/reference/events-that-trigger-workflows |
78 | | -1. The adapter will _only_ work for Probot apps with a permissions set that is less than, or equivalent to the permission set attached to the `GITHUB_TOKEN`: https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token |
| 66 | +## License |
79 | 67 |
|
80 | | -If that's you, then great! :rocket: |
| 68 | +[ISC](LICENSE) |
0 commit comments