Added --verbose flag and support for secrets

This commit is contained in:
Thomas Schoffelen 2018-12-31 19:45:13 +01:00
parent 13d8985461
commit d8303412eb
No known key found for this signature in database
GPG Key ID: 03F5C979F442A38D
3 changed files with 42 additions and 13 deletions

View File

@ -18,7 +18,6 @@ Other prerequisites:
* A repo with a `.github/main.workflow` file
## Usage
```
@ -31,10 +30,20 @@ Options:
-h, --help output usage information
```
### Passing secrets
You can set the value of [secrets](https://developer.github.com/actions/creating-workflows/storing-secrets/) defined in your workflow by passing them as environment variables, e.g.:
```sh
MY_SECRET_TOKEN=yo-mamma gha
```
## Development
This app currently only supports Github-hosted Dockerfiles (i.e. with the
There are still some things to do to achieve full functional parity.
This app currently only supports GitGub-hosted Dockerfiles (i.e. with the
`{user}/{repo}@{ref}` or `{user}/{repo}/{path}@{ref}` syntax), so we still need
to add:
@ -44,5 +53,4 @@ to add:
Some improvements are also possible in other areas:
- [ ] Add support for [secrets](https://developer.github.com/actions/creating-workflows/storing-secrets/)
- [ ] Add support for default [`GITHUB_TOKEN`](https://developer.github.com/actions/creating-workflows/storing-secrets/#github-token-secret) env var

View File

@ -14,6 +14,7 @@ const {
program
.version('1.0.0')
.option('-v, --verbose', 'Verbose output')
.option('-f <workflowfile>', 'Set workflow file path, defaults to .github/main.workflow')
.option('-e <event>', 'Set event, defaults to push')
.parse(process.argv)
@ -22,6 +23,7 @@ checkDocker()
const content = hcl.parse(fs.readFileSync(program.workflowfile || '.github/main.workflow', 'utf8'))
const event = program.event || 'push'
const verbose = program.verbose || false
const actions = cleanupHcl(content.action)
const workflows = cleanupHcl(content.workflow)
@ -37,7 +39,7 @@ for (const workflowTitle in workflows) {
workflow.resolves
.forEach((action) => {
buildDependencies(action, actions)
.forEach((action) => runAction(action, actions, event))
.forEach((action) => runAction(action, actions, event, verbose))
})
}
}

View File

@ -57,7 +57,7 @@ const buildDependencies = (startAction, actions) => {
return output.reverse()
}
const resolveRunner = (uses) => {
const resolveRunner = (uses, verbose) => {
// TODO: add support for a local Dockerfile path
// https://developer.github.com/actions/creating-workflows/workflow-configuration-options/#using-a-dockerfile-image-in-an-action
@ -86,9 +86,13 @@ const resolveRunner = (uses) => {
let baseDir = path.dirname(dockerFile)
let imageName = path.basename(baseDir)
exec(`if [[ "$(docker images -q ${imageName} 2> /dev/null)" == "" ]]; then
docker build ${baseDir} -f ${dockerFile} -t ${imageName};
fi`)
const cmd = `docker build ${baseDir} -f ${dockerFile} -t ${imageName}`
if (verbose) {
console.log(chalk.dim(cmd))
}
exec(`if [[ "$(docker images -q ${imageName} 2> /dev/null)" == "" ]]; then ${cmd}; fi`)
return imageName
}
@ -107,7 +111,7 @@ const defaultEnv = (action, event) => {
}
}
const runAction = (actionTitle, actions, event) => {
const runAction = (actionTitle, actions, event, verbose) => {
console.log(chalk.bold(chalk.blue(`===> ${actionTitle}`)))
const action = actions[actionTitle]
@ -116,7 +120,7 @@ const runAction = (actionTitle, actions, event) => {
}
const uses = action.uses
const imageName = resolveRunner(uses)
const imageName = resolveRunner(uses, verbose)
let args = []
if ('runs' in action && action.runs) {
@ -124,6 +128,13 @@ const runAction = (actionTitle, actions, event) => {
}
action.env = Object.assign(defaultEnv(action, event), 'env' in action && action.env ? action.env : {})
if ('secrets' in action && action.secrets && typeof action.secrets === 'object') {
action.secrets.forEach(secret => {
if (secret in process.env) {
action.env[secret] = process.env[secret]
}
})
}
for (const title in action.env) {
if (!action.env.hasOwnProperty(title)) {
continue
@ -143,13 +154,21 @@ const runAction = (actionTitle, actions, event) => {
const cmd = [
`docker run`,
`--rm -t`,
`--rm`,
`-t`,
`-v \`pwd\`:/github/workspace`,
`-v /tmp/gh-home:/github/home`,
`-v ${assetPath}:/github/workflow/event.json:ro`,
`-w /github/workspace`,
`${args.join(' ')} ${imageName} ${after}`
].join(' ')
args.join(' '),
imageName,
after
].join(' ').trim()
if (verbose) {
console.log(chalk.dim(cmd))
}
const res = exec(cmd)
if (res.code === 0) {