{"id":871,"date":"2023-08-26T01:14:22","date_gmt":"2023-08-25T17:14:22","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=871"},"modified":"2026-02-18T01:20:34","modified_gmt":"2026-02-17T17:20:34","slug":"build-docker-images-using-google-cloud-build-from-your-github-repository","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/build-docker-images-using-google-cloud-build-from-your-github-repository.html","title":{"rendered":"Build Docker Images using Google Cloud Build from Your GitHub Repository"},"content":{"rendered":"<p>Triggering a Docker image build on Google Cloud Build when pushing commits to a GitHub repository.<\/p>\n<h2>Create Google Cloud Build Configuration File<\/h2>\n<p>Create a <code>cloudbuild.yaml<\/code> in the root folder:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-yaml\">substitutions:\n  _REGION: us-west1\n  _REPOSITORY: your-repo\n  _BRANCH_TAG: ${BRANCH_NAME\/\/\/\/-} # Replace \/ with - in branch names\n\nsteps:\n  - id: my-app\n    name: gcr.io\/cloud-builders\/docker\n    args:\n      [\n        \"build\",\n        \"--cache-from\",\n        \"${_REGION}-docker.pkg.dev\/$PROJECT_ID\/${_REPOSITORY}\/my-app:${_BRANCH_TAG}\",\n        \"-t\",\n        \"${_REGION}-docker.pkg.dev\/$PROJECT_ID\/${_REPOSITORY}\/my-app:${_BRANCH_TAG}\",\n        \"-t\",\n        \"${_REGION}-docker.pkg.dev\/$PROJECT_ID\/${_REPOSITORY}\/my-app:$SHORT_SHA\",\n        \"docker\/my-blog\/\",\n      ]\n    waitFor: [\"-\"]\n\n# Cloud Build only pushes tags listed here\nimages:\n  - ${_REGION}-docker.pkg.dev\/$PROJECT_ID\/${_REPOSITORY}\/my-app:$SHORT_SHA\n  - ${_REGION}-docker.pkg.dev\/$PROJECT_ID\/${_REPOSITORY}\/my-app:${_BRANCH_TAG}<\/code><\/pre>\n<p>The above config will store your Docker images in Artifact Registry. If you want to push images to another region, simply rename <code>us-west1-docker.pkg.dev<\/code> to something like <code>asia-east1-docker.pkg.dev<\/code>.<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/cloud.google.com\/build\/docs\/configuring-builds\/create-basic-configuration\">https:\/\/cloud.google.com\/build\/docs\/configuring-builds\/create-basic-configuration<\/a><br \/>\n<a href=\"https:\/\/cloud.google.com\/build\/docs\/building\/store-artifacts-in-artifact-registry\">https:\/\/cloud.google.com\/build\/docs\/building\/store-artifacts-in-artifact-registry<\/a><\/p>\n<h2>Configure Google Cloud Build with Your GitHub Repository<\/h2>\n<p>Go to Google Cloud Dashboard &gt; Google Cloud Build &gt; Triggers &gt; Create trigger:<\/p>\n<ul>\n<li>Region: Global<\/li>\n<li>Event: Push to a branch<\/li>\n<li>Source: 1st Gen<\/li>\n<li>Repository:\n<ul>\n<li>Connect new repository<\/li>\n<li>Source code management provider: GitHub (Cloud Build GitHub App)<\/li>\n<li>Install Google Cloud Build GitHub app<\/li>\n<li>Only select repositories<\/li>\n<\/ul>\n<\/li>\n<li>Branch: <code>^main$<\/code><\/li>\n<li>Type: Autodetected<\/li>\n<li>Location: Repository<\/li>\n<\/ul>\n<p>That's it, now you could push some commits to your GitHub repository.<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/console.cloud.google.com\/cloud-build\/triggers\">https:\/\/console.cloud.google.com\/cloud-build\/triggers<\/a><br \/>\n<a href=\"https:\/\/console.cloud.google.com\/cloud-build\/builds\">https:\/\/console.cloud.google.com\/cloud-build\/builds<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Triggering a Docker image build on Google Cloud Build when pushing commits to a GitHub repository.<\/p>\n","protected":false},"author":1,"featured_media":872,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[36,10,114],"class_list":["post-871","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-devops","tag-docker","tag-github","tag-google-cloud-platform"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/872"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}