使用Cloud Build API很可能做到这一点。这是一个使用Node.js 客户端库的简单示例。
exports.createDockerBuild = async (req, res) => {
const google = require('googleapis').google;
const cloudbuild = google.cloudbuild({version: 'v1'});
const client = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
const projectId = await google.auth.getProjectId();
const resource = {
"source": {
"storageSource": {
"bucket": "my-source-bucket",
"object": "my-nodejs-source.tar.gz"
}
},
"steps": [{
"name": "gcr.io/cloud-builders/docker",
"args": [
"build",
"-t",
"gcr.io/my-project-name/my-nodejs-image",
"standard-hello-world"
]
}],
"images": ["gcr.io/$PROJECT_ID/my-nodejs-image"]
};
const params = {projectId, resource, auth: client};
result= await cloudbuild.projects.builds.create(params);
res.status(200).send("200 - Build Submitted");
};
我的源代码在一个桶里,但你可以很容易地从一个 repo 中提取它。
请记住,您需要使用 Node.js 8 beta 运行时才能使异步内容正常工作。