Skip to content

Commit 1c752e0

Browse files
authored
Adding external runner for doing a one-time full-sync (#604)
* Adding external runner for doing a one-time full-sync * Updating docs to describe GHA setup process
1 parent e059331 commit 1c752e0

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232
>
3333
> Settings files must have a `.yml` extension only. For now, the `.yaml` extension is ignored.
3434
35+
3536
## How it works
3637

38+
`Safe-settings` is designed to run as a service listening for webhook events or as a scheduled job running on some regular cadence. It can also be triggered through GitHub Actions. (See the [How to use](#how-to-use) section for details on deploying and configuring.)
39+
40+
3741
### Events
3842
The App listens to the following webhook events:
3943

@@ -364,11 +368,13 @@ You can pass environment variables; the easiest way to do it is via a `.env` fil
364368
365369
## How to use
366370
367-
1. __[Deploy and install the app](docs/deploy.md)__.
371+
1. Create an `admin` repo (or an alternative of your choosing) within your organization. Remember to set `CONFIG_REPO` if you choose something other than `admin`. See [Environment variables](#environment-variables) for more details.
372+
373+
2. Add the settings for the `org`, `suborgs`, and `repos`. Sample files can be found [here](docs/sample-settings).
374+
375+
3. __[Deploy and install the app](docs/deploy.md)__. Alternatively, the __[GitHub Actions Guide](docs/github-action.md)__ describes how to run `safe-settings` with GitHub Actions.
368376
369-
2. Create an `admin` repo (or an alternative of your choosing) within your organization. Remember to set `CONFIG_REPO` if you choose something other than `admin`. See [Environment variables](#environment-variables) for more details.
370377
371-
3. Add the settings for the `org`, `suborgs`, and `repos`. Sample files can be found [here](docs/sample-settings).
372378
373379
374380
## License

docs/github-action.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Running Safe-settings with GitHub Actions (GHA)
2+
3+
This guide describes how to schedule a full safe-settings sync using GitHub Actions. This assumes that an `admin` repository has been configured with your `safe-settings` configuration. Refer to the [How to Use](../README.md#how-to-use) docs for more details on that process.
4+
5+
6+
## GitHub App Creation
7+
Follow the [Create the GitHub App](deploy.md#create-the-github-app) guide to create an App in your GitHub account. This will allow `safe-settings` to access and modify your repos.
8+
9+
10+
## Defining the GitHub Action Workflow
11+
Running a full-sync with `safe-settings` can be done via `npm run full-sync`. This requires installing Node, such as with [actions/setup-node](https://github.com/actions/setup-node) (see example below). When doing so, the appropriate environment variables must be set (see the [Environment variables](#environment-variables) document for more details).
12+
13+
14+
### Example GHA Workflow
15+
The below example uses the GHA "cron" feature to run a full-sync every 4 hours. While not required, this example uses the `.github` repo as the `admin` repo (set via `ADMIN_REPO` env var) and the safe-settings configurations are stored in the `safe-settings/` directory (set via `CONFIG_PATH` and `DEPLOYMENT_CONFIG_FILE`).
16+
17+
```yaml
18+
name: Safe Settings Sync
19+
on:
20+
schedule:
21+
- cron: "0 */4 * * *"
22+
workflow_dispatch: {}
23+
24+
jobs:
25+
safeSettingsSync:
26+
runs-on: ubuntu-latest
27+
env:
28+
# Version/tag of github/safe-settings repo to use:
29+
SAFE_SETTINGS_VERSION: 2.1.13
30+
31+
# Path on GHA runner box where safe-settings code downloaded to:
32+
SAFE_SETTINGS_CODE_DIR: ${{ github.workspace }}/.safe-settings-code
33+
steps:
34+
# Self-checkout of 'admin' repo for access to safe-settings config:
35+
- uses: actions/checkout@v4
36+
37+
# Checkout of safe-settings repo for running full sync:
38+
- uses: actions/checkout@v4
39+
with:
40+
repository: github/safe-settings
41+
ref: $SAFE_SETTINGS_VERSION
42+
path: $SAFE_SETTINGS_CODE_DIR
43+
- uses: actions/setup-node@v4
44+
- run: npm install
45+
working-directory: $SAFE_SETTINGS_CODE_DIR
46+
- run: npm run full-sync
47+
working-directory: $SAFE_SETTINGS_CODE_DIR
48+
env:
49+
GH_ORG: ${{ vars.SAFE_SETTINGS_GH_ORG }}
50+
APP_ID: ${{ vars.SAFE_SETTINGS_APP_ID }}
51+
PRIVATE_KEY: ${{ secrets.SAFE_SETTINGS_PRIVATE_KEY }}
52+
GITHUB_CLIENT_ID: ${{ vars.SAFE_SETTINGS_GITHUB_CLIENT_ID }}
53+
GITHUB_CLIENT_SECRET: ${{ secrets.SAFE_SETTINGS_GITHUB_CLIENT_SECRET }}
54+
ADMIN_REPO: .github
55+
CONFIG_PATH: safe-settings
56+
DEPLOYMENT_CONFIG_FILE: ${{ github.workspace }}/safe-settings/deployment-settings.yml
57+
```

full-sync.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { createProbot } = require('probot')
2+
const appFn = require('./')
3+
4+
const probot = createProbot()
5+
probot.log.info('Starting full sync.')
6+
const app = appFn(probot, {})
7+
app.syncInstallation()
8+
.then(settings => {
9+
if (settings.errors.length > 0) {
10+
probot.log.error('Errors occurred during full sync.')
11+
process.exit(1)
12+
} else {
13+
probot.log.info('Done with full sync.')
14+
}
15+
})
16+
.catch(error => {
17+
process.stdout.write(`Unexpected error during full sync: ${error}\n`)
18+
process.exit(1)
19+
})

lib/settings.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Settings {
2222
settings.logError(error.message)
2323
await settings.handleResults()
2424
}
25+
return settings
2526
}
2627

2728
static async syncSubOrgs (nop, context, suborg, repo, config, ref) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"dev": "nodemon --inspect",
99
"start": "probot run ./index.js",
10+
"full-sync": "node ./full-sync.js",
1011
"test": "npm-run-all --print-label --parallel lint:* --parallel test:*",
1112
"lint:es": "eslint .",
1213
"lint:js": "standard",

0 commit comments

Comments
 (0)