Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

I want to execute Cloud Functions via Pub/Sub, when a specific Docker image is pushed to the Artifac

I have created a Pub/Sub topic resource named gcr according to the URL below, and set the topic to Cloud Functions trigger.

Now, CloudFunctions is executed when all images are pushed, deleted, or tag added. I want CloudFunctions to execute only when a specific image is pushed. How can I do this?
Solved Solved
0 2 3,229
1 ACCEPTED SOLUTION

The Google Cloud Pub/Sub topic gcr will receive events from the Artifact Registry for all image-related events, including pushing, deleting, or adding a tag. You can't limit the events sent to the Pub/Sub topic to only include push events for a specific image.

However, you can control this in your Google Cloud Function. When your Cloud Function receives a message from the Pub/Sub topic, it will include details about the event that triggered it. You can use this information to filter out events you're not interested in.

Here is an example of how you can handle this in your Google Cloud Function (Using Node.js):

exports.helloPubSub = (event, context) => {
const message = event.data
? Buffer.from(event.data, 'base64').toString()
: "No data!";

// Parse the message to a JSON object
const artifactData = JSON.parse(message);

// Check if the action is a PUSH action
if (artifactData.action !== "INSERT") {
console.log(`Skipping non-push event: ${artifactData.action}`);
return;
}

// Check if the event is for the specific image you're interested in
if (artifactData.resource.name !== "projects/_/locations/_/repositories/_/images/_") {
console.log(`Skipping event for different image: ${artifactData.resource.name}`);
return;
}

// TODO: Your code here...
};

In this code, artifactData.action will contain the type of action that occurred (INSERT, DELETE, or TAG_INSERT). artifactData.resource.name will contain the name of the image that the event is for. You should replace "projects/_/locations/_/repositories/_/images/_" with the name of the image you're interested in.

In this code, artifactData.action will contain the type of action that occurred (INSERT, DELETE, or TAG_INSERT). artifactData.resource.name will contain the name of the image that the event is for. You should replace "projects/_/locations/_/repositories/_/images/_" with the name of the image you're interested in.

This way, your Cloud Function will only execute its main logic when a push event occurs for the specific image you're interested in, and it will ignore all other events.

Please note that this will still result in your Cloud Function being triggered for all events, which could have cost implications depending on the number of events. However, the function will exit early and not perform any significant processing for events you're not interested in, which should limit the impact on cost and performance.

View solution in original post

2 REPLIES 2

The Google Cloud Pub/Sub topic gcr will receive events from the Artifact Registry for all image-related events, including pushing, deleting, or adding a tag. You can't limit the events sent to the Pub/Sub topic to only include push events for a specific image.

However, you can control this in your Google Cloud Function. When your Cloud Function receives a message from the Pub/Sub topic, it will include details about the event that triggered it. You can use this information to filter out events you're not interested in.

Here is an example of how you can handle this in your Google Cloud Function (Using Node.js):

exports.helloPubSub = (event, context) => {
const message = event.data
? Buffer.from(event.data, 'base64').toString()
: "No data!";

// Parse the message to a JSON object
const artifactData = JSON.parse(message);

// Check if the action is a PUSH action
if (artifactData.action !== "INSERT") {
console.log(`Skipping non-push event: ${artifactData.action}`);
return;
}

// Check if the event is for the specific image you're interested in
if (artifactData.resource.name !== "projects/_/locations/_/repositories/_/images/_") {
console.log(`Skipping event for different image: ${artifactData.resource.name}`);
return;
}

// TODO: Your code here...
};

In this code, artifactData.action will contain the type of action that occurred (INSERT, DELETE, or TAG_INSERT). artifactData.resource.name will contain the name of the image that the event is for. You should replace "projects/_/locations/_/repositories/_/images/_" with the name of the image you're interested in.

In this code, artifactData.action will contain the type of action that occurred (INSERT, DELETE, or TAG_INSERT). artifactData.resource.name will contain the name of the image that the event is for. You should replace "projects/_/locations/_/repositories/_/images/_" with the name of the image you're interested in.

This way, your Cloud Function will only execute its main logic when a push event occurs for the specific image you're interested in, and it will ignore all other events.

Please note that this will still result in your Cloud Function being triggered for all events, which could have cost implications depending on the number of events. However, the function will exit early and not perform any significant processing for events you're not interested in, which should limit the impact on cost and performance.

Hi ms4446!

Thanks for the quick reply.

> You can't limit the events sent to the Pub/Sub topic to only include push events for a specific image.
Yes, I understand.

Sorry, I didn't explain it well enough.
Cloud Build builds a Docker image and pushes it to the Artifact registry. We thought of running Cloud Functions on that trigger.
I was hoping that it would be triggered only when a specific image was pushed by Pub/Sub.

However, I found that this is not possible, and it seems that calling Cloud Functions from Cloud Build may be a solution.
Thanks for offering an alternative as well!