Google Cloud Build/Run trigger upon Pull Request on merge with specific branch - triggers

I'm trying to use a Google Cloud Build Trigger to trigger a Cloud Build and then deploy to Cloud Run upon a Pull Request to Github repo Branch. My console looks as follows:
My questions:
Is it possible to only trigger once the PR is approved or merged? Right now it triggers upon creation of the PR. I'd prefer to only build and deploy once my inevitable mistakes in the PR are corrected.
It seems to build the feature branch I'm attempting to merge, not the main. Am I misunderstanding what Base branch means? Is that not the branch that it should build once I merge to it?
Inline YAML from the trigger:
steps:
- name: gcr.io/cloud-builders/docker
args:
- build
- '--no-cache'
- '-t'
- '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
- .
- '-f'
- Dockerfile
id: Build
- name: gcr.io/cloud-builders/docker
args:
- push
- '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
id: Push
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
args:
- run
- services
- update
- $_SERVICE_NAME
- '--platform=managed'
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
- >-
--labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
- '--region=$_DEPLOY_REGION'
- '--quiet'
id: Deploy
entrypoint: gcloud
images:
- '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
options:
substitutionOption: ALLOW_LOOSE
substitutions:
_DEPLOY_REGION: europe-west1
_LABELS: gcb-trigger-id=c764048b-0347-4f67-8a6f-93a91f4b05af
_TRIGGER_ID: c764048b-0347-4f67-8a6f-93a91f4b05af
_GCR_HOSTNAME: eu.gcr.io
_PLATFORM: managed
_SERVICE_NAME: myservice
tags:
- gcp-cloud-build-deploy-cloud-run
- gcp-cloud-build-deploy-cloud-run-managed
- myservice

Make a trigger that triggers on the "Push to a branch" event
Set the branch to ^main$
That's pretty much it. Then whenever you merge a pull request it will trigger the build.

To answer your questions:
Is it possible to only trigger once the PR is approved or merged? Right now it triggers upon creation of the PR. I'd prefer to only build and deploy once my inevitable mistakes in the PR are corrected.
It is possible by using manual approvals. The user must have a Cloud Build Approver role in order to update a trigger to require or not require approval, meaning the user can approve or reject builds. You can check this documentation on gate builds on approval.
Another option is defining an organizational policy to control which external services can invoke build triggers. You can specify any number of allowed or denied values for your organization or project. You can check this documentation on gate builds on organizational policy.
Comment control must also be set to required so that builds will only be executed after an owner or collaborator comments /gcbrun so that builds won't be automatically executed by triggers. You can check the full steps here on creating a GitHub trigger.
It seems to build the feature branch I'm attempting to merge, not the main. Am I misunderstanding what Base branch means? Is that not the branch that it should build once I merge to it?
When you create a trigger, you will be asked to select a base branch (either main or any other branch that will be read after providing your GitHub repo). In my case, it listed two.
When you make changes in your repo and open a pull request, it will merge the changes from your head branch to your base branch (in this case your main).
You can check the full documentation on working with branches.

Related

How to run a custom command unique to a PR on merging a PR?

I am wondering if there is any way to do the following. Say I have an "open data" repo, which allows people to submit content. The repo saves all the data, and the changes to the structured JSON/YAML is reviewed in a PR. But then because I am in a serverless system (like Vercel), I need to upload the changes to the data to the production database, on merge of the branch. So there should be required a custom data migration in the PR, which runs when the PR is approved and merged.
How can that be accomplished? All I can imagine as a solution is having a special "code block" in markdown with some JSON config explaining what script to run for the data migration, and you add that marked code snippet as a comment to the PR, then parse the PR comments and figure out what script to run from that. But that would be of course (seemingly) a super hack, so is there a right way to do something like this?
The other option is to have to run the script/command manually after you merge the PR, but ideally there would be a more automatic way of doing it.
GitHub itself can run code on various events through actions. Actions are configured through YAML files in the directory .github/workflows in the repository. Some actions relative to a branch use the workflow files from that branch, while “global” actions use the workflow files from the default branch (typically called main, or master for older repositories).
For example, this workflow runs bin/update-production-database whenever the main branch is updated (whether from a pull request merge or by pushing directly):
name: Update database
on:
push:
branches:
- main
jobs:
update-database:
runs-on: ubuntu-latest
steps:
- run: bin/update-production-database
See more examples in Deploying with GitHub Actions. To pass the credentials needed to access the database, set up an encrypted secret.
To only run the job on a PR merge and not on other pushes, see Running your workflow when a pull request merges.
If you use Vercel (which I know nothing about), it claims it “automatically deploys your GitHub projects” so there may be a built-in solution there (either using actions so that the trigger comes from GitHub, or using some Vercel-owned server which polls GitHub).

Using CI triggers and PR build validation together: Prevent that build runs twice

