node.jsgoogle-cloud-platformgoogle-cloud-build

Run cloud-build triggers with Node.js library


I have a manual regional cloudbuild trigger, that uses Cloudbuild repositories (2nd gen) to make connections with github. I also need to pass substitution variables to the trigger.

Following this example to achieve the same:
https://github.com/googleapis/google-cloud-node/blob/main/packages/google-devtools-cloudbuild/samples/generated/v1/cloud_build.run_build_trigger.js

As far as the repoSource goes (https://cloud.google.com/build/docs/api/reference/rest/v1/projects.locations.triggers/run), the API is trying to leverage cloud source repositories.

Is there a way I can use 2nd gen repositories with the API/library?

Just to try out, if I can at least run the trigger:

    const buildRequest = {
        projectId: projectId,
        triggerId: `projects/${projectId}/locations/${triggerLocation}/triggers/${triggerName}`
    }

    console.log("Build request:", buildRequest);

    try {
        const [operation] = await cloudBuildClient.runBuildTrigger(buildRequest);
        res.status(200).json({ message: 'Build started', operation });
    } catch (error) {
        console.error('Build initiation error:', error);
        res.status(500).json({ error: 'Failed to start build' });
    }

But, this gives me Build initiation error: Error: 5 NOT_FOUND: Requested entity was not found.


Solution

  • Apparently, I was overcomplicating it with the build request Also, need to provide triggerId instead of trigger name

    The updated buildRequest:

    const buildRequest = {
        name: `projects/${projectId}/locations/${triggerLocation}/triggers/${triggerId}`,
        source: {
            substitutions: buildSubstitutions
        }
    }
    

    Good idea to do a getBuildTrigger if facing a similar issue:

    const { CloudBuildClient } = require('@google-cloud/cloudbuild');
    const cloudBuildClient = new CloudBuildClient();
    
    const request = {
       name: `projects/${projectId}/locations/${triggerLocation}/triggers/${triggerId}`
    };
    
    const response = await cloudBuildClient.getBuildTrigger(request);
    console.log(response);