更新云函数
This commit is contained in:
103
tencentscf.js
103
tencentscf.js
@@ -1,8 +1,7 @@
|
|||||||
// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
|
// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
|
||||||
const tencentcloud = require("tencentcloud-sdk-nodejs");
|
const tencentcloud = require("tencentcloud-sdk-nodejs");
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const file_buffer = fs.readFileSync('./myfile.zip');
|
const yaml = require('js-yaml');
|
||||||
const contents_in_base64 = file_buffer.toString('base64');
|
|
||||||
|
|
||||||
const ScfClient = tencentcloud.scf.v20180416.Client;
|
const ScfClient = tencentcloud.scf.v20180416.Client;
|
||||||
|
|
||||||
@@ -18,14 +17,25 @@ const clientConfig = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const sleep = ms => new Promise(res => setTimeout(res, ms));
|
||||||
|
!(async () => {
|
||||||
const client = new ScfClient(clientConfig);
|
const client = new ScfClient(clientConfig);
|
||||||
let params = {
|
|
||||||
|
let params
|
||||||
|
await client.ListFunctions({}).then(
|
||||||
|
async (data) => {
|
||||||
|
let func = data.Functions.filter(vo=>vo.FunctionName===process.env.TENCENT_FUNCTION_NAME)
|
||||||
|
const file_buffer = fs.readFileSync('./myfile.zip');
|
||||||
|
const contents_in_base64 = file_buffer.toString('base64');
|
||||||
|
if(func.length){
|
||||||
|
console.log(`函数已存在,去更新函数`)
|
||||||
|
// 更新代码
|
||||||
|
params = {
|
||||||
"Handler": "index.main_handler",
|
"Handler": "index.main_handler",
|
||||||
"FunctionName": process.env.TENCENT_FUNCTION_NAME, // 云函数程序名,例如 jd_scripts
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
"ZipFile": contents_in_base64
|
"ZipFile": contents_in_base64
|
||||||
};
|
};
|
||||||
client.UpdateFunctionCode(params).then(
|
await client.UpdateFunctionCode(params).then(
|
||||||
(data) => {
|
(data) => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
},
|
},
|
||||||
@@ -33,22 +43,51 @@ client.UpdateFunctionCode(params).then(
|
|||||||
console.error("error", err);
|
console.error("error", err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
} else{
|
||||||
|
console.log(`函数不存在,去创建函数`)
|
||||||
|
params = {
|
||||||
|
"Code": {
|
||||||
|
"ZipFile": contents_in_base64
|
||||||
|
},
|
||||||
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
|
"Runtime": "Nodejs12.16"
|
||||||
|
};
|
||||||
|
await client.CreateFunction(params).then(
|
||||||
|
(data) => {
|
||||||
|
console.log(data);
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.error("error", err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
await sleep(1000*100) // 等待100秒
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.error("error", err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(`更新环境变量`)
|
||||||
// 更新环境变量
|
// 更新环境变量
|
||||||
|
let inputYML = '.github/workflows/deploy_tencent_scf.yml';
|
||||||
|
let obj = yaml.load(fs.readFileSync(inputYML, {encoding: 'utf-8'}))
|
||||||
let vars = []
|
let vars = []
|
||||||
for(let key in process.env){
|
for(let key in obj.jobs.build.steps[3].env){
|
||||||
|
if(key!=='PATH' && process.env.hasOwnProperty(key))
|
||||||
vars.push({
|
vars.push({
|
||||||
"Key": key,
|
"Key": key,
|
||||||
"Value": process.env[key]
|
"Value": process.env[key]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
console.log(`您一共填写了${vars.length}个环境变量`)
|
||||||
params = {
|
params = {
|
||||||
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
"Environment": {
|
"Environment": {
|
||||||
"Variables": vars
|
"Variables": vars
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
client.UpdateFunctionConfiguration(params).then(
|
await client.UpdateFunctionConfiguration(params).then(
|
||||||
(data) => {
|
(data) => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
},
|
},
|
||||||
@@ -56,19 +95,48 @@ client.UpdateFunctionConfiguration(params).then(
|
|||||||
console.error("error", err);
|
console.error("error", err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
let triggers = []
|
||||||
|
params = {
|
||||||
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
|
}
|
||||||
|
await client.ListTriggers(params).then(
|
||||||
|
(data) => {
|
||||||
|
console.log(data);
|
||||||
|
triggers = data.Triggers
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.error("error", err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
for(let vo of triggers){
|
||||||
|
params = {
|
||||||
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
|
"Type": "timer",
|
||||||
|
"TriggerName": vo.TriggerName
|
||||||
|
}
|
||||||
|
await client.DeleteTrigger(params).then(
|
||||||
|
(data) => {
|
||||||
|
console.log(data);
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.error("error", err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
// 更新触发器
|
// 更新触发器
|
||||||
const inputYML = 'serverless.yml';
|
console.log(`去更新触发器`)
|
||||||
const yaml = require('js-yaml');
|
inputYML = 'serverless.yml';
|
||||||
const obj = yaml.load(fs.readFileSync(inputYML, {encoding: 'utf-8'}))
|
obj = yaml.load(fs.readFileSync(inputYML, {encoding: 'utf-8'}))
|
||||||
for(let vo of obj.inputs.events){
|
for(let vo of obj.inputs.events){
|
||||||
let param = {
|
let param = {
|
||||||
|
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
|
||||||
|
"TriggerName": vo.timer.parameters.name,
|
||||||
'Type' : "timer",
|
'Type' : "timer",
|
||||||
'TriggerDesc' : vo.timer.parameters.cronExpression,
|
'TriggerDesc' : vo.timer.parameters.cronExpression,
|
||||||
'CustomArgument' : vo.timer.parameters.argument,
|
'CustomArgument' : vo.timer.parameters.argument,
|
||||||
'Enable' : "true"
|
'Enable' : "OPEN",
|
||||||
}
|
}
|
||||||
client.CreateTrigger(param).then(
|
await client.CreateTrigger(param).then(
|
||||||
(data) => {
|
(data) => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
},
|
},
|
||||||
@@ -77,3 +145,8 @@ for(let vo of obj.inputs.events){
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
})()
|
||||||
|
.catch((e) => console.log(e))
|
||||||
|
.finally(async () => {
|
||||||
|
})
|
||||||
|
Reference in New Issue
Block a user