diff --git a/__tests__/ci.test.ts b/__tests__/ci.test.ts index b843a98..587ba5d 100644 --- a/__tests__/ci.test.ts +++ b/__tests__/ci.test.ts @@ -140,8 +140,8 @@ describe('Coding CI', () => { org: 'orgDemo', app: 'appDemo', stage: 'dev', - component: 'express', - name: 'expressDemo', + component: 'nextjs', + name: 'nextjsDemo', inputs: { src: './', }, @@ -154,6 +154,7 @@ describe('Coding CI', () => { }, }, needDeployLayer: true, + needBuild: true, }); expect(req).toEqual({ @@ -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', @@ -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: { @@ -200,6 +207,7 @@ describe('Coding CI', () => { }, }, needDeployLayer: true, + needBuild: true, }); expect(req).toEqual({ Data: { @@ -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'); diff --git a/__tests__/parse.test.ts b/__tests__/parse.test.ts index 37f3a10..3545cfd 100644 --- a/__tests__/parse.test.ts +++ b/__tests__/parse.test.ts @@ -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; @@ -258,7 +258,7 @@ inputs: }, }, }); - expect(fileExists(opFile)).toBe(true); + expect(fileExist(opFile)).toBe(true); }); } }); diff --git a/package.json b/package.json index 1568b47..d6212b7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/ci/actions.ts b/src/ci/actions.ts index 4e30821..2d5cf78 100644 --- a/src/ci/actions.ts +++ b/src/ci/actions.ts @@ -51,6 +51,7 @@ function createCodingCIJobReq({ useCITempAuth = false, parseOptions, needDeployLayer = false, + needBuild = false, needInstallSls = true, useGit = false, gitBranch = 'master', @@ -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 diff --git a/src/cli/parse.ts b/src/cli/parse.ts index f3f2e14..0318a3e 100644 --- a/src/cli/parse.ts +++ b/src/cli/parse.ts @@ -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'; @@ -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); diff --git a/src/typings/ci.d.ts b/src/typings/ci.d.ts index f69681e..0d670fe 100644 --- a/src/typings/ci.d.ts +++ b/src/typings/ci.d.ts @@ -298,6 +298,8 @@ export interface CreateCodingCIJobOptions { useGit?: boolean; // git 分支,使用时需要设置 useGit 为 true gitBranch?: string; + // 是否需要编译项目 + needBuild?: boolean; } export interface CreateCodingCIJobRequest { diff --git a/src/utils.ts b/src/utils.ts index c299216..ebd83ee 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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(); @@ -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 };