-
Notifications
You must be signed in to change notification settings - Fork 636
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
sync GitHub action distribution to S3 #5
Merged
Merged
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
34cdf93
WIP lambda to sync s3 dist
npalm fb66851
Add update check
npalm 2ae6b5b
Add syncer
npalm c4ecbb3
Refactor
npalm c0c767d
Refactor to functions
npalm 6263ebd
Fix terraform validate
npalm 017370f
Fix error
npalm bd4717a
Fix terraform validate
npalm 3b84221
Fix terraform validate
npalm ad576c8
Add syncer
npalm 40da5f7
Add syncer
npalm 06ae582
Add linter, clenup
npalm 365f1a5
Add linter, clenup
npalm 5acb2c5
Merge branch 'master' of github.com:philips-labs/terraform-aws-github…
npalm 397ba44
Add modules to root modules
npalm 04606bc
Update modules/action-runner-binary-cache/variables.tf
npalm 31db245
Fix review comments
npalm b54fbc2
Update jabascript libs
npalm 25cb2b1
Add aws tags
npalm e9b8e1a
Add aws tags
npalm 219c4c7
Update default example
npalm 25fdbeb
Update default example
npalm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Lambda Runner Distribution Syncer | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- .github/workflows/lambda-agent-webhook.yml | ||
- "modules/action-runner-binary-cache/lambdas/syncer/**" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
container: node:12 | ||
defaults: | ||
run: | ||
working-directory: modules/action-runner-binary-cache/lambdas/syncer | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install dependencies | ||
run: yarn install | ||
- name: Run linter | ||
run: yarn lint | ||
- name: Build distribution | ||
run: yarn build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
*.out | ||
example/*.secrets*.tfvars | ||
.envrc | ||
*.zip | ||
*.gz | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
modules/action-runner-binary-cache/lambdas/syncer/.eslintrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
root: true, | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['@typescript-eslint'], | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/eslint-recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
], | ||
}; |
15 changes: 15 additions & 0 deletions
15
modules/action-runner-binary-cache/lambdas/syncer/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# dependencies | ||
node_modules/ | ||
|
||
# production | ||
dist/ | ||
build/ | ||
|
||
# misc | ||
.DS_Store | ||
.env* | ||
*.zip | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v12.16.1 |
5 changes: 5 additions & 0 deletions
5
modules/action-runner-binary-cache/lambdas/syncer/.prettierrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"printWidth": 120, | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
4 changes: 4 additions & 0 deletions
4
modules/action-runner-binary-cache/lambdas/syncer/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
30 changes: 30 additions & 0 deletions
30
modules/action-runner-binary-cache/lambdas/syncer/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "github-runner-lambda-syncer", | ||
"version": "1.0.0", | ||
"main": "lambda.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "ts-node-dev src/local.ts", | ||
"test": "NODE_ENV=test jest", | ||
"test:watch": "NODE_ENV=test jest --watch", | ||
"lint": "yarn eslint --ext ts,tsx src", | ||
"watch": "ts-node-dev --respawn --exit-child src/local.ts", | ||
"build": "ncc build src/lambda.ts -o dist", | ||
"dist": "yarn build && cd dist && zip ../syncer.zip index.js" | ||
}, | ||
"devDependencies": { | ||
"@octokit/rest": "^17.6.0", | ||
"@types/jest": "^25.2.1", | ||
"@types/node": "^13.13.4", | ||
"@types/request": "^2.48.4", | ||
"@typescript-eslint/eslint-plugin": "^2.30.0", | ||
"@typescript-eslint/parser": "^2.30.0", | ||
"@zeit/ncc": "^0.22.1", | ||
"aws-sdk": "^2.645.0", | ||
"eslint": "^6.8.0", | ||
"jest": "^25.4.0", | ||
"ts-jest": "^25.4.0", | ||
"ts-node-dev": "^1.0.0-pre.44", | ||
"typescript": "^3.8.3" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
modules/action-runner-binary-cache/lambdas/syncer/src/lambda.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { handle } from './syncer/handler'; | ||
|
||
module.exports.handler = async (event: any, context: any, callback: any): Promise<any> => { | ||
const statusCode = await handle(); | ||
return callback(null, { | ||
statusCode: statusCode, | ||
}); | ||
}; |
7 changes: 7 additions & 0 deletions
7
modules/action-runner-binary-cache/lambdas/syncer/src/local.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { handle } from './syncer/handler'; | ||
|
||
handle() | ||
.then((c) => console.log(c)) | ||
.catch((e) => { | ||
console.log(e); | ||
}); |
95 changes: 95 additions & 0 deletions
95
modules/action-runner-binary-cache/lambdas/syncer/src/syncer/handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Octokit } from '@octokit/rest'; | ||
import { PassThrough } from 'stream'; | ||
import request from 'request'; | ||
import { S3 } from 'aws-sdk'; | ||
import AWS from 'aws-sdk'; | ||
|
||
AWS.config.update({ | ||
region: process.env.AWS_REGION, | ||
}); | ||
const s3 = new S3(); | ||
|
||
const versionKey = 'name'; | ||
const bucketName = process.env.S3_BUCKET_NAME as string; | ||
const bucketObjectKey = process.env.S3_OBJECT_KEY as string; | ||
if (!bucketName || !bucketObjectKey) { | ||
throw new Error('Please check all mandatory variables are set.'); | ||
} | ||
|
||
async function getCachedVersion(): Promise<string | undefined> { | ||
try { | ||
const objectTagging = await s3 | ||
.getObjectTagging({ | ||
Bucket: bucketName, | ||
Key: bucketObjectKey, | ||
}) | ||
.promise(); | ||
const versions = objectTagging.TagSet?.filter((t: S3.Tag) => t.Key === versionKey); | ||
return versions.length === 1 ? versions[0].Value : undefined; | ||
} catch (e) { | ||
console.debug('No tags found'); | ||
return undefined; | ||
} | ||
} | ||
|
||
interface ReleaseAsset { | ||
name: string; | ||
downloadUrl: string; | ||
} | ||
|
||
async function getLinuxReleaseAsset(): Promise<ReleaseAsset | undefined> { | ||
const githubClient = new Octokit(); | ||
const linuxAssets = ( | ||
await githubClient.repos.getLatestRelease({ | ||
gertjanmaas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
owner: 'actions', | ||
repo: 'runner', | ||
}) | ||
).data.assets.filter((a) => a.name?.includes('actions-runner-linux-x64-')); | ||
return linuxAssets?.length === 1 | ||
? { name: linuxAssets[0].name, downloadUrl: linuxAssets[0].browser_download_url } | ||
: undefined; | ||
} | ||
|
||
async function uploadToS3(actionRunnerReleaseAsset: ReleaseAsset): Promise<void> { | ||
const writeStream = new PassThrough(); | ||
s3.upload({ | ||
Bucket: bucketName, | ||
Key: bucketObjectKey, | ||
Tagging: versionKey + '=' + actionRunnerReleaseAsset.name, | ||
Body: writeStream, | ||
}).promise(); | ||
|
||
await new Promise((resolve, reject) => { | ||
console.debug('Start downloading %s and uploading to S3.', actionRunnerReleaseAsset.name); | ||
request | ||
.get(actionRunnerReleaseAsset.downloadUrl) | ||
.pipe(writeStream) | ||
.on('finish', () => { | ||
console.info(`The new distribution is uploaded to S3.`); | ||
resolve(); | ||
}) | ||
.on('error', (error) => { | ||
reject(error); | ||
}); | ||
}).catch((error) => { | ||
console.error(`Exception: ${error}`); | ||
}); | ||
} | ||
|
||
export const handle = async (): Promise<number> => { | ||
const actionRunnerReleaseAsset = await getLinuxReleaseAsset(); | ||
if (actionRunnerReleaseAsset === undefined) { | ||
console.error('Cannot find github release asset.'); | ||
return 500; | ||
} | ||
|
||
const currentVersion = await getCachedVersion(); | ||
console.log('latest: ' + currentVersion); | ||
if (currentVersion === undefined || currentVersion != actionRunnerReleaseAsset.name) { | ||
uploadToS3(actionRunnerReleaseAsset); | ||
} else { | ||
console.debug('Distribution is up-to-date, no action.'); | ||
} | ||
|
||
return 200; | ||
}; |
60 changes: 60 additions & 0 deletions
60
modules/action-runner-binary-cache/lambdas/syncer/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Basic Options */ | ||
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, | ||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, | ||
"outDir": "build", | ||
"lib": ["es2020", "DOM"] /* Specify library files to be included in the compilation. */, | ||
"allowJs": true /* Allow javascript files to be compiled. */, | ||
// "checkJs": true, /* Report errors in .js files. */ | ||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ | ||
// "declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | ||
// "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
// "outFile": "./", /* Concatenate and emit output to single file. */ | ||
// "outDir": "./", /* Redirect output structure to the directory. */ | ||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ | ||
// "composite": true, /* Enable project compilation */ | ||
// "incremental": true, /* Enable incremental compilation */ | ||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ | ||
// "removeComments": true, /* Do not emit comments to output. */ | ||
// "noEmit": true, /* Do not emit outputs. */ | ||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */ | ||
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */, | ||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ | ||
/* Strict Type-Checking Options */ | ||
"strict": true /* Enable all strict type-checking options. */, | ||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ | ||
// "strictNullChecks": true, /* Enable strict null checks. */ | ||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */ | ||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ | ||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ | ||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ | ||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ | ||
/* Additional Checks */ | ||
// "noUnusedLocals": true, /* Report errors on unused locals. */ | ||
// "noUnusedParameters": true, /* Report errors on unused parameters. */ | ||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ | ||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ | ||
/* Module Resolution Options */ | ||
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */, | ||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | ||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ | ||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ | ||
// "typeRoots": [] /* List of folders to include type definitions from. */, | ||
// "types": [] /* Type declaration files to be included in compilation. */, | ||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ | ||
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, | ||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ | ||
/* Source Map Options */ | ||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ | ||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ | ||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ | ||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ | ||
/* Experimental Options */ | ||
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */, | ||
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */, | ||
"resolveJsonModule": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StatusCode isn't necessary, this lambda isn't part of the API