Skip to content

Commit 3618c10

Browse files
authored
Merge pull request #5 from bredondev/onevar
Sub-commands (inline variable setting)
2 parents 3e1cbdd + 5a4fa50 commit 3618c10

File tree

10 files changed

+244
-108
lines changed

10 files changed

+244
-108
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## Unreleased
88

9-
Nil.
9+
### Changed
10+
11+
- Introduce sub-commands to add the possibility to set a variable on the fly, without a `gitlab.env.yml`.
12+
- The `token` can be retrieved from env variable.
13+
- New commands are :
14+
- `glci sv <options>` to set one variable.
15+
- `glci sav <options>` to set all variables in `gitlab.env.yml`.
1016

1117
## [v1.1.2] - 2017-09-06
1218

README.md

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Travis CI](https://img.shields.io/travis/temando/gitlab-ci-variables-cli.svg)](https://travis-ci.org/temando/gitlab-ci-variables-cli)
55
[![MIT License](https://img.shields.io/github/license/temando/gitlab-ci-variables-cli.svg)](https://en.wikipedia.org/wiki/MIT_License)
66

7-
CLI tool to allow setting multiple pipeline variables on Gitlab CI, instead of going through Gitlab UI and adding individual variables.
7+
CLI tool to allow setting pipeline variables on Gitlab CI, instead of going through Gitlab UI and adding individual variables.
88

99
Supports Gitlab API v4, available since Gitlab 9.0.
1010

@@ -18,6 +18,23 @@ $ npm install -g gitlab-ci-variables-setter-cli
1818

1919
## Usage
2020

21+
### One variable (`glci sv`)
22+
23+
Run the following command, where:
24+
25+
- `gitlab-token` is your Gitlab personal access token
26+
- `gitlab-project-url` is your project url on gitlab, e.g. https://gitlab.com/gitlab-org/gitlab-ce
27+
- `key` is the variable you want to set
28+
- `value` is the value of the variable you want to set
29+
30+
```sh
31+
$ glci sv --token <gitlab-token> --url <gitlab-project-url> --key <key> --value <value>
32+
Set <key> = <value> for gitlab-org/gitlab-ce.
33+
Completed setting variable on Gitlab CI.
34+
```
35+
36+
### Several variables (`glci sav`)
37+
2138
Put all required variable key/values on a properties file named `gitlab.env.yml`, e.g:
2239

2340
```yml
@@ -36,33 +53,40 @@ Run the following command from the directory that contains the properties file,
3653
- `gitlab-project-url` is your project url on gitlab, e.g. https://gitlab.com/gitlab-org/gitlab-ce
3754

3855
```sh
39-
$ setAllVars --token <gitlab-token> --url <gitlab-project-url>
56+
$ glci sav --token <gitlab-token> --url <gitlab-project-url>
4057
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
4158
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
4259
Completed setting variables on Gitlab CI.
4360
```
4461

45-
By default, all existing variables on Gitlab CI will be overridden. If you wish to ignore existing variables, add a `--do-not-force` option, e.g:
62+
#### For all usages
63+
64+
##### `--do-not-force`
65+
66+
By default, existing variables on Gitlab CI will be overridden. If you wish to ignore existing variables, add a `--do-not-force` option, e.g:
4667

4768
```sh
48-
$ setAllVars --token <gitlab-token> --url <gitlab-project-url> --do-not-force
69+
$ glci sav --token <gitlab-token> --url <gitlab-project-url> --do-not-force
4970
Skipping AWS_CREDENTIALS, already set for gitlab-org/gitlab-ce.
5071
Skipping NPM_INSTALL_TOKEN, already set for gitlab-org/gitlab-ce.
5172
Completed setting variables on Gitlab CI.
5273
```
5374

54-
If your working directory is a git repostory of your project, the `--url` option can be omitted, e.g:
75+
##### `--url`
76+
77+
If your working directory is a git repository of your project, the `--url` option can be omitted, e.g:
5578

5679
```sh
57-
$ setAllVars --token <gitlab-token>
80+
$ glci sv --token <gitlab-token> --key <key> --value <value>
5881
No URL specified, using git remote `origin`.
59-
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
60-
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
61-
Completed setting variables on Gitlab CI.
82+
Set <key> = <value> for gitlab-org/gitlab-ce.
83+
Completed setting variable on Gitlab CI.
6284
```
6385

6486
> Omitting `--url` will derive the URL from the remote named `origin`.
6587
88+
##### `--token`
89+
6690
This project supports `.gitlabrc` files using [rc](https://www.npmjs.com/package/rc).
6791
If `--token` is not specified, this project can use a `.gitlabrc`, e.g:
6892

@@ -71,20 +95,40 @@ token = this-is-my-gitlab-token
7195
```
7296

7397
```sh
74-
$ setAllVars --url <gitlab-project-url>
98+
$ glci sav --url <gitlab-project-url>
7599
Using token from .gitlabrc.
76100
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
77101
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
78102
Completed setting variables on Gitlab CI.
79103
```
80104

81-
Essentially, if your project is a git repository, and you have `.gitlabrc` file,
82-
no options are required and this tool can be invoked simply as:
105+
Alternatively, you can also set a GITLAB_TOKEN environment variable:
106+
107+
```sh
108+
$ export GITLAB_TOKEN=this-is-my-gitlab-token
109+
$ glci sv --url <gitlab-project-url> --key <key> --value <value>
110+
Using token from environment variable GITLAB_TOKEN.
111+
Set <key> = <value> for gitlab-org/gitlab-ce.
112+
Completed setting variable on Gitlab CI.
113+
```
114+
115+
---
116+
117+
Essentially, if your project is a git repository, and you have a `.gitlabrc` file or a GITLAB_TOKEN env variable,
118+
this tool can be invoked simply as:
83119

84120
```sh
85-
$ setAllVars
121+
$ glci sav
122+
Using token from .gitlabrc.
86123
No URL specified, using git remote `origin`.
87124
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
88125
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
89126
Completed setting variables on Gitlab CI.
90127
```
128+
```sh
129+
$ glci sv --key <key> --value <value>
130+
Using token from environment variable GITLAB_TOKEN.
131+
No URL specified, using git remote `origin`.
132+
Set <key> = <value> for gitlab-org/gitlab-ce.
133+
Completed setting variable on Gitlab CI.
134+
```

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "CLI tool to allow setting multiple pipeline variables on Gitlab CI.",
55
"author": "Khoa Tran",
66
"contributors": [
7-
"Brendan Abbott <[email protected]>"
7+
"Brendan Abbott <[email protected]>",
8+
"Baptiste Redon <[email protected]>"
89
],
910
"license": "MIT",
1011
"keywords": [
@@ -16,9 +17,9 @@
1617
"url": "[email protected]:temando/gitlab-ci-variables-cli.git"
1718
},
1819
"bin": {
19-
"setAllVars": "bin/setAllVariables.js"
20+
"glci": "bin/glci.js"
2021
},
21-
"main": "bin/setAllVariables.js",
22+
"main": "bin/glci.js",
2223
"files": [
2324
"bin"
2425
],

src/glci-sav.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander';
4+
import { getConf, getProperties } from './lib/utils';
5+
import gitlabCI from './lib/gitlab-ci';
6+
7+
async function execute(cmd) {
8+
const conf = await getConf();
9+
10+
const properties = getProperties();
11+
const handler = gitlabCI(conf.url, conf.token);
12+
const resp = await handler.setVariables(properties, !cmd.doNotForce);
13+
14+
console.log('Completed setting variables on Gitlab CI.');
15+
return resp;
16+
}
17+
18+
program
19+
.description('Read all key/value pairs under gitlab.env.yml on the current directory and sets them as environment variables on Gitlab CI')
20+
.option(
21+
'--url <url>',
22+
'Your Gitlab project URL, e.g. https://gitlab.com/gitlab-org/gitlab-ce',
23+
)
24+
.option(
25+
'--token <token>',
26+
'Your Gitlab token.',
27+
)
28+
.option(
29+
'--do-not-force',
30+
'Ignore variables if they already exist on gitlab CI. By default all variables are overridden',
31+
)
32+
.parse(process.argv);
33+
34+
execute(program);

src/glci-sv.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander';
4+
import { getConf } from './lib/utils';
5+
import gitlabCI from './lib/gitlab-ci';
6+
7+
async function execute(cmd) {
8+
let resp = null;
9+
const conf = await getConf();
10+
11+
if (!conf.key || !conf.value) {
12+
console.error('No key/value pair given.');
13+
process.exit(1);
14+
}
15+
16+
const handler = gitlabCI(conf.url, conf.token);
17+
18+
const existingKeys = (await handler.listVariables()).map(variable => variable.key);
19+
20+
const keyExists = existingKeys.includes(conf.key);
21+
22+
if (keyExists && cmd.doNotForce) {
23+
console.log(`Skipping ${conf.key}, already set.`);
24+
return undefined;
25+
}
26+
27+
if (keyExists) {
28+
resp = await handler.updateVariable(conf.key, conf.value);
29+
} else {
30+
resp = await handler.createVariable(conf.key, conf.value);
31+
}
32+
33+
console.log('Completed setting variable on Gitlab CI.');
34+
return resp;
35+
}
36+
37+
program
38+
.description('Set given key/value pair as environment variables on Gitlab CI')
39+
.option(
40+
'--url <url>',
41+
'Your Gitlab project URL, e.g. https://gitlab.com/gitlab-org/gitlab-ce',
42+
)
43+
.option(
44+
'--token <token>',
45+
'Your Gitlab token.',
46+
)
47+
.option(
48+
'--key <key>',
49+
'Your Gitlab CI variable.',
50+
)
51+
.option(
52+
'--value <value>',
53+
'Your Gitlab CI value.',
54+
)
55+
.option(
56+
'--do-not-force',
57+
'Ignore variable if it already exists on gitlab CI. By default variable is overridden',
58+
)
59+
.parse(process.argv);
60+
61+
execute(program);

src/glci.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander';
4+
5+
program
6+
.version('0.1.0')
7+
.command('sv', 'set a variable')
8+
.command('sav', 'set all variables')
9+
.parse(process.argv);
10+
11+
12+
if (!process.argv.slice(1).length) {
13+
program.outputHelp();
14+
}

src/lib/gitlab-ci.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ export default function gitlabCI(url, token) {
3737
}
3838

3939
/**
40-
* Set project variable
40+
* Create project variable
4141
*
4242
* @param key
4343
* @param value
4444
*
4545
* @return {Promise<Object>} variable object
4646
*/
47-
async function setVariable(key, value) {
47+
async function createVariable(key, value) {
4848
const response = await axios({
4949
method: 'post',
5050
url: `${apiUrl}?${tokenQueryString}`,
@@ -107,7 +107,7 @@ export default function gitlabCI(url, token) {
107107

108108
const promises = keysToSet.map(async (key) => {
109109
const value = properties[key];
110-
const keyExists = existingKeys.some(existingKey => existingKey === key);
110+
const keyExists = existingKeys.includes(key);
111111

112112
if (keyExists && !forceUpdate) {
113113
console.log(`Skipping ${key}, already set for ${projectId}.`);
@@ -120,7 +120,7 @@ export default function gitlabCI(url, token) {
120120
variable = await updateVariable(key, value);
121121
} else {
122122
// Create variable
123-
variable = await setVariable(key, value);
123+
variable = await createVariable(key, value);
124124
}
125125

126126
console.log(`Set ${key} = ${JSON.stringify(value)} for ${projectId}`);
@@ -133,7 +133,7 @@ export default function gitlabCI(url, token) {
133133
}
134134

135135
return {
136-
setVariable,
136+
createVariable,
137137
updateVariable,
138138
listVariables,
139139
setVariables,

src/lib/utils.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import rc from 'rc';
2+
import fs from 'fs';
3+
import gitRemoteOriginUrl from 'git-remote-origin-url';
4+
import getUrlFromGitRemote from './git';
5+
import loadPropertiesFile from './properties-file';
6+
7+
const gitlabEnvFileName = 'gitlab.env.yml';
8+
9+
async function getConf() {
10+
const conf = rc('gitlab');
11+
const errors = [];
12+
13+
// Check for token
14+
if (!conf.token) {
15+
if (!process.env.GITLAB_TOKEN) {
16+
errors.push('No Gitlab token given.');
17+
} else {
18+
conf.token = process.env.GITLAB_TOKEN;
19+
console.log('Using token from environment variable GITLAB_TOKEN.');
20+
}
21+
}
22+
23+
if (conf.config) {
24+
console.log(`Using token from ${conf.config}.`);
25+
}
26+
27+
// If there is no url provided, get it!
28+
if (!conf.url) {
29+
try {
30+
conf.url = await getUrlFromGitRemote(gitRemoteOriginUrl);
31+
console.log('No URL specified, using git remote `origin`.');
32+
} catch (err) {
33+
errors.push('No Gitlab project URL given.');
34+
}
35+
}
36+
37+
if (errors.length > 0) {
38+
console.error(errors.join('\n'));
39+
process.exit(1);
40+
}
41+
42+
return conf;
43+
}
44+
45+
function getProperties() {
46+
const cwd = process.cwd();
47+
const path = `${cwd}/${gitlabEnvFileName}`;
48+
49+
if (!fs.existsSync(path)) {
50+
console.error(`${gitlabEnvFileName} does not exist`);
51+
process.exit(1);
52+
}
53+
54+
return loadPropertiesFile(path);
55+
}
56+
57+
export {
58+
getConf,
59+
getProperties,
60+
};

0 commit comments

Comments
 (0)