jd_scripts/tencentscf.js

181 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-01-25 10:53:37 +08:00
// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
const tencentcloud = require("tencentcloud-sdk-nodejs");
const fs = require('fs')
2021-01-26 23:12:59 +08:00
const yaml = require('js-yaml');
process.env.action = 0;
2021-01-25 10:53:37 +08:00
const ScfClient = tencentcloud.scf.v20180416.Client;
const clientConfig = {
credential: {
secretId: process.env.TENCENT_SECRET_ID,
secretKey: process.env.TENCENT_SECRET_KEY,
},
2021-01-29 17:14:40 +08:00
region: process.env.TENCENT_REGION, // 区域参考https://cloud.tencent.com/document/product/583/17299
2021-01-25 10:53:37 +08:00
profile: {
httpProfile: {
endpoint: "scf.tencentcloudapi.com",
},
},
};
2021-01-26 23:12:59 +08:00
const sleep = ms => new Promise(res => setTimeout(res, ms));
!(async () => {
const client = new ScfClient(clientConfig);
2021-01-25 10:53:37 +08:00
2021-01-26 23:12:59 +08:00
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(`更新函数`)
// 更新代码,删除后重建
2021-01-26 23:12:59 +08:00
params = {
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
2021-01-26 23:12:59 +08:00
};
await client.DeleteFunction(params).then(
2021-01-26 23:12:59 +08:00
(data) => {
console.log(data);
},
(err) => {
console.error("error", err);
process.env.action ++;
2021-01-26 23:12:59 +08:00
}
);
await sleep(1000*50) // 等待50秒
}
console.log(`创建函数`)
let inputYML = ".github/workflows/deploy_tencent_scf.yml";
let obj = yaml.load(fs.readFileSync(inputYML, { encoding: "utf-8" }));
2021-01-26 23:12:59 +08:00
params = {
"Code": {
"ZipFile": contents_in_base64
},
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
"Runtime": "Nodejs12.16",
"Timeout": 900,
"Environment": {
"Variables": []
}
2021-01-26 23:12:59 +08:00
};
for (let key in obj.jobs.build.env) {
if (process.env[key].length > 0) {
params.Environment.Variables.push({
Key: key,
Value: process.env[key]
});
}
}
2021-01-26 23:12:59 +08:00
await client.CreateFunction(params).then(
(data) => {
console.log(data);
},
(err) => {
console.error("error", err);
process.env.action ++;
2021-01-26 23:12:59 +08:00
}
);
await sleep(1000*50) // 等待50秒
2021-01-26 23:12:59 +08:00
},
(err) => {
console.error("error", err);
process.env.action ++;
2021-01-26 23:12:59 +08:00
}
);
2021-01-25 22:02:44 +08:00
/* console.log(``)
2021-01-26 23:12:59 +08:00
// 更新环境变量
let inputYML = '.github/workflows/deploy_tencent_scf.yml';
let obj = yaml.load(fs.readFileSync(inputYML, {encoding: 'utf-8'}))
let vars = []
for(let key in obj.jobs.build.steps[3].env){
if(key!=='PATH' && process.env.hasOwnProperty(key))
vars.push({
"Key": key,
"Value": process.env[key]
})
2021-01-25 22:02:44 +08:00
}
2021-01-26 23:12:59 +08:00
console.log(`您一共填写了${vars.length}个环境变量`)
params = {
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
"Environment": {
"Variables": vars
}
};
await client.UpdateFunctionConfiguration(params).then(
(data) => {
console.log(data);
},
(err) => {
console.error("error", err);
}
);
let triggers = []
params = {
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
2021-01-25 22:02:44 +08:00
}
2021-01-26 23:12:59 +08:00
await client.ListTriggers(params).then(
2021-01-25 22:02:44 +08:00
(data) => {
console.log(data);
2021-01-26 23:12:59 +08:00
triggers = data.Triggers
2021-01-25 22:02:44 +08:00
},
(err) => {
console.error("error", err);
}
);
2021-01-26 23:12:59 +08:00
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);
}
);
}*/
2021-01-26 23:12:59 +08:00
// 更新触发器
console.log(`去更新触发器`)
let inputYML = 'serverless.yml';
let obj = yaml.load(fs.readFileSync(inputYML, {encoding: 'utf-8'}))
2021-01-26 23:12:59 +08:00
for(let vo of obj.inputs.events){
let param = {
"FunctionName": process.env.TENCENT_FUNCTION_NAME,
"TriggerName": vo.timer.parameters.name,
'Type' : "timer",
'TriggerDesc' : vo.timer.parameters.cronExpression,
'CustomArgument' : vo.timer.parameters.argument,
'Enable' : "OPEN",
}
await client.CreateTrigger(param).then(
(data) => {
console.log(data);
},
(err) => {
console.error("error", err);
process.env.action ++;
2021-01-26 23:12:59 +08:00
}
);
}
})()
.catch((e) => console.log(e))
.finally(async () => {
if (process.env.GITHUB_ACTIONS == "true") {
fs.writeFile('action.js', `var action = `+process.env.action+`;action > 0 ? require("./sendNotify").sendNotify("云函数部署异常!请重试","点击通知,登入后查看详情",{ url: process.env.GITHUB_SERVER_URL + "/" + process.env.GITHUB_REPOSITORY + "/actions/runs/" + process.env.GITHUB_RUN_ID + "?check_suite_focus=true" }): ""`,'utf8',function(error){
if(error){
console.log(error);
return false;
}
console.log('写入成功');
})
}
2021-01-26 23:12:59 +08:00
})