I want to use both CI-triggers and PR build validation in Azure DevOps. The goal is that as long as no PR has been created (and published) for a feature/topic-branch, the CI triggers should ensure that the branch gets built (so that developers get early feedback). I configured the following in the Pipeline (yaml):
trigger:
branches:
include:
- chore/*
- feature/*
- fix/*
- refactor/*
paths:
include:
- frontend/*
...
This works well. I further configured PR build validation under branch policies. The problem is that two builds are triggered now: the CI build and the PR build. Since we often update PRs multiple times to fix issues found during the code review, building everything twice isn't really what we need.
Is there any way to configure that CI builds are only triggered if there's no PR build for the same push?
This is not possible. They are totally separated triggers not aware of each other.
But you can achiever your result in a slightly different way. If you have branch policy configured and you sleect there a build you can set pr: none in you yaml definiton. It will block PR build, leaving CI build as they are. And this CI build will be considered as condition for you branch policy check.
Selecting this checkbox you will get list of builds which you may select as required
My Ci trigger build
PR view:
If I understand the question correctly add:
trigger: none
In your CI pipeline. This will have the PR kick it off via branch policy. Your CD pipeline will be triggered when the merge into master happens.
If you are using the Azure DevOps Repo, then the PR build is controlled by branch policies, but the CI-trigger (in azure-pipeline.yaml) has nothing to do with this branch policy.
So if you want both build validation for PR and the CI build at the same time, then every time you push your changes to update a PR, duplicate pipeline builds would be unavoidable. It is a side-effect.

Github actions check_run is not called

I am playing with github actions and trying to do something whenever other check is completed. I see 0 runs for this action.
This action is available in master branch and I am opening PR to the master.
I've tried to capture events via webhooks and I receive events there just fine. Why is my action not working?
Code below:
name: on check run
on:
check_run:
types: [ completed, rerequested, completed, requested_action]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: ENV
run: env
- name: check out event
run: cat "$GITHUB_EVENT_PATH"
update
I also tried to have other checks in the repository (travis ci), the action is still not executed.
update 2
It turned out I was looking for different event that I needed. I confused status even with "check run" event. Travis ci in it's default setup produced "status" event, "check" api needs to be enabled separately
I think the issue might be that the GitHub Checks API support on Travis was only added to travis-ci.com. So if your checks are running on travis-ci.org you need to migrate.
See this blog post for the announcement.
https://blog.travis-ci.com/2018-05-07-announcing-support-for-github-checks-api-on-travis-ci-com
It is available to private and open source projects using travis-ci.com
This is the announcement about migration of
https://blog.travis-ci.com/2018-09-27-deprecating-github-commit-status-api-for-github-apps-managed-repositories
As part of our gradual migration to GitHub Apps for our GitHub integration, we’re formally deprecating GitHub Commit Status API updates for repositories on travis-ci.com managed by GitHub Apps. Instead, these repositories will have status updates reported to the GitHub Check Runs API.

How do I exclude specific source branches from CI triggers for an Azure Repos Git project?

I'm hoping to somehow replicate the functionality of PR triggers which, according to the docs, are currently only supported for GitHub and Bitbucket Cloud repos. I'd like my CI pipelines to not trigger if the change is incoming from certain branches.
I've mostly tried to solve this problem with GitVersion, which is the part of my pipeline that makes it problematic to trigger builds when I'm merging back from a release build or master back onto develop. So far I've had no luck, so now I'm hoping I've overlooked a feature of Azure Pipelines which will help.
My current pipeline trigger:
trigger:
batch: true
branches:
include:
- develop
paths:
exclude:
- ReadMe.md
- development-pipeline.yml
- release-pipeline.yml
- GitVersion.yml
I'd like a pull request which originated in a release branch (can be identified with the regex pattern [Rr]eleases?[\/-]) or master to not trigger my pipeline. In reality, any change to the develop branch triggers the build.
If you just want the develop branch not to trigger ci build, then you can check "Enable continuous integration" option in the Continuous integration of builds Triggers and set exclude develop branch in the branch filters.
If you want some source branches to trigger the CI build of the develop branch, some can't. I am afraid that this feature you want is not achievable. Once your deveop branch
Enable continuous integration, then the deveop branch will trigger the build pipeline once it changes.
If you want to merge the commits on the release or master branch into the develop branch, and create pr does not trigger the CI build, you could enable the build policy in the Build validation in the branch policy.In this way, only after PR is completed will CI build be triggered.But pr build is unavoidable.

Build trigger is not invoked on push for Azure Git repositories

I have my git repository hosted on Azure DevOps. I created a new yaml based build pipeline in the master branch and have set the trigger section to two existing branches. Other branches don't have a azure-pipeline.yml file nor any kind of branch policies are set for this DevOps project.
trigger:
batch: 'true'
branches:
include:
- master
- develop
The trigger gets invoked for every change in the master branch as expected. But is ignoring any pushes to the develop branch.
If I configure a build pipeline with the visual editor and define the exact two branches there, for every push a build will be triggered.
Any idea how Azure Pipeline respects the build definition also for other branches without copy and pasting the whole definition for every possible branch?
Build trigger is not invoked on push for Azure Git repositories
I have created a sample with the syntax:
trigger:
batch: 'true'
branches:
include:
- master
- Dev
And it works fine on my side. Then I check the new project you provided, but I found that the .yml file is incomplete and does not contain a trigger: node.
So, to resolve this issue, we need to double check the .yml file you modified in under the master branch, and you build .yml file is you modified.
Besides, when we edit the build pipeline, there is a extended button, we could select the option Triggers to set the build trigger with visual editor:
If above not help you, you can try to create a new build pipeline, set the trigger only with Develop branch, check if it works fine, then return to the previous with master
and develop branch.
If all of the above methods are not worked, you may need share a detailed sample and some steps, the reason for this problem may be hidden in the corner we ignore.
Hope this helps.