Google Cloud Build无法在Git Hooks上正常运行

tcbh2hod  于 2023-09-29  发布在  Git
关注(0)|答案(1)|浏览(115)

我使用gcloud,我在虚拟机上有一个git服务器,在仓库中通过git钩子(post-receive),我调用cloud build来触发docker镜像的构建和在kubernetes上部署镜像。
我已经在云构建服务帐户上设置了适当的权限,以访问Container/Artifact Registry和Cloud Source Repositories上的另一个存储库,该存储库具有要部署的kubernetes清单。
问题是这个流有时工作,有时不工作,日志没有给予失败的原因。如果我比较一个结束良好的流和一个结束不好的流的日志,它们是相同的,除了一个流简单地停止。
调用Cloud Build的git钩子中的脚本看起来像这样(这是在GCP vm内部):

#!/bin/bash

oldrev=$1
newrev=$2
refname=$3

BRANCH_REF=$(git rev-parse --symbolic --abbrev-ref $refname)
BASE_PATH="path/to/basepath"

build_image() {
    cd "$BASE_PATH"
    mkdir -p "build.tmp"
    cd "build.tmp"
    git clone --branch $BRANCH_REF --single-branch "/path/to/repo" "$NAME"
    # This directory contains the Dockerfile
    cd repo

    #
    # code the condition to call cloud build
    # through the commit message
    #

    if [[ $COMMIT_MSG =~ $pattern ]]; then
        nohup gcloud builds submit --log-http --config="/path/to/cloudbuild.yaml" --substitutions=_NAME="myproyect",_HASH=$HASH > "$BASE_PATH/build_log.txt" 2>&1 &
    fi
}

这就是云构建yaml的样子

# This step builds the container image and pushes the image to Conteiner Registry
steps:
- name: 'gcr.io/cloud-builders/docker'
  id: Build
  args: [ 'build', '-t', 'gcr.io/cloud-builders/${_NAME}:docker-${_HASH}', '-t', 'gcr.io/cloud-builders/${_NAME}:latest', '.' ]

# This step clones the kubernetes google repository
- name: 'gcr.io/cloud-builders/gcloud'
  id: Clone env repository
  entrypoint: /bin/sh
  args:
  - '-c'
  - |
    gcloud source repos clone kubernetes_manifests && \
    cd kubernetes_manifests && \
    git config user.email $(gcloud auth list --filter=status:ACTIVE --format='value(account)')

# This step generates the new manifest
- name: 'gcr.io/cloud-builders/gcloud'
  id: Generate manifest
  entrypoint: /bin/sh
  args:
  - '-c'
  - |
    sed -e "s#image: gcr.io/cloud-builders/myproyect:.*#image: gcr.io/cloud-builders/${_NAME}:docker-${_HASH}#" kubernetes_manifests/myproyect/development/3.deployment.yaml > kubernetes_manifests/myproyect/development/3.deployment.yaml.tmp && \
    mv kubernetes_manifests/myproyect/development/3.deployment.yaml.tmp kubernetes_manifests/myproyect/development/3.deployment.yaml

# This step pushes the manifest back to kubernetes
- name: 'gcr.io/cloud-builders/gcloud'
  id: Push manifest
  entrypoint: /bin/sh
  args:
  - '-c'
  - |
    set -x && \
    cd kubernetes_manifests && \
    git add myproyect/development/3.deployment.yaml && \
    git commit -m "Update image gcr.io/cloud-builders/${_NAME}:${_HASH}
    Built from commit ${_HASH} of repository ${_NAME}" --author="Cloud Build Agent" && \
    git push origin master

# This step deploys the new version of our container image
- name: 'gcr.io/cloud-builders/kubectl'
  id: Deploy
  args:
  - 'apply'
  - '-f'
  - 'kubernetes_manifests/myproyect/development/3.deployment.yaml'
  env:
  - 'CLOUDSDK_COMPUTE_REGION=us-central1-f'
  - 'CLOUDSDK_CONTAINER_CLUSTER=k8-myproyect'
  - 'CLOUDSDK_CORE_PROJECT=cloud-builders'

timeout: 43200s
options:
 machineType: 'N1_HIGHCPU_8'
 logging: CLOUD_LOGGING_ONLY
substitutions:
 _NAME: 'myproyect' # default value
 _HASH: 'latest' # default value
images:
- 'gcr.io/cloud-builders/${_NAME}:docker-${_HASH}'
- 'gcr.io/cloud-builders/${_NAME}:latest'

就像我说的,有时候有用,有时候没用,我不明白为什么。我希望有人能为我指出解决这个问题的正确方向。
如果有任何价值,这是日志中失败/停止时的最后一个请求

==== request start ====
uri: https://storage.googleapis.com/storage/v1/b?alt=json&maxResults=1000&prefix=myproyect_cloudbuild&project=myProyect
method: GET
== headers start ==
b'accept': b'application/json'
b'accept-encoding': b'gzip, deflate'
b'authorization': --- Token Redacted ---
b'content-length': b'0'
b'user-agent': b'google-cloud-sdk gcloud/445.0.0 command/gcloud.builds.submit invocation-id/6aa15f12724940dea7fc6bc4a244eac9 environment/GCE environment-version/None client-os/LINUX client-os-ver/5.15.0 client-pltf-arch/x86_64 interactive/False from-script/False python/3.9.16 term/ (Linux 5.15.0-1038-gcp)'
b'x-goog-api-client': b'cred-type/mds'
== headers end ==
== body start ==

== body end ==
==== request end ====
---- response start ----
status: 200
-- headers start --
Cache-Control: private, max-age=0, must-revalidate, no-transform
Content-Length: 789
Content-Type: application/json; charset=UTF-8
Date: Tue, 12 Sep 2023 17:30:52 GMT
Expires: Tue, 12 Sep 2023 17:30:52 GMT
Server: UploadServer
Vary: Origin, X-Origin
X-GUploader-UploadID: ADPycdtf0wrEzjksupKzYNSnqtRqv9z93lIEVFhG0tjpoxELV-WkCSmlW4YPMl8idPfG194FtutaWllo37PMhVYmcrk0rg
-- headers end --
-- body start --
{
  "kind": "storage#buckets",
  "items": [
    {
      "kind": "storage#bucket",
      "selfLink": "https://www.googleapis.com/storage/v1/b/myproyect_cloudbuild",
      "id": "myproyect_cloudbuild",
      "name": "myproyect_cloudbuild",
      "projectNumber": "249995562558",
      "metageneration": "1",
      "location": "US",
      "storageClass": "STANDARD",
      "etag": "CAE=",
      "timeCreated": "2023-06-13T00:53:30.886Z",
      "updated": "2023-06-13T00:53:30.886Z",
      "iamConfiguration": {
        "bucketPolicyOnly": {
          "enabled": false
        },
        "uniformBucketLevelAccess": {
          "enabled": false
        },
        "publicAccessPrevention": "inherited"
      },
      "locationType": "multi-region",
      "rpo": "DEFAULT"
    }
  ]
}

-- body end --
total round trip time (request+response): 0.074 secs
---- response end ----
----------------------
enyaitl3

enyaitl31#

基本上我的错误是试图在后台运行cloud build命令,解决方案是使用gcloud builds submit中的async选项
gcloud builds submit --async --config="/path/to/cloudbuild.yaml”--substitutions=_NAME=“myproyect”,_HASH=$HASH
而不是这个
nohup gcloud builds submit.等

相关问题