Skip to content

Commit

Permalink
ci: let define build-from-remote inputs separately
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 28, 2024
1 parent a037e3c commit 24cb020
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/npm-publish-from-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ on:
inputs:
source:
description: 'source reference, example: `google/zx/main/0cba54884f3084af1674118ef6299302d82daaf9`'
required: true
repoName:
description: 'source gh repo, like `google/zx`'
repoBranch:
description: 'target branch'
default: 'main'
repoCommit:
description: 'commit id'
# buildCmd:
# description: 'build cmd'
# default: 'npm ci && npm run build'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"test:unit": "c8 -r lcov -r text -o target/coverage -x src/scripts -x src/test -x target node --loader ts-node/esm --experimental-specifier-resolution=node src/scripts/test.mjs",
"test:smoke:esm": "node ./src/test/smoke/invoke.test.mjs",
"test:smoke:cjs": "node src/test/smoke/invoke.test.cjs",
"publish:draft": "npm run build && npm publish --no-git-tag-version"
"publish:draft": "npm run build && npm publish --no-git-tag-version",
"manual:test:build-from-remote": "NODE_OPTIONS='--experimental-strip-types' npx zx@latest ./src/scripts/build-from-remote.mts --mode=test"
},
"repository": {
"type": "git",
Expand Down
45 changes: 27 additions & 18 deletions src/scripts/build-from-remote.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ const MODE = argv.mode || 'run'
const SECRETS = ['NPM_TOKEN', 'GH_TOKEN', 'GITHUB_TOKEN', 'AUTH_TOKEN']
const GH_URL = 'https://github.com'

;(async() => {
try {
if (MODE === 'run') {
await run()
process.exit(0)
} else if (MODE === 'test') {
test()
} else {
throw new Error(`unknown mode: ${MODE}. Only 'test' & 'run' values are supported`)
}
} catch (e: unknown) {
console.error((e as Error).message)
process.exit(1)
}
})()

export interface TContext {
cwd?: string
buildCmd?: string
Expand All @@ -45,12 +29,15 @@ export const createContext = (av: Record<any, string> = argv, env = process.env)
...JSON.parse(av.ctx || env.CTX || '{}'),
...av,
}
const sourceRef = parseSourceRef(input.source)

const sourceRef = input.source && parseSourceRef(input.source)
const ctx: TContext = {
...input,
...sourceRef
}

if (!(ctx.repoName && ctx.repoBranch && ctx.repoCommit)) throw new Error('One of `source` or `repoName, repoBranch, repoCommit` is required')

return ctx
}

Expand Down Expand Up @@ -86,6 +73,22 @@ export const buildFromRemote = async (av = argv, env = process.env)=> {
await buildSource(ctx)
}

;(async() => {
try {
if (MODE === 'run') {
await run()
process.exit(0)
} else if (MODE === 'test') {
test()
} else {
throw new Error(`unknown mode: ${MODE}. Only 'test' & 'run' values are supported`)
}
} catch (e: unknown) {
console.error((e as Error).message)
process.exit(1)
}
})()

async function run () {
await buildFromRemote()
}
Expand All @@ -98,14 +101,20 @@ function test(){
const ref = parseSourceRef(source)
assert.deepEqual(createContext({cwd: 'foo', source}), {cwd: 'foo', ...ref, source})
assert.deepEqual(createContext({}, {CTX: JSON.stringify({cwd: 'foo', source})}), {cwd: 'foo', ...ref, source})

try {
createContext({}, {})
} catch (e) {
assert.equal((e as Error).message, 'One of `source` or `repoName, repoBranch, repoCommit` is required')
}
})
})

describe('protect', () => {
it('raises an error if secrets are exposed', () => {
try {
protect({ NPM_TOKEN: 'Foo' })
} catch (e: unknown) {
} catch (e) {
assert.equal((e as Error).message, 'Credentials should not be observable from the build step')
}
})
Expand Down

0 comments on commit 24cb020

Please sign in to comment.