首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在构建ZIP文件时没有将bitbucket管道变量$BITBUCKET_REPO_SLUG转换为存储库名

为什么在构建ZIP文件时没有将bitbucket管道变量$BITBUCKET_REPO_SLUG转换为存储库名
EN

Stack Overflow用户
提问于 2022-05-11 22:55:43
回答 2查看 621关注 0票数 0

我正在创建一个Bitbucket管道,用于将代码从Bitbucket部署到AWS EC2实例。在管道中这样做所需的步骤是:

  1. 将所有代码打包到一个压缩文件中,
  2. 将该zip文件上传到S3桶
  3. ,使用AWS代码部署

将ZIP文件部署到EC2实例

我希望在将zip文件上传到S3桶时调用它。为此,我使用$BITBUCKET_REPO_SLUG管道变量并设置管道的第一步,如下所示,其中applications是我想要打包到zip文件中的存储库中的文件夹。

代码语言:javascript
复制
    staging:
      - step:
          name: Zip Code
          image: atlassian/default-image:3
          script:
            - zip -r "$BITBUCKET_REPO_SLUG.zip" "applications"
          artifacts:
            - "$BITBUCKET_REPO_SLUG.zip"

但是,从下面的管道输出(在Build下)可以看到,$BITBUCKET_REPO_SLUG.zip并没有像预期的那样更改为<repository_name>.zip

代码语言:javascript
复制
zip -r "$BITBUCKET_REPO_SLUG.zip" "applications"
+ zip -r "$BITBUCKET_REPO_SLUG.zip" "applications"
  adding: applications/ (stored 0%)
  adding: applications/configuration/ (stored 0%)
  adding: applications/configuration/trade_capture_trayport_private.ini (deflated 58%)
  adding: applications/trade_capture_etrm/ (stored 0%)
  adding: applications/trade_capture_etrm/trade_capture_trayport_private.ps1 (deflated 73%)
  adding: applications/trade_capture_etrm/etrm_private_alerting.ps1 (deflated 65%)
  adding: applications/trade_capture_etrm/tests/ (stored 0%)
  adding: applications/trade_capture_etrm/tests/trayport.tests.ps1 (deflated 93%)
  adding: applications/appspec.yml (deflated 70%)

Build teardown
  Searching for files matching artifact pattern $BITBUCKET_REPO_SLUG.zip
  Searching for test report files in directories named [test-results, failsafe-reports, test-reports, TestResults, surefire-reports] down to a depth of 4
  Finished scanning for test reports. Found 0 test report files.
  Merged test suites, total number tests is 0, with 0 failures and 0 errors.

在接下来的步骤中,当上传到S3时,我使用相同的方法来引用zip文件,如下面的代码所示

代码语言:javascript
复制
      - step:
          name: ⬆️ Upload to S3
          services:
            - docker
          oidc: true
          script:
            # Test upload
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                COMMAND: 'upload'
                APPLICATION_NAME: $APPLICATION_NAME
                ZIP_FILE: "$BITBUCKET_REPO_SLUG.zip"
                S3_BUCKET: $S3_BUCKET_STAGING
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

从上传到S3步骤的输出可以看出,$BITBUCKET_REPO_SLUG.zip被正确地转换为.zip

代码语言:javascript
复制
INFO: Authenticating with a OpenID Connect (OIDC) Web Identity Provider
INFO: Executing the aws-ecr-push-image pipe...
INFO: Uploading powershell_trade_capture.zip to S3.
Traceback (most recent call last):
  File "/pipe.py", line 264, in <module>
    pipe.run()
  File "/pipe.py", line 254, in run
    self.upload_to_s3()
  File "/pipe.py", line 230, in upload_to_s3
    with open(self.get_variable('ZIP_FILE'), 'rb') as zip_file:
FileNotFoundError: [Errno 2] No such file or directory: 'powershell_trade_capture.zip'

为什么管道变量$BITBUCKET_REPO_SLUG被正确地转换为“上载到S3”步骤中的值,而没有在"Zip代码“步骤中转换为存储库名称(而不是作为字符串)?

EN

回答 2

Stack Overflow用户

发布于 2022-05-12 22:05:26

问题是,Bitbucket管道的工件部分目前不支持变量替换。有关2022年2月创建的详细信息,请参见https://jira.atlassian.com/browse/BCLOUD-21666

我们找到的解决方案是解决问题,避免使用工件部分。我们通过将"Zip“和”上载到管道的S3“部分结合起来来做到这一点。这意味着不再需要使用人工制品。

因此,这个新的部分设置如下。

代码语言:javascript
复制
      - step:
          name: ⬆️ Zip & Upload to S3
          services:
            - docker
          oidc: true
          image: atlassian/default-image:3
          script:
            # Build the zip file
            - zip -r $BITBUCKET_REPO_SLUG.zip "applications"
            # Upload the zip file to S3
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                COMMAND: "upload"
                APPLICATION_NAME: $APPLICATION_NAME
                ZIP_FILE: $BITBUCKET_REPO_SLUG.zip
                S3_BUCKET: $S3_BUCKET_STAGING
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

在这种情况下,$BITBUCKET_REPO_SLUG的使用没有问题,管道也成功了。

票数 1
EN

Stack Overflow用户

发布于 2022-05-12 07:59:42

您不需要zip或工件的引号,因此我假设它没有正确地转换变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72208433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档