【问题标题】:YAML anchors in bitbucket pipelinesbitbucket 管道中的 YAML 锚点
【发布时间】:2020-12-01 13:06:44
【问题描述】:

我正在尝试编写 bitbucket 管道并使用YAML anchors 来减少重复。

这就是我想做的例子:

---

definitions:
  steps:
    - step: &build-test-alpine
        image: php:7.4-alpine
        caches:
          - composer
        script:
          - apk add unzip curl
          - curl -sS https://getcomposer.org/installer | php -- --install-dir='/usr/local/bin' --filename='composer'
          - composer --no-ansi -n install
          - composer --no-ansi -n test

pipelines:
  custom:
    deploy:
      - step:
          name: Build and deploy application
          <<: *build-test-alpine
          script:
            - Some other command to execute

  default:
    - step:
        <<: *build-test-alpine
        name: PHP 7.4
    - step:
        <<: *build-test-alpine
        image: php:7.3-alpine
        name: PHP 7.3

...

当然这不起作用(请参阅自定义部署步骤)。不能定义另一个脚本项并期望它将其合并到锚脚本。有没有办法做到这一点?

【问题讨论】:

    标签: yaml bitbucket-pipelines


    【解决方案1】:

    过了很长一段时间,我对这个问题有了回应,但它并不像人们想象的那么好。

    我所期望的是,YAML 解析器能够以某种方式合并部分列表项的元素并使其成为一个。这不起作用,也不受 YAML 标准的支持。

    我们可以这样做:

    ---
    
    definitions:
      commonItems: &common
        apk add unzip curl &&
        curl -sS https://getcomposer.org/installer | php -- --install-dir='/usr/local/bin' --filename='composer' &&
        composer --no-ansi -n install &&
        composer --no-ansi -n test
      steps:
        - step: &build-test-alpine
            image: php:7.4-alpine
            caches:
              - composer
            script:
              - *common
    
    pipelines:
      custom:
        deploy:
          - step:
              name: Build and deploy application
              <<: *build-test-alpine
              script:
                - *common
                - some other command to execute
    
      default:
        - step:
            <<: *build-test-alpine
            name: PHP 7.4
        - step:
            <<: *build-test-alpine
            image: php:7.3-alpine
            name: PHP 7.3
    
    

    基本上每次我们设置一个合并锚点的步骤时,在锚点元素覆盖锚点本身中的相同项目之后指定的任何项目。因此,如果我们将常用命令转换为 YAML 锚中的大字符串,那么我们就可以在我们想要的任何步骤中重复使用它,并且减少输入和重复,并且内容变得更具可读性。

    【讨论】:

      【解决方案2】:

      您也许可以将您的 Some other command to execute 脚本放入 after-script。像这样的:

      definitions:
        steps:
          - step: &build-test-alpine
              image: php:7.4-alpine
              caches:
                - composer
              script:
                - apk add unzip curl
                - curl -sS https://getcomposer.org/installer | php -- --install-dir='/usr/local/bin' --filename='composer'
                - composer --no-ansi -n install
                - composer --no-ansi -n test
      
      pipelines:
        custom:
          deploy:
            - step:
                name: Build and deploy application
                <<: *build-test-alpine
                after-script:
                  - Some other command to execute
      
        default:
          - step:
              <<: *build-test-alpine
              name: PHP 7.4
          - step:
              <<: *build-test-alpine
              image: php:7.3-alpine
              name: PHP 7.3
      

      【讨论】:

      • 不幸的是,脚本和后脚本之间没有保留环境,所以它对我不起作用。
      猜你喜欢
      • 2021-07-20
      • 2020-01-30
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多