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.