Skip to content

Commit

Permalink
feat: support faas warm and invoke command (#25)
Browse files Browse the repository at this point in the history
* test: add nuxtjs test

* feat: support faas warm and invoke command

* fix: divide command module init

* fix: optimize warm up options
  • Loading branch information
yugasun authored Oct 22, 2020
1 parent df0b955 commit 40ac01a
Show file tree
Hide file tree
Showing 22 changed files with 530 additions and 59 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ TENCENT_SECRET_KEY=xxx

# cos url for project code download in CI environment
CODE_URL_COS=xxx
# git ulr for git project
CODE_URL_GIT=xxx

# nextjs
CODE_URL_COS_NEXTJS=xxx
STATIC_URL_NEXTJS=xxx

# nuxtjs
CODE_URL_COS_NUXTJS=xxx
STATIC_URL_NUXTJS=xxx
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ jobs:
TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }}
CODE_URL_COS: https://test-1251556596.cos.ap-guangzhou.myqcloud.com/express-demo.zip
CODE_URL_GIT: https://github.com/serverless-plus/express-demo.git
CODE_URL_COS_SSR: https://test-1251556596.cos.ap-guangzhou.myqcloud.com/nextjs-demo-pure.zip
STATIC_URL: https://cli-nextjs-test-1251556596.cos.ap-guangzhou.myqcloud.com
CODE_URL_COS_NEXTJS: https://test-1251556596.cos.ap-guangzhou.myqcloud.com/nextjs-demo-pure.zip
STATIC_URL_NEXTJS: https://cli-nextjs-test-1251556596.cos.ap-guangzhou.myqcloud.com
CODE_URL_COS_NUXTJS: https://test-1251556596.cos.ap-guangzhou.myqcloud.com/nuxtjs-demo-pure.zip
STATIC_URL_NUXTJS: https://cli-nuxtjs-test-1251556596.cos.ap-guangzhou.myqcloud.com
76 changes: 72 additions & 4 deletions __tests__/ci/ci.ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe('Coding CI', () => {
projectId = req.ProjectId;
});

