Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create/Update Triggers with gcloud #99

Closed
DocBradfordSoftware opened this issue Jul 20, 2017 · 19 comments
Closed

Create/Update Triggers with gcloud #99

DocBradfordSoftware opened this issue Jul 20, 2017 · 19 comments
Assignees

Comments

@DocBradfordSoftware
Copy link

I wasn't sure where to post this issue, but it would be nice to have a command line way to create triggers. I have a lot of microservices and it takes a lot of effort to add triggers via the UI.

@skelterjohn
Copy link
Contributor

Thank you for the feedback.

There are two aspects for creating triggers - one could be in a CLI (and I'll show you how to do it with curl etc), and one cannot.

The one we cannot do outside of the UI is the synchronization of a github or bitbucket repo with your Google cloud source repo. If this part is the annoyance for you, then I apologize but for now you must use the UI.

Once the repo is sychronized, you can create triggers using curl as so:

Make the request file request.json, which can look like so:

{
  "trigger_template": {
    "repoName": "$YOUR_REPO_NAME",
    "branchName": "$SOME_BRANCH"
  },
  "filename": "$PATH_TO_CONFIG_FILE"
}

$PATH_TO_CONFIG_FILE is the path to your cloudbuild.yaml configuration, relative to the repo root. You can also use tagName instead of branchName. The value for these fields is a regular expression.

Then make the request:

curl -XPOST -T request.json -H"Authorization: Bearer $(gcloud config config-helper --format='value(credential.access_token)')" https://cloudbuild.googleapis.com/v1/projects/$YOUR_PROJECT_ID/triggers

Let me know if this works out for you.

@DocBradfordSoftware
Copy link
Author

@skelterjohn, thanks for the reply, I understand your answer, It's just that, by deciding to use cloud-builder instead of say, Jenkins. This would be the one part of the infrastructure, that would never be able to be automated. Feel free to close this issue, if you don't think it is a future option.

@skelterjohn
Copy link
Contributor

I understand. I'll leave this issue open - I'd certainly like to see this entire process work within the CLI.

@rimusz
Copy link

rimusz commented Jul 20, 2017

cli support for triggers would be awesome to have :)

@errordeveloper
Copy link

@skelterjohn I've used the instructions here but got things a little bit confused...

I've tried to create triggers using curl like you showed first, and API happily accepted my requests, however, when I load the console, it asked me to enable the API first, which I did... but after that, I've not been able to create a working trigger. Whatever I do, I get "caller does not have permission".

Here is a trigger I've created using the UI:

ilya@dx-training:~$ curl --header "${auth}" https://cloudbuild.googleapis.com/v1/projects/dx-training/triggers
{
  "triggers": [
    {
      "id": "ec49960d-0e2c-4a99-bcee-f9a2275f504d",
      "createTime": "2018-04-13T12:26:35.054774968Z",
      "triggerTemplate": {
        "projectId": "dx-training",
        "repoName": "github-errordeveloper-k8s-podinfo",
        "branchName": ".*"
      },
      "filename": "cloudbuild.yaml"
    }
  ]
}

When I try to run it, this is what I get:

ilya@dx-training:~$ curl --request POST --header "${auth}" https://cloudbuild.googleapis.com/v1/projects/dx-training/triggers/ec49960d-0e2c-4a99-bcee-f9a2275f504d:run -d '{"projectId":"dx-training","repoName":"github-errordeveloper-k8s-po
dinfo","branchName":".*"}' --header 'Content-Type: application/json'
{
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}

I suspect it's because I tried creating triggers before enabling the API and something didn't get initialised correctly, or it could be to do with IAM setup in this project, and I don't know where to look.

I've also tried using gcloud container builds submit, and it actually worked, so not all is broken in my project, but I need to get triggers to work.

@imjasonh
Copy link
Contributor

@errordeveloper Thanks for reporting this! The underlying cause was an unfortunate bug in a recent migration, wherein newly created triggers since yesterday would not be directly runnable (they would still trigger). That issue has now been fixed and verified by one of our engineers. Can you try again and let me know if you're still seeing the error?

@errordeveloper
Copy link

