Google Cloud Builder 中同一构建步骤中的多个命令

Multiple commands in the same build step in Google Cloud Builder

我想在 Google Cloud Builder 环境中 运行 我们的自动化后端测试套件。然而,很自然地,我遇到了在 Cloud Builder 中安装各种依赖项和先决条件的需求,以便我们的最终测试 运行ner (php tests/run) 可以 运行.

这是我当前的cloudbuild.yaml:

steps:

  - name: 'ubuntu'
    args: ['bash', './scripts/install-prerequisites.sh', '&&', 'composer install -n -q --prefer-dist', '&&', 'php init --overwrite=y', '&&', 'php tests/run']

目前,多个命令的链接不起作用。唯一执行的是 bash ./scripts/install-prerequisites.sh 部分。如何让所有这些命令按顺序执行?

参见:

By default, build steps run sequentially, but you can configure them to run concurrently.

The order of the build steps in the steps field relates to the order in which the steps are executed. Steps will run serially or concurrently based on the dependencies defined in their waitFor fields.

A step is dependent on every id in its waitFor and will not launch until each dependency has completed successfully.

所以你只需要将命令作为每个步骤分开。

像这样。

steps:
  - name: 'ubuntu'
    args: ['bash', './scripts/install-prerequisites.sh']
    id: 'bash ./scripts/install-prerequisites.sh'
  - name: 'ubuntu'
    args: ['composer', 'install', '-n', '-q', '--prefer-dist']
    id: 'composer install -n -q --prefer-dist'
  - name: 'ubuntu'
    args: ['php', 'init', '--overwrite=y']
    id: 'php init --overwrite=y'
  - name: 'ubuntu'
    args: ['php', 'tests/run']
    id: 'php tests/run'

顺便问一下,可以使用 ubuntu 图片 运行 php 和 composer 命令吗?

我认为你应该使用或构建 docker 图像,它可以 运行 php 和 composer 命令。

作曲家 docker 图片是 here

steps:
- name: 'gcr.io/$PROJECT_ID/composer'
  args: ['install']

我相信目前您有 2 个选择来实现此目标:

  1. 创建一个包含您想要的命令序列的脚本并直接调用该脚本:
# cloudbuild.yaml
steps:
  - name: 'ubuntu'
    args: ['./my-awesome-script.sh']
# my-awesome-script.sh
/usr/bin/env/bash

set -eo pipefail

./scripts/install-prerequisites.sh
composer install -n -q --prefer-dist
php init --overwrite=y
php tests/run
  1. 使用您想遵循的所有命令调用 bash -c
steps:
  - name: 'ubuntu'
    args: ['bash', '-c', './scripts/install-prerequisites.sh && composer install -n -q --prefer-dist && php init --overwrite=y && php tests/run']

运行 脚本更易读的方法可能是使用 breakout 语法(来源:mastering cloud build syntax

steps:
- name: 'ubuntu'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    ./scripts/install-prerequisites.sh \
    && composer install -n -q --prefer-dist \
    && php init --overwrite=y \
    && php tests/run

但是,这仅在您的构建步骤映像安装了适当的 deps(php、作曲家)时才有效。