bitbucket-pipelines.yml 文件中的部署环境“暂存”在管道中多次出现

hdw*_*ros 12 continuous-integration bitbucket bitbucket-pipelines

我正在尝试让 Bitbucket Pipelines 执行定义部署区域的多个步骤。当我这样做时,我收到错误

配置错误 bitbucket-pipelines.yml 文件中的部署环境“暂存”在管道中多次出现。请参阅我们的文档了解有效环境及其顺序。

据我了解,部署变量必须逐步发生。

我如何设置此示例管道文件才不会遇到该错误?

image: ubuntu:18.04

definitions:
    steps:
        - step: &build
            name: npm-build
            condition:
                changesets:
                    includePaths:
                        # Only run npm if anything in the build directory was touched
                        - "build/**"
            image: node:14.17.5
            script:
              - echo 'build initiated'
              - cd build
              - npm install
              - npm run dev
              - echo 'build complete'
            artifacts:
              - themes/factor/css/**
              - themes/factor/js/**
        - step: &deploychanges
            name: Deploy_Changes
            deployment: Staging
            script:
              - echo 'Installing server dependencies'
              - apt-get update -q
              - apt-get install -qy software-properties-common
              - add-apt-repository -y ppa:git-ftp/ppa
              - apt-get update -q
              - apt-get install -qy git-ftp
              - echo 'All dependencies installed'
              - echo 'Transferring changes'
              - git ftp init --user $FTP_USER --passwd $FTP_PASSWORD $FTP_ADDRESS push --force --changed-only -vv
              - echo 'File transfer complete'
        
        - step: &deploycompiled
            name: Deploy_Compiled
            deployment: Staging
            condition:
                changesets:
                    includePaths:
                        # Only run npm if anything in the build directory was touched
                        - "build/**"
            script:
              - echo 'Installing server dependencies'
              - apt-get update -q
              - apt-get install -qy software-properties-common
              - add-apt-repository -y ppa:git-ftp/ppa
              - apt-get update -q
              - apt-get install -qy git-ftp
              - echo 'All dependencies installed'
              - echo 'Transferring compiled assets'
              - git ftp init --user $FTP_USER --passwd $FTP_PASSWORD $FTP_ADDRESS push --all --syncroot themes/factor/css/ -vv
              - git ftp init --user $FTP_USER --passwd $FTP_PASSWORD $FTP_ADDRESS push --all --syncroot themes/factor/js/ -vv
              - echo 'File transfer complete'

pipelines:
    branches:
        master:
            - step: *build
                <<: *deploychanges
                deployment: Production
            - step:            
                <<: *deploycompiled
                deployment: Production

        dev:
            - step: *build
            - step: *deploychanges
            - step: *deploycompiled
Run Code Online (Sandbox Code Playgroud)

tym*_*mik 9

我发现,在管道中的多个步骤中重用环境变量而不使用部署子句的问题的解决方法是将 ENV VARS 转储到一个文件并将其保存为将在以下步骤中获取的工件。

它的代码片段如下所示:

  steps:

    - step: &set-environment-variables
        name: 'Set environment variables'
        script:
          - printenv | xargs echo export > .envs
        artifacts:
          - .envs


    - step: &next-step
        name: "Next step in the pipeline"
        script:
          - source .envs
          - next_actions


pipelines:

  pull-requests:
    '**':
      - step:
          <<: *set-environment-variables
          deployment: my-deployment
      - step:
          <<: *next-step
          name: "Name of the next step being executed"

  branches:
    staging:
      - step:
          <<: *set-environment-variables
          deployment: my-deployment
      - step:
          <<: *next-step
          name: "Name of the next step being executed"
Run Code Online (Sandbox Code Playgroud)

到目前为止,这个解决方案对我有用。

更新:在 .envs 文件中出现“%s”问题导致后面的源 .envs 语句失败后,这里是与初始步骤略有不同的方法。它解决了这个特定问题,但也只导出您知道管道中需要的那些变量 - 请注意,第一个脚本步骤有很多可用的 bitbucket 环境变量,无论如何,这些变量自然可以用于以后的脚本,并且它使得更有意义(无论如何对我来说),您不只是将所有环境变量转储到 .env 工件,而是以更加受控的方式进行。

   - step: &set-environment-variables
        name: 'Set environment variables'
        script:
          - echo "export SSH_USER=$SSH_USER" > .envs
          - echo "export SERVER_IP=$SERVER_IP" >> .envs
          - echo "export ANOTHER_ENV_VAR=$ANOTHER_ENV_VAR" >> .envs
        artifacts:
          - .envs
Run Code Online (Sandbox Code Playgroud)

在此示例中,.envs 现在将仅包含这 3 个环境变量,而不是整个系统 + bitbucket 变量堆(当然,也没有讨厌的 %s 字符!)

  • 我认为在可下载工件中保留 .env 文件的秘密不是一个好习惯 (4认同)

小智 5

使用舞台功能

Bitbucket 发布了一项名为“stage”的测试版功能,支持使用一个部署环境执行多个步骤。

阶段允许您使用共享属性对管道步骤进行逻辑分组,例如对同一部署环境的步骤进行分组、锁定多个步骤的部署环境(防止其他管道运行与其交互)以及跨多组连续步骤共享部署变量。

所以你的管道将是:

pipelines:
  branches:
    master:
      - stage:
          name: 'Build and Deploy to prod'
          deployment: "Production" # <- put here your deployment name
          steps: # next steps will use the same deployment variables
            - step: *build
            - step: *deploychanges
            - step: *deploycompiled
    dev:
      - stage:
          name: 'Build and Deploy to dev'
          steps:
            - step: *build
            - step: *deploychanges
            - step: *deploycompiled
Run Code Online (Sandbox Code Playgroud)

参考: https: //support.atlassian.com/bitbucket-cloud/docs/stage-options/


Vib*_*was 0

通常情况下,您会部署到一个环境中。所以只有一个部署步骤。因此,您特别应该将您的“部署”组专门放入其中。这就是 Bitbucket 管理代码是否部署的方式。因此,您可以有多个步骤,其中一个步骤用于测试单元案例、集成案例,另一个步骤用于构建二进制文件,最后一个步骤作为工件部署到环境标记部署组。请参见下面的示例。

definitions: 
  steps:
    - step: &test-vizdom-services
        name: "Vizdom services unit Tests"
        image: mcr.microsoft.com/dotnet/core/sdk:3.1
        script:     
            - cd ./vizdom/vizdom.services.Tests
            - dotnet test vizdom.services.Tests.csproj    


pipelines:
  custom:
    DEV-AWS-api-deploy:  
      - step: *test-vizdom-services        
      - step:
          name: "Vizdom Webapi unit Tests"
          image: mcr.microsoft.com/dotnet/core/sdk:3.1
          script:
              - export ASPNETCORE_ENVIRONMENT=Dev       
              - cd ./vizdom/vizdom.webapi.tests
              - dotnet test vizdom.webapi.tests.csproj    
      - step:
          deployment: DEV-API
          name: "API: Build > Zip > Upload > Deploy"
          image: mcr.microsoft.com/dotnet/core/sdk:3.1
          script:
              - apt-get update
              - apt-get install zip -y
              - mkdir -p ~/deployment/release_dll
              - cd ./vizdom/vizdom.webapi
              - cp -r ../shared_settings ~/deployment              
              - dotnet publish vizdom.webapi.csproj -c Release -o ~/deployment/release_dll
              - cp Dockerfile ~/deployment/
              - cp -r deployment_scripts ~/deployment
              - cp deployment_scripts/appspec_dev.yml ~/deployment/appspec.yml
              - cd ~/deployment
              - zip -r $BITBUCKET_CLONE_DIR/dev-webapi-$BITBUCKET_BUILD_NUMBER.zip .
              - cd $BITBUCKET_CLONE_DIR
              - pipe: atlassian/aws-code-deploy:0.5.3
                variables:
                  AWS_DEFAULT_REGION: 'us-east-1'
                  AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                  AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                  COMMAND: 'upload'
                  APPLICATION_NAME: 'ss-webapi'
                  ZIP_FILE: 'dev-webapi-$BITBUCKET_BUILD_NUMBER.zip'
                  S3_BUCKET: 'ss-codedeploy-repo'
                  VERSION_LABEL: 'dev-webapi-$BITBUCKET_BUILD_NUMBER.zip'
              - pipe: atlassian/aws-code-deploy:0.5.3
                variables:
                  AWS_DEFAULT_REGION: 'us-east-1'
                  AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                  AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                  COMMAND: 'deploy'
                  APPLICATION_NAME: 'ss-webapi'
                  DEPLOYMENT_GROUP: 'dev-single-instance'
                  WAIT: 'false'
                  S3_BUCKET: 'ss-codedeploy-repo'
                  VERSION_LABEL: 'dev-webapi-$BITBUCKET_BUILD_NUMBER.zip'
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我有多个步骤来运行测试用例,但我最终会构建二进制文件并在最后一步中部署代码。我本可以将其分解为单独的步骤,但我不想浪费时间使用另一个步骤,因为克隆和复制工件需要一些时间。现在有 3 个步骤,可以分为 4 个步骤。其中第 4 个步骤是部署步骤。我希望这能带来一些清晰度。

您还可以根据需要修改部署组的名称,最多可以有 50 个部署组:)