it('[createCodingCIJob] should create project job success', async () => {
it('[createCodingCIJob using nextjs] should create project job success', async () => {
const req = await ci.createCodingCIJob({
jobName: 'slsplus-cli-test-ssr',
jobName: 'slsplus-cli-test-nextjs',
projectId,
envs: credentialEnvs,
parseOptions: {
Expand Down Expand Up @@ -71,6 +71,7 @@ describe('Coding CI', () => {
},
needDeployLayer: true,
needBuild: true,
warmUp: true,
});
expect(req).toEqual({
Data: {
Expand All @@ -88,12 +89,79 @@ describe('Coding CI', () => {
envs: [
{
Name: 'CODE_URL',
Value: process.env.CODE_URL_COS_SSR as string,
Value: process.env.CODE_URL_COS_NEXTJS as string,
Sensitive: false,
},
{
Name: 'STATIC_URL',
Value: process.env.STATIC_URL as string,
Value: process.env.STATIC_URL_NEXTJS as string,
Sensitive: false,
},
],
});
expect(typeof req.RequestId).toBe('string');
expect(typeof req.Data.Build.Id).toBe('number');
expect(req.Data.Build.JobId).toBe(cosJobId);

buildId = req.Data.Build.Id;
});

it('[createCodingCIJob using nuxtjs] should create project job success', async () => {
const req = await ci.createCodingCIJob({
jobName: 'slsplus-cli-test-nuxtjs',
projectId,
envs: credentialEnvs,
parseOptions: {
slsOptions: {
org: 'orgDemo',
app: 'appDemo',
stage: 'dev',
component: 'nuxtjs',
name: 'nuxtjsDemo',
inputs: {
src: { src: './', exclude: ['.env', 'node_modules/**'] },
apigatewayConf: { protocols: ['http', 'https'] },
staticConf: {
cosConf: {
replace: true,
bucket: 'cli-nuxtjs-test',
},
},
},
},
layerOptions: {
org: 'orgDemo',
app: 'appDemo',
name: 'nuxtjsDemo-layer',
stage: 'dev',
runtime: 'Nodejs10.15',
},
},
needDeployLayer: true,
needBuild: true,
});
expect(req).toEqual({
Data: {
Id: expect.any(Number),
},
RequestId: expect.any(String),
});

cosJobId = req.Data.Id;
});

it('[triggerCodingCIBuild] should trigger ci build success', async () => {
const req = await ci.triggerCodingCIBuild({
jobId: cosJobId,
envs: [
{
Name: 'CODE_URL',
Value: process.env.CODE_URL_COS_NUXTJS as string,
Sensitive: false,
},
{
Name: 'STATIC_URL',
Value: process.env.STATIC_URL_NUXTJS as string,
Sensitive: false,
},
],
Expand Down
1 change: 1 addition & 0 deletions bin/slsplus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node

require('dotenv').config()
require('../dist/src/cli')
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const config = {
testTimeout: 60000,
testEnvironment: 'node',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$',
// testRegex: '(/__tests__/ci/ci.ssr\.(test|spec))\\.(js|ts)$',
testPathIgnorePatterns: ['/node_modules/','/dist/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@ygkit/request": "^0.1.1",
"chalk": "^4.1.0",
"commander": "^6.1.0",
"dotenv": "^8.2.0",
"fs-extra": "^9.0.1",
"js-yaml": "^3.14.0",
"ora": "^5.1.0",
Expand All @@ -78,7 +79,6 @@
"@types/jest": "^26.0.13",
"@typescript-eslint/eslint-plugin": "^4.1.0",
"@typescript-eslint/parser": "^4.1.0",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
Expand Down
8 changes: 4 additions & 4 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ function cleanEmptyValue(obj: ObjectInstance) {
}

async function request(capi: Capi, options: RequestOptions): Promise<any | ApiError> {
const { ServiceType } = capi.options;
const typePrefix = `API_${ServiceType.toUpperCase()}_`;
try {
const reqData = cleanEmptyValue(options) as RequestData;
const res = await capi.request(reqData, {
isV3: false,
debug: false,
host: 'coding.tencentcloudapi.com',
RequestClient: 'slsplus_coding',
});
const { Response } = res;
if (Response && Response.Error && Response.Error.Code) {
throw new ApiError({
type: `API_CODING_${options.Action}`,
type: `${typePrefix}${options.Action}`,
message: `${Response.Error.Message} (reqId: ${Response.RequestId})`,
reqId: Response.RequestId,
code: Response.Error.Code,
Expand All @@ -46,7 +46,7 @@ async function request(capi: Capi, options: RequestOptions): Promise<any | ApiEr
return Response;
} catch (e) {
throw new ApiError({
type: `API_CODING__${options.Action}`,
type: `${typePrefix}${options.Action}`,
message: e.message,
stack: e.stack,
reqId: e.reqId,
Expand Down
12 changes: 11 additions & 1 deletion src/ci/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ function createCodingCIJobReq({
depotId,
envs = [],
pipeline,
useCITempAuth = false,
parseOptions,
useCITempAuth = false,
needDeployLayer = false,
needBuild = false,
needInstallSls = true,
useGit = false,
gitBranch = 'master',
warmUp = false,
}: CreateCodingCIJobOptions): CreateCodingCIJobRequest {
if (!pipeline) {
pipeline = new Pipeline();
Expand Down Expand Up @@ -152,6 +153,15 @@ function createCodingCIJobReq({
}
// 6.2 deploy project
steps.addShell('serverless deploy --debug');

if (warmUp && parseOptions?.slsOptions) {
const { slsOptions } = parseOptions;
stage = stages.addStage('Warming up serverless project');
steps = stage.addSteps();
steps.addShell(
`slsplus faas warm-app --app=${slsOptions.app} --stage=${slsOptions.stage} --name=${slsOptions.name}`,
);
}
} else {
if (!(pipeline instanceof Pipeline)) {
throw new Error('[PARAMETER_ERROR] pipeline should be instance of Pipeline');
Expand Down
7 changes: 4 additions & 3 deletions src/ci/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Capi } from '@tencent-sdk/capi';
import {
Credential,
CodingCIInterface,
CreateCodingCIJobResponse,
CreateProjectWithTemplateResponse,
TriggerCodingCIBuildResponse,
DescribeCodingCIBuildResponse,
DescribeCodingCIBuildStageResponse,
DescribeCodingCIBuildLogResponse,
CodingCIOptions,
CreateProjectWithTemplateOptions,
CreateCodingCIJobOptions,
TriggerCodingCIBuildOptions,
Project,
} from '../typings/ci';
} from '../typings';
import {
createProjectWithTemplateReq,
createCodingCIJobReq,
Expand All @@ -26,13 +26,14 @@ import { request } from '../apis';

class CodingCI implements CodingCIInterface {
capi: Capi;
constructor({ secretId, secretKey, token, region = 'ap-guangzhou' }: CodingCIOptions) {
constructor({ secretId, secretKey, token, region = 'ap-guangzhou' }: Credential) {
this.capi = new Capi({
SecretId: secretId,
SecretKey: secretKey,
Token: token,
Region: region,
ServiceType: 'coding',
RequestClient: 'slsplus_coding',
Version: '2019-10-21',
SignatureMethod: 'sha256',
});
Expand Down
12 changes: 12 additions & 0 deletions src/cli/clone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn } from 'child_process';
import ora from 'ora';
import { program } from 'commander';

export function clone(source: string, dest: string): void {
const spinner = ora().start('Start cloning git project...\n');
Expand All @@ -22,3 +23,14 @@ export function clone(source: string, dest: string): void {
}
});
}

const cloneCommand = (): void => {
program
.command('clone <source> [destination]')
.description('clone a repository into a newly created directory')
.action((source, destination) => {
clone(source, destination);
});
};

export { cloneCommand };
Loading

0 comments on commit 40ac01a

Please sign in to comment.