@imjasonh thanks a lot! I can confirm my trigger works now.

@3lll
Copy link

3lll commented Apr 21, 2018

Please could somebody confirm if it is possible to view/use Cloud Source Repositories from other projects on the Container Builder Trigger page - I can't seem to get this to work, even though I'm sure I've given the correct permissions. There only seems to be an API for this as I can't find any info in the SDK.

Thanks

@Philmod
Copy link
Contributor

Philmod commented Apr 21, 2018

Hey @3lll , I don't think you can create a Build Trigger on a CSRepository from another GCP project.

If the source of the code is in GitHub/Bitbucket, you could just create a mirror in each project.

@3lll
Copy link

3lll commented Apr 22, 2018 via email

@alancmclean
Copy link

@skelterjohn Can this be accomplished via a Cloud Function?

@arinto
Copy link

arinto commented Sep 5, 2018

Any updates on this issue? 😇

@verdverm
Copy link

verdverm commented Sep 6, 2018

@arinto Especially since clicking the "Add trigger" button does not seem to be working in the UI today!

Would also love to get trigger creation for github repos into my automation.

https://status.cloud.google.com/incident/developers-console/18005

@irvifa
Copy link

irvifa commented Sep 18, 2018

Wondering if this will be included inside the gcloud builds command in the future. Most of the API already been included in the CLI except for the create and patch command. I wonder why 🤔

Also @alancmclean I think it's possible except for the how to save the credentials required to authenticate the request. I wonder if there's a way to keep the credentials decrypted and encrypted only during runtime.

Also can we use this for private repository?

  1. https://cloud.google.com/docs/authentication/
  2. https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.triggers/create
  3. https://cloud.google.com/sdk/gcloud/reference/builds/

@stubbi
Copy link

stubbi commented Nov 22, 2018

I am surprised it is not possible to set up triggers inside the cloudbuild.yaml and wonder if that could become some option in the future. Gitlab for example has a nice syntax for this using only and except:

  only:
    refs:
      - schedules@gitlab-org/gitlab-ce
      - schedules@gitlab-org/gitlab-ee
  except:
    - tags
    - /(^docs[\/-].*|.*-docs$)/

This way you can specify to trigger the pipeline on specific branches or tags. I think such an option would add a lot of value since I could have more of my infrastructure inside my source control.

@irvifa
Copy link

irvifa commented Dec 11, 2018

Just wondering if there's a REST API provided to mirror repository from Bitbucket/Github to Google Source Repository so that I can create the trigger using the REST API 🤔

@wlynch wlynch self-assigned this Jun 8, 2019
@wlynch
Copy link

wlynch commented Jun 8, 2019

Hi!

gcloud builds triggers is now available in the Alpha channel. Update is still currently missing, but should come in the near future.

See https://cloud.google.com/sdk/gcloud/reference/alpha/builds/triggers/ for documentation.

@MattGson
Copy link

MattGson commented Jun 10, 2019

@irvifa You can actually use the REST API for Bitbucket/Github without using GSR at all.
Took me a while to figure out but you just need to create one trigger with the GUI then the repo is linked.

The trick is that the repo name shown in the GUI is not what you need to put in the request body. I used Chrome dev tools to capture the request in the console and find the actual name given to my Bitbucket repo. Now I can create triggers using the REST API.

For example in the console the repo might be myteam/my-repo but the name given to it by GCP is bitbucket_myteam_my-repo. If you put in the wrong name it will treat it like a GSR repo instead and give a warning that the repo does not exist.

Edit: I looked into this further and it seems that GCP creates a linked Cloud Source Repo for each Bitbucket or Github repo you connect. To find the name of the linked CSR repo you can just view the console for CSR.

@bendory
Copy link
Member

bendory commented Jun 14, 2019

This GitHub issue tracker is intended for bugs with the officially supported builder images specifically.
Please report issues and feature requests regarding the GCB service to our public issue tracker at
https://issuetracker.google.com/issues/new?component=190802&template=1162743

@bendory bendory closed this as completed Jun 14, 2019
k0uki pushed a commit to OLTAInc/cloud-builders that referenced this issue Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests