1

3 つのコンテナー+ LB サービスのデプロイを含む deployment.yamlと、Bitbucket git repo の特定のブランチへの新しいコミットがあるたびにコンテナー イメージをビルドする手順を含むcloudbuild.yamlがあります。

新しいイメージバージョンがあるたびに展開が更新されないという事実を除いて、すべて正常に機能しています(展開で:latestタグを使用しました)。これを変更するには、展開イメージで:latest以外の一意のものを使用する必要があることを理解しました。 git commit SHA として。

問題: GCB CI プロセス中にイメージ宣言の更新を実行して、新しいコミット SHA を含める方法がわかりません。

YAML: https://paste.ee/p/CsETr

4

2 に答える 2

5

デプロイでイメージタグまたは URI 変数を使用し、ビルド時にそれらを sed に置き換えることで解決策を見つけました。

deplyment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: dev
  name: app
  labels:
    app: app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      initContainers:
      - name: init
        image: INIT_IMAGE_NAME
        imagePullPolicy: Always
        command: ['sh', '-c', 'cp -r /app /srv; chown -R 82:82 /srv/app']
        volumeMounts:
        - name: code
          mountPath: /srv
      containers:
      - name: nginx
        image: NGINX_IMAGE_NAME
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        volumeMounts:
        - name: code
          mountPath: /srv
        - name: php-socket
          mountPath: /var/run
        livenessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
        readinessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
      - name: php
        image: PHP_IMAGE_NAME
        imagePullPolicy: Always
        volumeMounts:
        - name: code
          mountPath: /srv
        - name: php-socket
          mountPath: /var/run
        livenessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
        readinessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
      volumes:
        - name: code
          emptyDir: {}
        - name: php-socket
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  namespace: dev
  name: app-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: app

クラウドビルド.yaml

steps:

# Build Images
- id: Building Init Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA', '-f', 'init.dockerfile', '.']

- id: Building Nginx Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA', '-f', 'nginx.dockerfile', '.']
  waitFor: ['-']

- id: Building PHP-FPM Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA', '-f', 'php.dockerfile', '.']
  waitFor: ['-']


# Push Images
- id: Pushing Init Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA']

- id: Pushing Nginx Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA']

- id: Pushing PHP-FPM Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA']


# Update Image Tags
- id: 'Setting Init Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,INIT_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA," deployment.yaml']

- id: 'Setting Nginx Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,NGINX_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA," deployment.yaml']

- id: 'Setting PHP Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,PHP_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA," deployment.yaml']


# Update Deployment
- id: Updating Deployment
  name: gcr.io/cloud-builders/kubectl
  args: ['apply','-f','deployment.yaml']

  env:
    - CLOUDSDK_COMPUTE_ZONE=europe-west2-b
    - CLOUDSDK_CONTAINER_CLUSTER=clusterx

# Images
images:
  - eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA
  - eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA
  - eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA

# Tags
tags:
  - master
  - dev
  - init
于 2018-07-28T13:01:49.660 に答える