尝试使用 Svelte5 的 Stripe Webhook 来部署 Firebase 功能。我设定了秘密:
firebase functions:secrets:get STRIPE_SECRET_KEY
firebase functions:secrets:get STRIPE_WEBHOOK_SECRET
并在
functions/src/index.ts
中包含我的最少代码
import { initializeApp } from 'firebase-admin/app';
import { defineSecret } from 'firebase-functions/params';
import { onRequest } from 'firebase-functions/v2/https';
import Stripe from 'stripe';
// Initialize Firebase
initializeApp();
// Define secrets using defineSecret
const stripeSecretKey = defineSecret('STRIPE_SECRET_KEY');
const stripeWebhookSecret = defineSecret('STRIPE_WEBHOOK_SECRET');
// Simplified handleStripeWebhook function
export const handleStripeWebhook = onRequest(
{ secrets: [stripeSecretKey, stripeWebhookSecret], cors: true },
async (req, res) => {
const stripe = new Stripe(stripeSecretKey.value());
const sig = req.headers['stripe-signature'];
let event;
try {
const body = req.rawBody.toString();
event = stripe.webhooks.constructEvent(body, sig, stripeWebhookSecret.value());
} catch (err) {
console.error(`Webhook Error: ${(err as Error).message}`);
res.status(400).send(`Webhook Error: ${(err as Error).message}`);
return;
}
// Log the event for troubleshooting
console.log(`Received event: ${JSON.stringify(event)}`);
res.json({ received: true });
}
);
但是看到这个烦人的错误消息,它并没有真正告诉我任何事情
i functions: updating Node.js 18 (2nd Gen) function handleStripeWebhook(us-central1)...
Could not create or update Cloud Run service handlestripewebhook, Container Healthcheck failed. Revision 'handlestripewebhook-00012-qar' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.
Logs URL: https://console.cloud.google.com/logs/viewer?project=XXXX&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
Functions deploy had errors with the following functions:
handleStripeWebhook(XXXXX)
i functions: cleaning up build files...
⚠ functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/gcr/images/XXXX/us/gcf
Error: There was an error deploying functions
我尝试过删除node_modules并重新安装功能 - 似乎是唯一有答案的线程。我已经销毁并重新输入了我的秘密。目前没有任何效果。
编辑: 错误1
{
"textPayload": "Default STARTUP TCP probe failed 1 time consecutively for container \"worker\" on port 8080. The instance was not started.",
"insertId": "6758d8180000a03441679b15",
"resource": {
"type": "cloud_run_revision",
"labels": {
"revision_name": "handlestripewebhook-00012-qar",
"service_name": "handlestripewebhook",
"configuration_name": "handlestripewebhook",
"location": "REDACTED_LOCATION",
"project_id": "REDACTED_PROJECT_ID"
}
},
"timestamp": "2024-12-11T00:08:56.041012Z",
"severity": "ERROR",
"labels": {
"goog-managed-by": "cloudfunctions",
"instanceId": "REDACTED_INSTANCE_ID"
},
"logName": "projects/REDACTED_PROJECT_ID/logs/run.googleapis.com%2Fvarlog%2Fsystem",
"receiveTimestamp": "2024-12-11T00:08:56.295019255Z"
}
错误2
{
"protoPayload": {
"@type": "type.googleapis.com/google.cloud.audit.AuditLog",
"status": {
"code": 9,
"message": "Ready condition status changed to False for Revision handlestripewebhook-00012-qar with message: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.\n\nLogs URL: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22 \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start"
},
"serviceName": "run.googleapis.com",
"methodName": "/InternalServices.ReplaceInternalService",
"resourceName": "namespaces/PROJECT_ID/revisions/handlestripewebhook-00012-qar",
"response": {
"metadata": {
"name": "handlestripewebhook-00012-qar",
"namespace": "PROJECT_NAMESPACE",
"selfLink": "/apis/serving.knative.dev/v1/namespaces/PROJECT_NAMESPACE/revisions/handlestripewebhook-00012-qar",
"uid": "REDACTED_UID",
"resourceVersion": "REDACTED_VERSION",
"generation": 1,
"creationTimestamp": "2024-12-11T00:08:23.090699Z",
"labels": {
"serving.knative.dev/configuration": "handlestripewebhook",
"serving.knative.dev/configurationGeneration": "12",
"serving.knative.dev/service": "handlestripewebhook",
"serving.knative.dev/serviceUid": "REDACTED_UID",
"goog-managed-by": "cloudfunctions",
"goog-cloudfunctions-runtime": "nodejs18",
"cloud.googleapis.com/location": "us-central1",
"serving.knative.dev/route": "handlestripewebhook",
"run.googleapis.com/startupProbeType": "Default"
},
"annotations": {
"run.googleapis.com/client-name": "cloudfunctions",
"run.googleapis.com/startup-cpu-boost": "true",
"cloudfunctions.googleapis.com/trigger-type": "HTTP_TRIGGER",
"run.googleapis.com/secrets": "secret-REDACTED:projects/PROJECT_ID/secrets/STRIPE_WEBHOOK_SECRET,secret-REDACTED:projects/PROJECT_ID/secrets/STRIPE_SECRET_KEY",
"run.googleapis.com/build-source-location": "{\"worker\":\"gs://gcf-v2-sources-PROJECT_ID-us-central1/handleStripeWebhook/function-source.zip#REDACTED\"}",
"serving.knative.dev/creator": "[email protected]",
"run.googleapis.com/build-function-target": "{\"worker\":\"handleStripeWebhook\"}",
"run.googleapis.com/base-images": "{\"worker\":\"us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/nodejs18\"}",
"run.googleapis.com/build-id": "{\"worker\":\"REDACTED\"}",
"run.googleapis.com/launch-stage": "BETA",
"run.googleapis.com/operation-id": "REDACTED",
"autoscaling.knative.dev/maxScale": "100"
},
"ownerReferences": [
{
"kind": "Configuration",
"name": "handlestripewebhook",
"uid": "REDACTED_UID",
"apiVersion": "serving.knative.dev/v1",
"controller": true,
"blockOwnerDeletion": true
}
]
},
"apiVersion": "serving.knative.dev/v1",
"kind": "Revision",
"spec": {
"containerConcurrency": 80,
"timeoutSeconds": 60,
"serviceAccountName": "[email protected]",
"containers": [
{
"name": "worker",
"image": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED",
"ports": [
{
"name": "http1",
"containerPort": 8080
}
],
"env": [
{
"name": "BLA_SENDGRID_KEY",
"value": "REDACTED"
},
{
"name": "BLA_ENDGRID_TEMPLATE",
"value": "REDACTED"
},
{
"name": "FIREBASE_CONFIG",
"value": "{\"projectId\":\"PROJECT_ID\",\"storageBucket\":\"PROJECT_ID.firebasestorage.app\"}"
},
{
"name": "GCLOUD_PROJECT",
"value": "PROJECT_ID"
},
{
"name": "EVENTARC_CLOUD_EVENT_SOURCE",
"value": "projects/PROJECT_ID/locations/us-central1/services/handleStripeWebhook"
},
{
"name": "FUNCTION_TARGET",
"value": "handleStripeWebhook"
},
{
"name": "LOG_EXECUTION_ID",
"value": "true"
},
{
"name": "STRIPE_SECRET_KEY",
"valueFrom": {
"secretKeyRef": {
"key": "2",
"name": "secret-REDACTED"
}
}
},
{
"name": "STRIPE_WEBHOOK_SECRET",
"valueFrom": {
"secretKeyRef": {
"key": "2",
"name": "secret-REDACTED"
}
}
}
],
"resources": {
"limits": {
"cpu": "1",
"memory": "256Mi"
}
},
"startupProbe": {
"timeoutSeconds": 240,
"periodSeconds": 240,
"failureThreshold": 1,
"tcpSocket": {
"port": 8080
}
}
}
],
"runtimeClassName": "run.googleapis.com/linux-base-image-update"
},
"status": {
"observedGeneration": 1,
"conditions": [
{
"type": "Ready",
"status": "False",
"reason": "HealthCheckContainerError",
"message": "The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.\n\nLogs URL: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22 \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start",
"lastTransitionTime": "2024-12-11T00:08:56.042205Z"
},
{
"type": "ContainerHealthy",
"status": "False",
"reason": "HealthCheckContainerError",
"message": "The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.\n\nLogs URL: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22",
"lastTransitionTime": "2024-12-11T00:08:56.042205Z"
},
{
"type": "ContainerReady",
"status": "True",
"message": "Container image import completed in 5.970382s.",
"lastTransitionTime": "2024-12-11T00:08:29.856747Z"
},
{
"type": "ResourcesAvailable",
"status": "True",
"message": "Revision deployment finished in 23.73708s. Checking container health.",
"lastTransitionTime": "2024-12-11T00:08:53.590376Z"
},
{
"type": "Retry",
"status": "True",
"reason": "ImmediateRetry",
"message": "System will retry after 0:00:00 from lastTransitionTime for attempt 0.",
"lastTransitionTime": "2024-12-11T00:08:53.590376Z",
"severity": "Info"
}
],
"logUrl": "https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22",
"imageDigest": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED",
"containerStatuses": [
{
"name": "worker",
"imageDigest": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED"
}
]
},
"@type": "type.googleapis.com/google.cloud.run.v1.Revision"
}
},
"insertId": "umk9de28fhw",
"resource": {
"type": "cloud_run_revision",
"labels": {
"service_name": "handlestripewebhook",
"revision_name": "handlestripewebhook-00012-qar",
"project_id": "PROJECT_ID",
"location": "us-central1",
"configuration_name": "handlestripewebhook"
}
},
"timestamp": "2024-12-11T00:08:56.052959Z",
"severity": "ERROR",
"logName": "projects/PROJECT_ID/logs/cloudaudit.googleapis.com%2Fsystem_event",
"receiveTimestamp": "2024-12-11T00:08:56.435853493Z"
}
所以我犯了最大的错误 - 我通过
firebase deploy --only functions --debug
注意到我没有安装 stripe。所以 npm i stripe
在我的 functions
文件夹中,一切顺利。