chagan
chagan

Reputation: 239

GCP cloud build VIEW RAW logs link

I have written a small cloud function in GCP which is subscribed to Pub/Sub event. When any cloud builds triggered function post message into the slack channel over webook.

In response, we get lots of details to trigger name, branch name, variables details but i am more interested in Build logs URL.

Currently getting build logs URL in response is like : logUrl: https://console.cloud.google.com/cloud-build/builds/899-08sdf-4412b-e3-bd52872?project=125205252525252

which requires GCP console access to check logs.

While in the console there an option View Raw. Is it possible to get that direct URL in the event response? so that i can directly sent it to slack and anyone can access direct logs without having GCP console access.

Upvotes: 1

Views: 1197

Answers (2)

Harsh Manvar
Harsh Manvar

Reputation: 30208

as @guillaume blaquiere helped.

Just sharing the piece of code used in cloud function to generate the singedURL of cloud build logs.

var filename ='log-' + build.id + '.txt';
    var file = gcs.bucket(BUCKET_NAME).file(filename);
    const getURL = async () => {
  return new Promise((resolve, reject) => {
    file.getSignedUrl({
      action: 'read',
      expires: Date.now() + 76000000
    }, (err, url) => {
      if (err) {
        console.error(err);
        reject(err);
      }
      console.log("URL");
      resolve(url);
    });
  })
}
    const singedUrl = await getURL();

if anyone looking for the whole code please follow this link : https://github.com/harsh4870/Cloud-build-slack-notification/blob/master/singedURL.js

Upvotes: 0

guillaume blaquiere
guillaume blaquiere

Reputation: 76073

In your Cloud Build event message, you need to extract 2 values from the JSON message:

  • logsBucket
  • id

The raw file is stored here

<logsBucket>/log-<id>.txt

So, you can get it easily in your function with Cloud Storage client library (preferred solution) or with a simple HTTP Get call to the storage API.

If you need more guidance, let me know your dev language, I will send you a piece of code.

Upvotes: 3

Related Questions