Added reading dockerfile from env var, code cleanup

This commit is contained in:
Jyotsna 2020-11-25 13:05:58 +05:30
parent 9fa045d9cf
commit 7ab8f38c56
3 changed files with 40 additions and 17 deletions

View File

@ -38,7 +38,7 @@ const getAllPodsMock = {
const getNamespaceMock = {
'code': 0,
'stdout': '{"apiVersion": "v1","kind": "Namespace","metadata": {"annotations": {"githubWorkflow_c11401b9d232942bac19cbc5bc32b42d": "{\'run\': \'202489005\',\'repository\': \'testUser/hello-kubernetes\',\'workflow\': \'workflow1\',\'jobName\': \'build-and-deploy\',\'createdBy\': \'testUser\',\'runUri\': \'https://github.com/testUser/hello-kubernetes/actions/runs/202489005\',\'commit\': \'currentCommit\',\'lastSuccessRunCommit\': \'lastCommit\',\'branch\': \'refs/heads/branch-rename\',\'deployTimestamp\': \'1597062957973\',\'filePathConfigs\': \'{}\',\'provider\': \'GitHub\'}","githubWorkflow_21fd7a597282ca5adc05ba99018b3706": "{\'run\': \'202504411\',\'repository\': \'testUser/hello-kubernetes\',\'workflow\': \'workflowMaster\',\'jobName\': \'build-and-deploy\',\'createdBy\': \'testUser\',\'runUri\': \'https://github.com/testUser/hello-kubernetes/actions/runs/202504411\',\'commit\': \'currentCommit1\',\'lastSuccessRunCommit\': \'NA\',\'branch\': \'refs/heads/master\',\'deployTimestamp\': \'1597063919873\',\'filePathConfigs\': \'{}\',\'provider\': \'GitHub\'}"}},"spec": {"finalizers": ["kubernetes"]},"status": {"phase": "Active"}}'
'stdout': '{"apiVersion": "v1","kind": "Namespace","metadata": {"annotations": {"githubWorkflow_c11401b9d232942bac19cbc5bc32b42d": "{\'run\': \'202489005\',\'repository\': \'testUser/hello-kubernetes\',\'workflow\': \'workflow1\',\'jobName\': \'build-and-deploy\',\'createdBy\': \'testUser\',\'runUri\': \'https://github.com/testUser/hello-kubernetes/actions/runs/202489005\',\'commit\': \'currentCommit\',\'lastSuccessRunCommit\': \'lastCommit\',\'branch\': \'refs/heads/branch-rename\',\'deployTimestamp\': \'1597062957973\',\'dockerfilePaths\': \'{}\',\'manifestsPaths\': \'[]\',\'helmChartPaths\': \'[]\',\'provider\': \'GitHub\'}","githubWorkflow_21fd7a597282ca5adc05ba99018b3706": "{\'run\': \'202504411\',\'repository\': \'testUser/hello-kubernetes\',\'workflow\': \'workflowMaster\',\'jobName\': \'build-and-deploy\',\'createdBy\': \'testUser\',\'runUri\': \'https://github.com/testUser/hello-kubernetes/actions/runs/202504411\',\'commit\': \'currentCommit1\',\'lastSuccessRunCommit\': \'NA\',\'branch\': \'refs/heads/master\',\'deployTimestamp\': \'1597063919873\',\'filePathConfigs\': \'{}\',\'provider\': \'GitHub\'}"}},"spec": {"finalizers": ["kubernetes"]},"status": {"phase": "Active"}}'
};
const getWorkflowsUrlResponse = {

View File

@ -150,14 +150,24 @@ function getFilePathsConfigs() {
filePathsConfig[MANIFEST_PATHS_KEY] = inputManifestFiles;
let helmChartPaths = (process.env.HELM_CHART_PATHS && process.env.HELM_CHART_PATHS.split('\n').filter(path => path != "")) || [];
filePathsConfig[HELM_CHART_KEY] = helmChartPaths;
//Fetch labels from each image
//Parsing dockerfile paths for images
let imageNames = core.getInput('images').split('\n');
let imageDockerfilePathList = [];
let imageDockerfilePathMap = {};
let pathKey, pathVal;
//Fetching from env var if available
let dockerfilePathsList = (process.env.DOCKERFILEPATHS && process.env.DOCKERFILEPATHS.split('\n')) || [];
dockerfilePathsList.forEach(path => {
if (path) {
pathKey = path.split(' ')[0];
pathVal = path.split(' ')[1];
imageDockerfilePathMap[pathKey] = pathVal;
}
});
//Fetching from image lable if available
for (const image of imageNames) {
let args = [image];
let resultObj;
let containerRegistryName = image;
let imageDockerfilePathObj = {};
try {
let usrname = process.env.CR_USERNAME || null;
let pwd = process.env.CR_PASSWORD || null;
@ -189,15 +199,17 @@ function getFilePathsConfigs() {
if (resultObj) {
resultObj = resultObj[0];
if ((resultObj.Config) && (resultObj.Config.Labels) && (resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY])) {
imageDockerfilePathObj[image] = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
pathVal = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
}
else {
imageDockerfilePathObj[image] = 'Not available';
pathVal = 'Not available';
}
if (!imageDockerfilePathMap[image]) { //If (image : someVal) does not exist from env var parsing then add
imageDockerfilePathMap[image] = pathVal;
}
imageDockerfilePathList.push(imageDockerfilePathObj);
}
}
filePathsConfig[DOCKERFILE_PATH_KEY] = imageDockerfilePathList;
filePathsConfig[DOCKERFILE_PATH_KEY] = imageDockerfilePathMap;
return Promise.resolve(filePathsConfig);
});
}

View File

@ -6,7 +6,6 @@ import { GitHubClient } from '../githubClient';
import { StatusCodes } from "./httpClient";
import * as exec from "./exec";
import * as inputParams from "../input-parameters";
import { Z_FILTERED } from 'zlib';
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
@ -144,16 +143,26 @@ export async function getFilePathsConfigs(): Promise<any> {
let helmChartPaths = (process.env.HELM_CHART_PATHS && process.env.HELM_CHART_PATHS.split('\n').filter(path => path != "")) || [];
filePathsConfig[HELM_CHART_KEY] = helmChartPaths;
//Fetch labels from each image
//Parsing dockerfile paths for images
let imageNames = core.getInput('images').split('\n');
let imageDockerfilePathList: any = [];
let imageDockerfilePathMap: any = {};
let pathKey: any, pathVal: any;
//Fetching from env var if available
let dockerfilePathsList: any[] = (process.env.DOCKERFILEPATHS && process.env.DOCKERFILEPATHS.split('\n')) || [];
dockerfilePathsList.forEach(path => {
if(path){
pathKey = path.split(' ')[0];
pathVal = path.split(' ')[1];
imageDockerfilePathMap[pathKey] = pathVal;
}
})
//Fetching from image lable if available
for(const image of imageNames){
let args: string[] = [image];
let resultObj: any;
let containerRegistryName = image;
let imageDockerfilePathObj: any = {};
try{
let usrname = process.env.CR_USERNAME || null;
@ -191,16 +200,18 @@ export async function getFilePathsConfigs(): Promise<any> {
if(resultObj){
resultObj = resultObj[0];
if((resultObj.Config) && (resultObj.Config.Labels) && (resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY])){
imageDockerfilePathObj[image] = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
pathVal = resultObj.Config.Labels[DOCKERFILE_PATH_LABEL_KEY];
}
else{
imageDockerfilePathObj[image] = 'Not available';
pathVal = 'Not available';
}
imageDockerfilePathList.push(imageDockerfilePathObj);
if(!imageDockerfilePathMap[image]){ //If (image : someVal) does not exist from env var parsing then add
imageDockerfilePathMap[image] = pathVal;
}
}
}
filePathsConfig[DOCKERFILE_PATH_KEY] = imageDockerfilePathList;
filePathsConfig[DOCKERFILE_PATH_KEY] = imageDockerfilePathMap;
return Promise.resolve(filePathsConfig);
}