Skip to content

Commit 792f1e7

Browse files
SAAS 1100 - Support ability to pass a one time codefresh.yaml for pipeline execution (#252)
1 parent 97de1bb commit 792f1e7

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

lib/interface/cli/commands/pipeline/run.cmd.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const Command = require('../../Command');
33
const _ = require('lodash');
44
const CFError = require('cf-errors');
55
const { prepareKeyValueFromCLIEnvOption, crudFilenameOption } = require('../../helpers/general');
6-
const ObjectID = require('mongodb').ObjectID;
76
const { workflow, pipeline, log } = require('../../../../logic').api;
87
const authManager = require('../../../../logic').auth.manager;
98
const Docker = require('dockerode');
9+
const { validatePipelineYaml } = require('../../helpers/validation');
10+
const { printResult } = require('../root/validate.cmd');
1011

1112
const regex = /##[0-9a-f]{24}##/i;
1213
const imageName = 'codefresh/engine:master';
@@ -81,6 +82,13 @@ const run = new Command({
8182
describe: 'Set build variables from a file',
8283
});
8384

85+
crudFilenameOption(yargs, {
86+
name: 'yaml',
87+
alias: 'y',
88+
raw: true,
89+
describe: 'Override codefresh.yaml for this execution',
90+
});
91+
8492
return yargs;
8593
},
8694
handler: async (argv) => {
@@ -92,6 +100,7 @@ const run = new Command({
92100
const resetVolume = argv['reset-volume'];
93101
const variablesFromFile = argv['var-file'];
94102
const contexts = argv['context'];
103+
const userYamlDescriptor = argv['yaml'];
95104
const local = argv.local;
96105

97106
try {
@@ -101,6 +110,12 @@ const run = new Command({
101110
message: `Passed pipeline id: ${pipelineName} does not exist`,
102111
});
103112
}
113+
114+
if (userYamlDescriptor) {
115+
const result = await validatePipelineYaml(undefined, userYamlDescriptor);
116+
printResult(result);
117+
}
118+
104119
if (local) {
105120
const docker = new Docker();
106121
docker.pull(imageName, (err, stream) => {
@@ -110,9 +125,17 @@ const run = new Command({
110125
console.log('\nDone pulling.');
111126
const currentContext = authManager.getCurrentContext();
112127
docker.run(imageName, [], [], {
113-
Env: [`ACCESS_TOKEN=${currentContext.token}`, `PIPELINE_ID=${pipelineName}`, `BRANCH=${branch}`, `CF_HOST=${currentContext.url}`, 'DOCKER_SOCKET_PATH=/var/run/docker.sock'],
128+
Env: [
129+
`ACCESS_TOKEN=${currentContext.token}`,
130+
`PIPELINE_ID=${pipelineName}`, `BRANCH=${branch}`,
131+
`CF_HOST=${currentContext.url}`,
132+
'DOCKER_SOCKET_PATH=/var/run/docker.sock',
133+
userYamlDescriptor && `OVERRIDE_WORKFLOW_YAML=${userYamlDescriptor}`,
134+
],
114135
Hostconfig: {
115-
Binds: ['/var/run/docker.sock:/var/run/docker.sock'],
136+
Binds: [
137+
'/var/run/docker.sock:/var/run/docker.sock',
138+
],
116139
},
117140
}, (err, data) => {
118141
if (err) {
@@ -149,6 +172,7 @@ const run = new Command({
149172
branch,
150173
sha,
151174
enableNotifications,
175+
userYamlDescriptor,
152176
},
153177
};
154178

lib/interface/cli/commands/root/validate.cmd.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { pathExists, watchFile } = require('../../helpers/general');
77

88
const VALID_MESSAGE = Style.green('Yaml is valid!');
99

10-
function _printResult(result) {
10+
function printResult(result) {
1111
console.log(result.valid ? VALID_MESSAGE : Style.red(result.message), '\n');
1212
}
1313

@@ -68,7 +68,7 @@ const validateCmd = new Command({
6868

6969
filenames.forEach(f => validatePipelineYaml(f).then((result) => {
7070
console.log(`Validation result for ${f}:`);
71-
_printResult(result);
71+
printResult(result);
7272
}));
7373
return;
7474
}
@@ -79,14 +79,15 @@ const validateCmd = new Command({
7979
watchFile(filename, async () => {
8080
console.log('File changed');
8181
const result = await validatePipelineYaml(filename);
82-
_printResult(result);
82+
printResult(result);
8383
});
8484
}
8585

8686
// even with --attach option validates file for first time
8787
const result = await validatePipelineYaml(filename);
88-
_printResult(result);
88+
printResult(result);
8989
},
9090
});
9191

9292
module.exports = validateCmd;
93+
validateCmd.printResult = printResult;

lib/interface/cli/helpers/general.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ const crudFilenameOption = (yargs, options = {}) => {
108108
.option(options.name || 'filename', filenameOption)
109109
.coerce(options.name || 'filename', (arg) => {
110110
try {
111+
const rawFile = fs.readFileSync(path.resolve(process.cwd(), arg), 'utf8');
111112
if (arg.endsWith('.json')) {
112-
return JSON.parse(fs.readFileSync(path.resolve(process.cwd(), arg), 'utf8'));
113+
return options.raw ? rawFile : JSON.parse();
113114
} else if (arg.endsWith('.yml') || arg.endsWith('yaml')) {
114-
return yaml.safeLoad(fs.readFileSync(path.resolve(process.cwd(), arg), 'utf8'));
115+
return options.raw ? rawFile : yaml.safeLoad();
115116
} else {
116117
throw new CFError('File extension is not recognized');
117118
}

lib/interface/cli/helpers/validation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ async function validatePipelineSpec(data) {
2424
return { valid: !!result.valid, message };
2525
}
2626

27-
async function validatePipelineYaml(filename) {
28-
const yamlContent = await readFile(filename, 'UTF-8');
27+
async function validatePipelineYaml(filename, fileContent) {
28+
const yamlContent = fileContent || await readFile(filename, 'UTF-8');
2929
const result = await pipeline.validateYaml(yamlContent);
3030
let message;
3131
if (!result.valid) {

lib/logic/api/pipeline.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ const runPipelineByName = async (name, data) => {
142142
body.sha = data.sha;
143143
}
144144

145+
if (data.userYamlDescriptor) {
146+
body.userYamlDescriptor = data.userYamlDescriptor;
147+
}
148+
145149
if (data.contexts) {
146150
let contexts = [];
147151
if (_.isString(data.contexts)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.9.8",
3+
"version": "0.9.9",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)