Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move env variabled to yaml #143

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ This will create an EFS (Elastic File System) and mount it on `/var/lib/jenkins`
#### Add environment variables
Users can add global level environment variables using configuration as code as follows:

Update the `envVarsFilePath` property in `ciSettings` to the file path containing all environment variables in the form of key:value pair. See [CIStackProps](./lib/ci-stack.ts) for details.
Update the `envVarsFilePath` property in `ciSettings` to the *yaml* file path containing all environment variables in the form of key:value pair. See [CIStackProps](./lib/ci-stack.ts) for details.

Example: See [env.txt](./test/data/env.txt)
Example: See [env.txt](./test/data/env.yaml)
```
envVarsFilePath = 'test/data/env.txt'
envVarsFilePath = 'test/data/env.yaml'
```

#### Assume role
Expand Down
9 changes: 3 additions & 6 deletions lib/compute/env-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFileSync } from 'fs';
import { load } from 'js-yaml';

/**
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -22,13 +23,9 @@ export class EnvConfig {
public static addEnvConfigToJenkinsYaml(yamlObject: any, envVarsFilePath: string): any {
const jenkinsYaml: any = yamlObject;
const envArray: Env[] = [];
const envFile: string = readFileSync(envVarsFilePath, 'utf-8');
const c = envFile.split('\n');
c.forEach((item) => {
const e = item.split(/:(.*)/s).map((element) => element.trim());
envArray.push(new Env(e[0], e[1]));
});
const envFile: any = load(readFileSync(envVarsFilePath, 'utf-8'));

Object.keys(envFile).forEach((item) => envArray.push(new Env(item, envFile[item])));
const newEnvVars: Env[] = envArray;

const envConfig: { [x: string]: any; } = {
Expand Down
8 changes: 3 additions & 5 deletions test/compute/env-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Env Config', () => {
const testYaml = 'test/data/test_env.yaml';
const jenkinsYaml = load(readFileSync(JenkinsMainNode.BASE_JENKINS_YAML_PATH, 'utf-8'));

const envVarConfig = EnvConfig.addEnvConfigToJenkinsYaml(jenkinsYaml, 'test/data/env.txt');
const envVarConfig = EnvConfig.addEnvConfigToJenkinsYaml(jenkinsYaml, 'test/data/env.yaml');
const newConfig = dump(envVarConfig);
writeFileSync(testYaml, newConfig, 'utf-8');

Expand All @@ -27,11 +27,9 @@ describe('Env Config', () => {
envVars: {
env: [
{ key: 's3Bucket', value: 'artifactBucket' },
{ key: 'account', value: '1234' },
{ key: 'isStaging', value: 'true' },
{ key: 'account', value: 1234 },
{ key: 'isStaging', value: true },
{ key: 'url', value: 'https://url.com' },
{ key: 'nospace', value: 'dummy' },
{ key: 'multiplespace', value: 'spaces' },
],
},
};
Expand Down
6 changes: 0 additions & 6 deletions test/data/env.txt

This file was deleted.

4 changes: 4 additions & 0 deletions test/data/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
s3Bucket: artifactBucket
account: 1234
isStaging: true
url: https://url.com
8 changes: 2 additions & 6 deletions test/data/test_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ jenkins:
- key: s3Bucket
value: artifactBucket
- key: account
value: '1234'
value: 1234
- key: isStaging
value: 'true'
value: true
- key: url
value: https://url.com
- key: nospace
value: dummy
- key: multiplespace
value: spaces
globalCredentialsConfiguration:
configuration:
providerFilter: none
Expand Down