Skip to content

Commit

Permalink
Implemented exec and manual "command" arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
firecow committed Jan 28, 2020
1 parent 05c23cc commit ed49abb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TODO: Fill this

## Quirks
### Artifacts
Artifacts works right now, as along as you don't overwirte git tracked files and as long as you don't use dependencies tag.
Artifacts works right now, as along as you don't overwrite tracked files and as long as you don't use dependencies tag.

# Development
## Scripts
Expand All @@ -75,24 +75,23 @@ Artifacts works right now, as along as you don't overwirte git tracked files and
$ npm run build-linux
$ npm run build-win
$ npm run build-macos
$ npm run build-all

# TODO

## Known Bugs
- include:local isn't recursive

## Features
- Execute single job `gitlab-local-pipeline ts-lint`
- Verbosity on .gitlab-ci.local.yml overrides and appends.

## Unsupported tags, will be implemented in order
- needs (directed acyclic graph)
- rules
- when:always
- when:on_failure
- when:delayed
- start_in (Used only with when:delayed)
- when:always
- when:never
- include:file
- include:template
- include:remote
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"lint": "tslint --project .",
"lint-fix": "tslint --fix --project .",
"ncu": "ncu --semverLevel major -e 2",
"test-yml-spec": "node -r source-map-support/register dist/index.js -m manual_job_invoked_from_cli --cwd tests/test-yml-spec"
"test-yml-spec": "node -r source-map-support/register dist/index.js manual manual_job_invoked_from_cli --cwd tests/test-yml-spec",
"test-exec-never-job": "node -r source-map-support/register dist/index.js exec never_job --cwd tests/test-yml-spec"
},
"bin": "dist/index.js",
"dependencies": {
Expand Down
47 changes: 45 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const cwd = argv.cwd || process.cwd();
const m: any = argv.m;
const manualArgs: string[] = [].concat(m || []);

const firstArg = argv._[0] ?? "pipeline";

if (firstArg === "manual") {
for (let i = 1; i < argv._.length; i += 1) {
manualArgs.push(argv._[i]);
}
}

const parser = new Parser(cwd, logger);

const runJobs = async () => {
Expand All @@ -67,9 +75,15 @@ const runJobs = async () => {
console.log(`=> ${c.yellow(`${stageName}`)} > ${c.blueBright(`${jobNames}`)} ${c.magentaBright("starting")}...`);
for (const job of jobs) {
if (job.isManual() && !manualArgs.includes(job.name)) {
console.log(`${c.blueBright(`${job.name}`)} skipped. Manual job`);
console.log(`${c.blueBright(`${job.name}`)} skipped. when:manual`);
continue;
}

if (job.isNever()) {
console.log(`${c.blueBright(`${job.name}`)} skipped. when:never`);
continue;
}

const jobPromise = job.start();
promises.push(jobPromise);
}
Expand All @@ -86,9 +100,38 @@ const runJobs = async () => {
}
};

const runExecJobs = async () => {
const promises: Array<Promise<any>> = [];
for (let i = 1; i < argv._.length; i += 1) {
const jobName = argv._[i];
const job = parser.getJobs().get(argv._[i]);
if (!job) {
console.error(`${c.blueBright(`${jobName}`)} ${c.red(" could not be found")}`);
process.exit(1);
}

const jobPromise = job.start();
promises.push(jobPromise);
}

try {
await Promise.all(promises);
console.log("");
} catch (e) {
if (e !== "") {
console.error(e);
}
process.exit(1);
}
};

process.on("uncaughtException", (err) => {
// Handle the error safely
console.log(err);
});

runJobs().catch();
if (["pipeline", "manual"].includes(firstArg)) {
runJobs().catch();
} else if (firstArg === "exec") {
runExecJobs().catch();
}
4 changes: 4 additions & 0 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class Job {
return this.when === "manual";
}

public isNever(): boolean {
return this.when === "never";
}

public async start(): Promise<void> {
this.running = true;

Expand Down
6 changes: 6 additions & 0 deletions tests/test-yml-spec/.gitlab-ci.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ project_context:
local_only_job:
stage: build
script: echo "I only get run locally '${LOCAL_ONLY}'"

never_job:
when: never
stage: startup
script:
- echo "I'm when:never i could have only been called by 'gitlab-local-pipeline exec never_job'"
2 changes: 1 addition & 1 deletion tests/test-yml-spec/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ manual_job_invoked_from_cli:
when: manual
stage: last
script:
- echo "I was invoked manually, because 'gitlab-local-pipeline manual_job_invoked_from_cli' was called"
- echo "I was invoked manually, because 'gitlab-local-pipeline manual manual_job_invoked_from_cli' was called"

0 comments on commit ed49abb

Please sign in to comment.