Skip to content

Commit

Permalink
fix: support ci build option
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun authored and Yuga Sun committed Oct 20, 2020
1 parent 4155a91 commit 73f5cb0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
23 changes: 18 additions & 5 deletions __tests__/ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ describe('Coding CI', () => {
org: 'orgDemo',
app: 'appDemo',
stage: 'dev',
component: 'express',
name: 'expressDemo',
component: 'nextjs',
name: 'nextjsDemo',
inputs: {
src: './',
},
Expand All @@ -154,6 +154,7 @@ describe('Coding CI', () => {
},
},
needDeployLayer: true,
needBuild: true,
});

expect(req).toEqual({
Expand All @@ -164,7 +165,7 @@ describe('Coding CI', () => {
HookType: 'DEFAULT',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent:
"pipeline {\n agent any\n\n stages {\n stage(\"Initializing node.js environment\") {\n steps {\n sh 'env'\n sh 'date'\n sh 'echo TENCENT_SECRET_ID=$TENCENT_SECRET_ID > .env'\n sh 'echo TENCENT_SECRET_KEY=$TENCENT_SECRET_KEY >> .env'\n sh 'echo TENCENT_TOKEN=$TENCENT_TOKEN >> .env'\n sh 'echo SERVERLESS_PLATFORM_VENDOR=tencent >> .env'\n sh 'echo SERVERLESS_PLATFORM_STAGE=$SERVERLESS_PLATFORM_STAGE >> .env'\n sh '''cat > npm.sh << EOF\r\n#! /bin/bash\r\nrootPath=\\\\`pwd\\\\`\r\nfunction read_dir(){\r\n for file in \\\\`ls \\\\$1\\\\`\r\n do\r\n if [ -d \\\\$1'/'\\\\$file ]; then\r\n if [ \\\\$file != 'node_modules' ]; then\r\n read_dir \\\\$1'/'\\\\$file\r\n fi\r\n else\r\n if [ \\\\$file = 'package.json' ]; then\r\n cd \\\\$1\r\n npm install\r\n cd \\\\$rootPath\r\n fi\r\n fi\r\n done\r\n}\r\nread_dir \\\\$1\r\nEOF'''\n sh 'cat npm.sh && ls -la'\n }\n }\n\n stage(\"Installing serverless cli\") {\n steps {\n sh 'npm config ls'\n sh 'npm set registry https://registry.npmjs.org/'\n sh 'npm install -g serverless'\n sh 'sls -v'\n }\n }\n\n stage(\"Downloading code\") {\n steps {\n sh 'wget $CODE_URL -O code.zip'\n sh 'ls -l && file code.zip'\n sh 'unzip -n code.zip && rm code.zip'\n }\n }\n\n stage(\"Installing dependencies\") {\n steps {\n sh 'chmod +x ./npm.sh && ./npm.sh `pwd` && rm npm.sh'\n }\n }\n\n stage(\"Processing serverless config files\") {\n steps {\n sh 'npm install -g @slsplus/cli'\n sh 'slsplus parse --output --auto-create --sls-options=\\'{\"org\":\"orgDemo\",\"app\":\"appDemo\",\"stage\":\"dev\",\"component\":\"express\",\"name\":\"expressDemo\",\"inputs\":{\"src\":\"./\"}}\\' && cat serverless.yml'\n sh 'slsplus parse --output --auto-create --layer-options=\\'{\"org\":\"orgDemo\",\"app\":\"appDemo\",\"stage\":\"dev\",\"runtime\":\"Nodejs10.15\"}\\' && cat layer/serverless.yml'\n }\n }\n\n stage(\"Deploying serverless project\") {\n steps {\n sh 'serverless deploy --debug --target=./layer'\n sh 'serverless deploy --debug'\n }\n }\n\n }\n}\n",
"pipeline {\n agent any\n\n stages {\n stage(\"Initializing node.js environment\") {\n steps {\n sh 'env'\n sh 'date'\n sh 'echo TENCENT_SECRET_ID=$TENCENT_SECRET_ID > .env'\n sh 'echo TENCENT_SECRET_KEY=$TENCENT_SECRET_KEY >> .env'\n sh 'echo TENCENT_TOKEN=$TENCENT_TOKEN >> .env'\n sh 'echo SERVERLESS_PLATFORM_VENDOR=tencent >> .env'\n sh 'echo SERVERLESS_PLATFORM_STAGE=$SERVERLESS_PLATFORM_STAGE >> .env'\n sh '''cat > npm.sh << EOF\r\n#! /bin/bash\r\nrootPath=\\\\`pwd\\\\`\r\nfunction read_dir(){\r\n for file in \\\\`ls \\\\$1\\\\`\r\n do\r\n if [ -d \\\\$1'/'\\\\$file ]; then\r\n if [ \\\\$file != 'node_modules' ]; then\r\n read_dir \\\\$1'/'\\\\$file\r\n fi\r\n else\r\n if [ \\\\$file = 'package.json' ]; then\r\n cd \\\\$1\r\n npm install\r\n cd \\\\$rootPath\r\n fi\r\n fi\r\n done\r\n}\r\nread_dir \\\\$1\r\nEOF'''\n sh 'cat npm.sh && ls -la'\n }\n }\n\n stage(\"Installing serverless cli\") {\n steps {\n sh 'npm config ls'\n sh 'npm set registry https://registry.npmjs.org/'\n sh 'npm install -g serverless'\n sh 'sls -v'\n }\n }\n\n stage(\"Downloading code\") {\n steps {\n sh 'wget $CODE_URL -O code.zip'\n sh 'ls -l && file code.zip'\n sh 'unzip -n code.zip && rm code.zip'\n }\n }\n\n stage(\"Installing dependencies\") {\n steps {\n sh 'rm -rf ./node_modules && chmod +x ./npm.sh && ./npm.sh `pwd` && rm npm.sh'\n }\n }\n\n stage(\"Building project\") {\n steps {\n sh 'npm run build'\n }\n }\n\n stage(\"Processing serverless config files\") {\n steps {\n sh 'npm install -g @slsplus/cli'\n sh 'slsplus parse --output --auto-create --sls-options=\\'{\"org\":\"orgDemo\",\"app\":\"appDemo\",\"stage\":\"dev\",\"component\":\"nextjs\",\"name\":\"nextjsDemo\",\"inputs\":{\"src\":\"./\"}}\\' && cat serverless.yml'\n sh 'slsplus parse --output --auto-create --layer-options=\\'{\"org\":\"orgDemo\",\"app\":\"appDemo\",\"stage\":\"dev\",\"runtime\":\"Nodejs10.15\"}\\' && cat layer/serverless.yml'\n }\n }\n\n stage(\"Deploying serverless project\") {\n steps {\n sh 'serverless deploy --debug --target=./layer'\n sh 'serverless deploy --debug'\n }\n }\n\n }\n}\n",
AutoCancelSameRevision: true,
AutoCancelSameMergeRequest: true,
TriggerRemind: 'ALWAYS',
Expand All @@ -185,11 +186,17 @@ describe('Coding CI', () => {
org: 'orgDemo',
app: 'appDemo',
stage: 'dev',
component: 'express',
name: 'expressDemo',
component: 'nextjs',
name: 'nextjsDemo',
inputs: {
src: { src: './', exclude: ['.env', 'node_modules/**'] },
apigatewayConf: { protocols: ['http', 'https'] },
staticConf: {
cosConf: {
replace: true,
bucket: 'cli-nextjs-test',
},
},
},
},
layerOptions: {
Expand All @@ -200,6 +207,7 @@ describe('Coding CI', () => {
},
},
needDeployLayer: true,
needBuild: true,
});
expect(req).toEqual({
Data: {
Expand Down Expand Up @@ -244,6 +252,11 @@ describe('Coding CI', () => {
Value: process.env.CODE_URL_COS as string,
Sensitive: false,
},
{
Name: 'STATIC_URL',
Value: process.env.STATIC_URL as string,
Sensitive: false,
},
],
});
expect(typeof req.RequestId).toBe('string');
Expand Down
4 changes: 2 additions & 2 deletions __tests__/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';
import { removeSync, outputFileSync } from 'fs-extra';
import { parse, readAndParse } from '../src/cli/parse';
import { fileExists } from '../src/utils';
import { fileExist } from '../src/utils';
import { AnyObject } from '../src/typings/index.d';
interface ConfigFileContent extends AnyObject {
yml: string;
Expand Down Expand Up @@ -258,7 +258,7 @@ inputs:
},
},
});
expect(fileExists(opFile)).toBe(true);
expect(fileExist(opFile)).toBe(true);
});
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"author": "yugasun",
"license": "MIT",
"dependencies": {
"@tencent-sdk/capi": "^1.1.4",
"@tencent-sdk/capi": "^1.1.5",
"@types/fs-extra": "^9.0.1",
"@types/js-yaml": "^3.12.5",
"@types/ora": "^3.2.0",
Expand Down
11 changes: 10 additions & 1 deletion src/ci/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function createCodingCIJobReq({
useCITempAuth = false,
parseOptions,
needDeployLayer = false,
needBuild = false,
needInstallSls = true,
useGit = false,
gitBranch = 'master',
Expand Down Expand Up @@ -109,7 +110,15 @@ function createCodingCIJobReq({
// 4. install project dependencies
stage = stages.addStage('Installing dependencies');
steps = stage.addSteps();
steps.addShell('chmod +x ./npm.sh && ./npm.sh `pwd` && rm npm.sh');
if (!needBuild) {
steps.addShell('chmod +x ./npm.sh && ./npm.sh `pwd` && rm npm.sh');
} else {
// 4.1 Build project
steps.addShell('rm -rf ./node_modules && chmod +x ./npm.sh && ./npm.sh `pwd` && rm npm.sh');
stage = stages.addStage('Building project');
steps = stage.addSteps();
steps.addShell('npm run build');
}

// 5. Processing serverless config files (optional)
// whether need parse serverless.yml to source values config
Expand Down
4 changes: 2 additions & 2 deletions src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fse from 'fs-extra';
import YAML from 'js-yaml';
import traverse from 'traverse';
import chalk from 'chalk';
import { fileExists, isJsonPath, isYamlPath, getFileExt } from '../utils';
import { fileExist, isJsonPath, isYamlPath, getFileExt } from '../utils';
import { AnyObject, ParseOptions } from '../typings';
import { getDefaultConfig } from '../components/config';
import { createLayerConfig } from '../components/layer';
Expand Down Expand Up @@ -141,7 +141,7 @@ export function parse({

// if serveless config file not exit and autoCreate is true
// try to create a default one
if (!fileExists(inputPath)) {
if (!fileExist(inputPath)) {
if (autoCreate) {
const defaultConfig = getDefaultConfig(component);
const defaultContent = YAML.dump(defaultConfig);
Expand Down
2 changes: 2 additions & 0 deletions src/typings/ci.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ export interface CreateCodingCIJobOptions {
useGit?: boolean;
// git 分支,使用时需要设置 useGit 为 true
gitBranch?: string;
// 是否需要编译项目
needBuild?: boolean;
}

export interface CreateCodingCIJobRequest {
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ const isYamlPath = (filePath: string): boolean =>
const isJsonPath = (filePath: string): boolean => endsWith('.json', filePath);

/**
* Checks if a file exists
* Checks if a file exist
*/
const fileExists = (filePath: string): boolean => {
const fileExist = (filePath: string): boolean => {
try {
const stats = fs.lstatSync(filePath);
return stats.isFile();
Expand All @@ -69,4 +69,4 @@ const getFileExt = (filePath: string): string => {
}
};

export { deepClone, isYamlPath, isJsonPath, fileExists, getType, mergeObject, getFileExt };
export { deepClone, isYamlPath, isJsonPath, fileExist, getType, mergeObject, getFileExt };

0 comments on commit 73f5cb0

Please sign in to comment.