我正在尝试使用Google Cloud Build(包括替代产品)部署Google Cloud Function。
但是,我在构建时收到错误:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: Provided module can't be loaded.
Detailed stack trace: Error: Incoming webhook URL is required
我的cloudbuild.yaml文件包含:
steps:
# This step builds the container image.
- name: 'gcr.io/cloud-builders/gcloud'
id: Deploy
args: ['functions', 'deploy', 'subscribeSlack', '--trigger-topic', 'cloud-builds', '--runtime', 'nodejs10', '--set-env-vars', '"SLACK_WEBHOOK_URL=${_SLACK_WEBHOOK_URL}"', '--region', '${_REGION}']
env:
- 'SLACK_WEBHOOK_URL=${_SLACK_WEBHOOK_URL}'
而且,我已经通过Google Cloud Console在触发器中添加了替换项: screenshot of build trigger with substitutions filled in。
该函数本身基于Google文档中的the example:
const { IncomingWebhook } = require('@slack/webhook');
const url = process.env.SLACK_WEBHOOK_URL;
const webhook = new IncomingWebhook(url);
// subscribeSlack is the main function called by Cloud Functions.
module.exports.subscribeSlack = (pubSubEvent, context) => {
const build = eventToBuild(pubSubEvent.data);
// Skip if the current status is not in the status list.
// Add additional statuses to list if you'd like:
// QUEUED, WORKING, SUCCESS, FAILURE,
// INTERNAL_ERROR, TIMEOUT, CANCELLED
const status = ['SUCCESS', 'FAILURE', 'INTERNAL_ERROR', 'TIMEOUT'];
if (status.indexOf(build.status) === -1) {
return;
}
// Send message to Slack.
const message = createSlackMessage(build);
webhook.send(message);
};
// eventToBuild transforms pubsub event message to a build object.
const eventToBuild = (data) => {
return JSON.parse(Buffer.from(data, 'base64').toString());
}
// createSlackMessage creates a message from a build object.
const createSlackMessage = (build) => {
console.log(JSON.stringify(build));
const message = {
text: `Build \`${build.status}\``,
mrkdwn: true,
attachments: [
{
title: 'Build logs',
title_link: build.logUrl,
fields: [{
title: 'Status',
value: build.status
}]
}
]
};
if(build.source.repoSource.repoName !== undefined)
message.attachments[0].fields.push({ title: "Repo", value: build.source.repoSource.repoName });
if(build.finishTime !== undefined)
message.attachments[0].fields.push({ title: "Finished", value: (new Date(build.finishTime)).toLocaleString('en-GB', {timeZone: "Australia/Brisbane"}) });
return message;
}
该功能可以从gcloud
cli很好地部署,但是仅在使用Cloud Build时失败。
答案 0 :(得分:0)
我检查了cloudbuild.yaml,发现在 SLACK_WEBHOOK_URL = $ {__ SLACK_WEBHOOK_URL} 周围有双引号和单引号会产生此问题,取出双引号似乎可以解决此问题。
我附上了修改后的cloudbuild.yaml
steps:
# This step builds the container image.
- name: 'gcr.io/cloud-builders/gcloud'
id: Deploy
args: ['functions', 'deploy', 'subscribeSlack', '--trigger-topic', 'cloud-builds', '--runtime', 'nodejs10', '--set-env-vars', 'SLACK_WEBHOOK_URL=${_SLACK_WEBHOOK_URL}', '--region', '${_REGION}']
env:
- 'SLACK_WEBHOOK_URL=${_SLACK_WEBHOOK_URL}'