Skip to content

Commit 383841d

Browse files
authored
Merge pull request #2 from brendo/make-token-read-from-gitlabrc
Make --token optional (through .gitlabrc file)
2 parents 5ab2567 + 05de96c commit 383841d

File tree

7 files changed

+81
-11
lines changed

7 files changed

+81
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
bin
33
gitlab.env.yml
44
.nyc_output
5+
.gitlabrc

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
99
### Changed
1010

1111
- The `--url` option became optional. When omitted, the plugin will infer the URL from the git remote.
12+
- The `--token` option became optional if a `.gitlabrc` file exists.
1213

1314
## [v1.0.10] - 2017-08-18
1415

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ npm install -g gitlab-ci-variables-setter-cli
1818

1919
## Usage
2020

21-
Put all required variable key/values on a properties file named `gitlab.env.yml`. For example:
21+
Put all required variable key/values on a properties file named `gitlab.env.yml`, e.g:
2222

2323
```yml
2424
AWS_CREDENTIALS: |
@@ -37,18 +37,54 @@ Run the following command from the directory that contains the properties file,
3737

3838
```sh
3939
$ setAllVars --token <gitlab-token> --url <gitlab-project-url>
40+
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
41+
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
42+
Completed setting variables on Gitlab CI.
4043
```
4144

42-
By default, all existing variables on Gitlab CI will be overridden. If you wish to ignore existing variables, add a `--do-not-force` option, i.e.
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:
4346

4447
```sh
4548
$ setAllVars --token <gitlab-token> --url <gitlab-project-url> --do-not-force
49+
Skipping AWS_CREDENTIALS, already set for gitlab-org/gitlab-ce.
50+
Skipping NPM_INSTALL_TOKEN, already set for gitlab-org/gitlab-ce.
51+
Completed setting variables on Gitlab CI.
4652
```
4753

48-
If your working directory is a git repostory of your project, the `--url` option can be omitted, i.e:
54+
If your working directory is a git repostory of your project, the `--url` option can be omitted, e.g:
4955

5056
```sh
5157
$ setAllVars --token <gitlab-token>
58+
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.
5262
```
5363

5464
> Omitting `--url` will derive the URL from the remote named `origin`.
65+
66+
This project supports `.gitlabrc` files using [rc](https://www.npmjs.com/package/rc).
67+
If `--token` is not specified, this project can use a `.gitlabrc`, e.g:
68+
69+
```ini
70+
token = this-is-my-gitlab-token
71+
```
72+
73+
```sh
74+
$ setAllVars --url <gitlab-project-url>
75+
Using token from .gitlabrc.
76+
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
77+
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
78+
Completed setting variables on Gitlab CI.
79+
```
80+
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:
83+
84+
```sh
85+
$ setAllVars
86+
No URL specified, using git remote `origin`.
87+
Set AWS_CREDENTIALS = <value> for gitlab-org/gitlab-ce.
88+
Set NPM_INSTALL_TOKEN = <value> for gitlab-org/gitlab-ce.
89+
Completed setting variables on Gitlab CI.
90+
```

package-lock.json

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "1.0.10",
44
"description": "CLI tool to allow setting multiple pipeline variables on Gitlab CI.",
55
"author": "Khoa Tran",
6+
"contributors": [
7+
"Brendan Abbott <brendan@bloodbone.ws>"
8+
],
69
"license": "MIT",
710
"keywords": [
811
"gitlab",
@@ -34,6 +37,7 @@
3437
"git-remote-origin-url": "^2.0.0",
3538
"git-url-parse": "^7.0.0",
3639
"js-yaml": "^3.9.1",
40+
"rc": "^1.2.1",
3741
"url-parse": "^1.1.9"
3842
},
3943
"devDependencies": {

src/setAllVariables.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import program from 'commander';
44
import fs from 'fs';
5+
import rc from 'rc';
56
import gitRemoteOriginUrl from 'git-remote-origin-url';
67
import getUrlFromGitRemote from './lib/git';
78
import gitlabCI from './lib/gitlab-ci';
@@ -10,15 +11,20 @@ import loadPropertiesFile from './lib/properties-file';
1011
const gitlabEnvFileName = 'gitlab.env.yml';
1112

1213
async function execute(cmd) {
14+
const conf = rc('gitlab');
1315
const errors = [];
1416

1517
// Check for token
16-
if (cmd.token === undefined) {
18+
if (!conf.token) {
1719
errors.push('No Gitlab token given.');
1820
}
1921

22+
if (conf.config) {
23+
console.log(`Using token from ${conf.config}.`);
24+
}
25+
2026
// If there is no url provided, get it!
21-
let url = cmd.url;
27+
let url = conf.url;
2228
if (!url) {
2329
try {
2430
url = await getUrlFromGitRemote(gitRemoteOriginUrl);
@@ -48,7 +54,7 @@ async function execute(cmd) {
4854
}
4955

5056
const properties = loadPropertiesFile(path);
51-
const handler = gitlabCI(url, cmd.token);
57+
const handler = gitlabCI(url, conf.token);
5258
await handler.setVariables(properties, forceUpdate);
5359

5460
console.log('Completed setting variables on Gitlab CI.');

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,8 +1787,8 @@ istanbul-lib-hook@^1.0.7:
17871787
append-transform "^0.4.0"
17881788

17891789
istanbul-lib-instrument@^1.7.4:
1790-
version "1.7.4"
1791-
resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.7.4.tgz#e9fd920e4767f3d19edc765e2d6b3f5ccbd0eea8"
1790+
version "1.7.5"
1791+
resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.7.5.tgz#adb596f8f0cb8b95e739206351a38a586af21b1e"
17921792
dependencies:
17931793
babel-generator "^6.18.0"
17941794
babel-template "^6.16.0"
@@ -2444,7 +2444,7 @@ randomatic@^1.1.3:
24442444
is-number "^3.0.0"
24452445
kind-of "^4.0.0"
24462446

2447-
rc@^1.1.7:
2447+
rc@^1.1.7, rc@^1.2.1:
24482448
version "1.2.1"
24492449
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.1.tgz#2e03e8e42ee450b8cb3dce65be1bf8974e1dfd95"
24502450
dependencies:

0 commit comments

Comments
 (0)