Skip to content

Integrate with github, create build trigger #10

New issue

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

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

Already on GitHub? # to your account

Merged
merged 8 commits into from
Oct 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Development

Some keys are set as environment variables;


## Local Setup (optional)

#### Credentials

To be able to use functions that use the Firebase AdminSDK you need to set credentials.

1. Download the service account file from firebase
2. Set the path to that file to an environment variable

__Linux / MacOS__

```bash
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
```

__Windows (PowerShell)__

```ps
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
```

_(for more information, please see the [docs](https://firebase.google.com/docs/admin/setup))_

#### Integrations

To test specific functionality like the integrations you will also have to set the following environment variables

__Discord__

```bash
export discord.token="my_discord_token"
```

__Github__

```bash
export github.client-secret="my_github_app_client_secret"`
export github.private-key="my_github_app_private_key"
```

## Local Commands

In order to run firebase locally simply use

```
firebase serve
```

To only run one component, like `hosting`, `functions` or `firestore` you may use the `--only` flag.

```
firebase serve --only functions
```

If everything works, finally deploy the changes

```
firebase deploy
```

## New project setup

_(only when migrating to new firebase project / environment)_

```
firebase functions:config:set discord.token="my_discord_token"
firebase functions:config:set github.client-secret="my_github_app_client_secret"
firebase functions:config:set discord.private-key="my_github_app_private_key"
```
3 changes: 3 additions & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -10,3 +10,6 @@ node_modules/

# Service account
service-account.json

# Private key
*.pem
178 changes: 178 additions & 0 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@
},
"main": "lib/index.js",
"dependencies": {
"@octokit/auth-app": "^2.6.0",
"@octokit/rest": "^18.0.6",
"eris": "^0.13.3",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
1 change: 1 addition & 0 deletions functions/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as buildQueue from './buildQueue';
export { authorise } from './authorise';
export { unityVersions } from './unityVersions';
export { triggerWorkflow } from './triggerWorkflow';
27 changes: 27 additions & 0 deletions functions/src/api/triggerWorkflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Request } from 'firebase-functions/lib/providers/https';
import { Response } from 'express-serve-static-core';
import { firebase, functions } from '../config/firebase';
import { GitHub } from '../config/github';

export const triggerWorkflow = functions.https.onRequest(
async (request: Request, response: Response) => {
try {
const gitHub = await GitHub.init();

// This works only in the "installation" auth scope.
const result = await gitHub.repos.createDispatchEvent({
owner: 'unity-ci',
repo: 'docker',
event_type: 'new_build_requested',
client_payload: {
test_var: 'test value',
},
});

response.status(200).send(result);
} catch (err) {
firebase.logger.error(err);
response.status(500).send('Oops.');
}
},
);
24 changes: 24 additions & 0 deletions functions/src/config/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/rest';
import { settings } from './settings';
import { firebase } from './firebase';

const { 'private-key': privateKey, 'client-secret': clientSecret } = firebase.config().github;

export class GitHub {
static async init() {
const appOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
...settings.github.auth,
privateKey,
clientSecret,
},
});

const { data } = await appOctokit.request('/app');
firebase.logger.debug('app parameters', data);

return appOctokit;
}
}
9 changes: 9 additions & 0 deletions functions/src/config/settings.ts
Original file line number Diff line number Diff line change
@@ -7,4 +7,13 @@ export const settings = {
alerts: '763544776649605151',
},
},
github: {
auth: {
id: 84327,
privateKey: '-----BEGIN PRIVATE KEY-----\n... (do not place the actual private key here)',
installationId: 12321333,
clientId: 'Iv1.fa93dce6a47c9357',
clientSecret: '(do not place the real secret here)',
},
},
};
Loading