Skip to content

Commit 66a6d58

Browse files
committed
fix: working
1 parent bfb89d3 commit 66a6d58

File tree

9 files changed

+22
-209
lines changed

9 files changed

+22
-209
lines changed

Diff for: README.md

+1-122
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,7 @@
1-
# node-gitlab-ci
2-
3-
**Dynamically create your `.gitlab-ci.yml` from TypeScript `.ts` files!**
4-
5-
## Installation
6-
7-
First of all navigate to your repository and install the package via `yarn` or `npm`:
8-
9-
```bash
10-
# Yarn
11-
yarn add -D node-gitlab-ci
12-
13-
# NPM
14-
npm install --save-dev node-gitlab-ci
15-
```
16-
17-
Afterwards, create a `.gitlab-ci.yml` file with the following content:
18-
19-
```yml
20-
# CI pipeline is dynamically created through `node-gitlab-ci`, please checkout `.gitlab-ci.ts`!
21-
22-
ts config:
23-
image: devowliode/node-gitlab-ci:latest
24-
stage: build
25-
script: node-gitlab-ci create-yml
26-
artifacts:
27-
paths:
28-
- .gitlab-ci.ts.yml
29-
30-
trigger pipeline:
31-
stage: test
32-
trigger:
33-
strategy: depend
34-
include:
35-
- artifact: .gitlab-ci.ts.yml
36-
job: ts config
37-
```
38-
39-
What does this mean? The first job creates the `.gitlab-ci.ts.yml` file dynamically and the second job triggers the child pipeline. Learn more about it [here](https://docs.gitlab.com/ee/ci/parent_child_pipelines.html). It is recommend to add `.gitlab-ci.ts.yml` to your `.gitignore` file.
1+
# gitlab-yml
402

413
## Usage
424

43-
### Your first `.gitlab-ci.ts`
44-
45-
It is a good practice to create a root `.gitlab-ci.ts` in your repository:
46-
47-
```ts
48-
import { Config, CreateConfigFunction } from "node-gitlab-ci";
49-
50-
const createConfig: CreateConfigFunction = async () => {
51-
const config = new Config();
52-
53-
config.stages("build", "test");
54-
55-
config.defaults({
56-
image: "alpine:latest",
57-
});
58-
59-
// Setting variables globally or per job
60-
config.variable("DOCKER_DRIVER", "overlay2");
61-
62-
// Run a job only in production branch
63-
config.job(
64-
"only production",
65-
{
66-
only: {
67-
refs: ["master"],
68-
},
69-
},
70-
true // Creates a hidden job (prefixed with a dot)
71-
);
72-
73-
// Allows you to include further configurations by glob patterns
74-
await config.include(__dirname, ["devops/.gitlab/*.ts"]);
75-
await config.include(__dirname, ["packages/*/devops/.gitlab/.gitlab-ci.ts"]);
76-
77-
return config;
78-
};
79-
80-
export { createConfig };
81-
```
82-
83-
The complete GitLab CI [pipeline configuration](https://docs.gitlab.com/ee/ci/yaml/) is typed. Give it a try within your IDE and autocomplete!
84-
85-
**Note**: You can not `import` (ES6) or `require` (ES5) all your installed modules. At the time of creating the dynamic pipeline, it is executed within [`devowliode/node-gitlab-ci`](https://hub.docker.com/r/devowliode/node-gitlab-ci) docker container and there are only the `node` modules like `fs`, `path`, ... available. Please read more about it below "Use installed modules".
86-
87-
### Dry run locally
88-
89-
If you have successfully created the above file open a terminal session, navigate to your repository and:
90-
91-
```bash
92-
# Yarn
93-
yarn node-gitlab-ci create-yml
94-
95-
# NPM
96-
npx node-gitlab-ci create-yml
97-
```
98-
99-
A file `.gitlab-ci.ts.yml` will be created.
100-
101-
### How `include` works
102-
103-
The most interesting part of `node-gitlab-ci` is how `include` works (for example you are using `yarn workspaces` or `lerna`). With `Config#include` you can dynamically include files by a glob pattern:
104-
105-
```ts
106-
// Do not forget the await!
107-
await config.include(__dirname, ["packages/*/devops/.gitlab/.gitlab-ci.ts"]);
108-
```
109-
110-
The extension file `packages/test/devops/.gitlab/.gitlab-ci.ts` must look like this:
111-
112-
```ts
113-
import { ExtendConfigFunction } from "node-gitlab-ci";
114-
115-
const extendConfig: ExtendConfigFunction = async (config) => {
116-
// Create a job
117-
config.job(/* [...] */);
118-
119-
// You can include further files
120-
await config.include(__dirname, ["./stage-*.ts"]);
121-
};
122-
123-
export { extendConfig };
124-
```
125-
1265
### How `extends` work
1276

1287
`node-gitlab-ci` resolves automatically the [`extends`](https://docs.gitlab.com/ee/ci/yaml/#extends) keyword for you so you can fully profit from nested jobs without limitations (e. g. nested `extends` with same keys like `only` are no covered by GitLab CI). This is done a **deep merge** mechanism:

Diff for: lib/config/config.js

+3-24
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
7272
exports.Config = void 0;
7373
var deepmerge_1 = __importDefault(require("deepmerge"));
7474
var glob_1 = require("glob");
75-
var node_1 = require("@gitbeaker/node");
7675
var child_process_1 = require("child_process");
7776
/**
7877
* A global OOP-style GitLab CI configurator.
@@ -203,10 +202,10 @@ var Config = /** @class */ (function () {
203202
var useName = hidden && !name.startsWith(".") ? "." + name : name;
204203
if (!this.plain.jobs[useName]) {
205204
this.plain.jobs[useName] = job;
206-
console.log("Job \"" + useName + "\" created successfully!");
205+
// console.log(`Job "${useName}" created successfully!`);
207206
}
208207
else {
209-
console.info("Job \"" + useName + "\" already exists, skipping...");
208+
// console.info(`Job "${useName}" already exists, skipping...`);
210209
}
211210
};
212211
/**
@@ -292,26 +291,6 @@ var Config = /** @class */ (function () {
292291
delete copy.jobs;
293292
return copy;
294293
};
295-
Object.defineProperty(Config.prototype, "api", {
296-
/**
297-
* Get the REST API handler.
298-
*
299-
* @see https://www.npmjs.com/package/node-gitlab
300-
*/
301-
get: function () {
302-
if (!this.gapi) {
303-
this.gapi = new node_1.Gitlab({
304-
host: process.env.CI_API_V4_URL,
305-
token: process.env.GITLAB_TOKEN,
306-
jobToken: process.env.CI_JOB_TOKEN,
307-
rejectUnauthorized: true,
308-
});
309-
}
310-
return this.gapi;
311-
},
312-
enumerable: false,
313-
configurable: true
314-
});
315294
/**
316295
* Check if files got changed by a commit by a regexp. E. g. `^\.vscode\/launch\.json$`.
317296
*/
@@ -354,7 +333,7 @@ var Config = /** @class */ (function () {
354333
jobKey = "." + from;
355334
}
356335
if (!jobKey) {
357-
console.warn("The job \"" + from + "\" does not exist, skipping...");
336+
// console.warn(`The job "${from}" does not exist, skipping...`);
358337
continue;
359338
}
360339
var jobObj = pipeline.jobs[jobKey];

Diff for: lib/index.js

100755100644
-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1111
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
1212
};
1313
Object.defineProperty(exports, "__esModule", { value: true });
14-
var commander_1 = require("commander");
15-
require("./create-yml");
16-
var program = commander_1.parse(process.argv);
17-
// If no argument is passed show help
18-
if (process.argv.length < 3) {
19-
program.help();
20-
}
2114
__exportStar(require("./config"), exports);

Diff for: package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "gitlab-ts",
2+
"name": "gitlab-yml",
33
"version": "0.1.0",
44
"description": "Create GitLab CI pipelines with TypeScript.",
55
"scripts": {
@@ -51,8 +51,6 @@
5151
"endOfLine": "lf"
5252
},
5353
"dependencies": {
54-
"@gitbeaker/node": "^23.2.0",
55-
"commander": "^5.1.0",
5654
"deepmerge": "^4.2.2",
5755
"glob": "^7.1.6",
5856
"ts-node": "^8.10.1",

Diff for: src/config/config.ts

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { GitLabCi, JobDefinition } from ".";
22
import merge from "deepmerge";
33
import { sync as globSync } from "glob";
4-
import { Gitlab } from "@gitbeaker/node";
54
import { execSync } from "child_process";
65

76
type JobDefinitionExtends = JobDefinition & { needsExtends?: string[] };
87
type MacroArgs = {};
98

10-
type ConstructedType<Constructor> = Constructor extends {
11-
new (...args: any[]): infer B;
12-
}
13-
? B
14-
: never;
15-
16-
type GitlabType = ConstructedType<typeof Gitlab>;
17-
189
/**
1910
* A global OOP-style GitLab CI configurator.
2011
*/
@@ -35,11 +26,6 @@ class Config {
3526
*/
3627
private patchers: Array<(plain: GitLabCi) => void> = [];
3728

38-
/**
39-
* REST API handler.
40-
*/
41-
private gapi?: GitlabType;
42-
4329
/**
4430
* The top-level `workflow:` key applies to the entirety of a pipeline, and will determine whether
4531
* or not a pipeline is created. It currently accepts a single `rules:` key that operates similarly
@@ -158,9 +144,9 @@ class Config {
158144

159145
if (!this.plain.jobs[useName]) {
160146
this.plain.jobs[useName] = job;
161-
console.log(`Job "${useName}" created successfully!`);
147+
// console.log(`Job "${useName}" created successfully!`);
162148
} else {
163-
console.info(`Job "${useName}" already exists, skipping...`);
149+
// console.info(`Job "${useName}" already exists, skipping...`);
164150
}
165151
}
166152

@@ -231,24 +217,6 @@ class Config {
231217

232218
return copy;
233219
}
234-
235-
/**
236-
* Get the REST API handler.
237-
*
238-
* @see https://www.npmjs.com/package/node-gitlab
239-
*/
240-
public get api() {
241-
if (!this.gapi) {
242-
this.gapi = new Gitlab({
243-
host: process.env.CI_API_V4_URL,
244-
token: process.env.GITLAB_TOKEN,
245-
jobToken: process.env.CI_JOB_TOKEN,
246-
rejectUnauthorized: true,
247-
}) as GitlabType;
248-
}
249-
return this.gapi;
250-
}
251-
252220
/**
253221
* Check if files got changed by a commit by a regexp. E. g. `^\.vscode\/launch\.json$`.
254222
*/
@@ -291,7 +259,7 @@ class Config {
291259
}
292260

293261
if (!jobKey) {
294-
console.warn(`The job "${from}" does not exist, skipping...`);
262+
// console.warn(`The job "${from}" does not exist, skipping...`);
295263
continue;
296264
}
297265
const jobObj = pipeline.jobs[jobKey];

Diff for: src/config/types/artifacts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type ArtifactsDefinition = {
55
/**
66
* @see https://docs.gitlab.com/ee/ci/yaml/#artifactspaths
77
*/
8-
paths: string[];
8+
paths?: string[];
99
/**
1010
* @see https://docs.gitlab.com/ee/ci/yaml/#artifactsexpose_as
1111
*/

Diff for: src/config/types/only.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
type OnlyExpression = {
2-
refs?: string[];
3-
variables?: string[];
4-
changes?: string[];
5-
kubernetes?: "active";
6-
};
1+
type OnlyExpression =
2+
| {
3+
refs?: RefList;
4+
variables?: string[];
5+
changes?: string[];
6+
kubernetes?: "active";
7+
}
8+
| RefList;
79

810
type ExpectExpression = OnlyExpression;
911

12+
type RefList = Ref[];
13+
14+
type Ref = string | RegExp;
15+
1016
export { OnlyExpression, ExpectExpression };

Diff for: src/config/types/script.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @see https://docs.gitlab.com/ee/ci/yaml/#script
33
*/
4-
type ScriptDefinition = string[];
4+
type ScriptDefinition = string[] | string;
55

66
export { ScriptDefinition };

Diff for: src/index.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
#!/usr/bin/env node
22

3-
import { parse } from "commander";
4-
import "./create-yml";
5-
6-
const program = parse(process.argv);
7-
8-
// If no argument is passed show help
9-
if (process.argv.length < 3) {
10-
program.help();
11-
}
12-
133
export * from "./config";

0 commit comments

Comments
 (0)