Bitbucket 管道环境变量

发布于 2025-01-11 04:00:32 字数 3504 浏览 0 评论 0原文

我正在将 React 应用程序部署到 s3 存储桶,并且有很多环境变量,所以问题是如何处理 bitbucket 管道中的环境变量?

管道

+ npm run build
> [email protected] build /opt/atlassian/pipelines/agent/build
> env-cmd -f .env.prod react-scripts build  && cp build/index.html build/200.html
Error: Failed to find .env file at path: .env.prod
    at getEnvFile (/opt/atlassian/pipelines/agent/build/node_modules/env-cmd/dist/get-env-vars.js:40:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `env-cmd -f .env.prod react-scripts build  && cp build/index.html build/200.html`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-03-01T10_17_14_844Z-debug.log

bitbucket-pipeline.yml中的错误

image: node:10

# Workflow Configuration

pipelines:
  default:
    - parallel:
      - step:
          name: Build and Test
          caches:
            - node
          script:
            - npm install
            # CI=true in default variables for Bitbucket Pipelines https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/
      
  branches:
    master:
      - parallel:
        - step:
            name: Build and Test
            caches:
              - node
            script:
              - npm install
              # CI=true in default variables for Bitbucket Pipelines https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/
              - npm run build
            artifacts:
              - build/**
        - step:
            name: Security Scan
            script:
              # Run a security scan for sensitive data.
              # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
              - pipe: atlassian/git-secrets-scan:0.5.1
      - step:
          name: Deploy to Production
          deployment: Production
          trigger: manual
          clone:
            enabled: false
          script:
            # sync your files to S3
            - pipe: atlassian/aws-s3-deploy:1.1.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                S3_BUCKET: $S3_BUCKET
                LOCAL_PATH: 'build'
            # triggering a distribution invalidation to refresh the CDN caches
            - pipe: atlassian/aws-cloudfront-invalidate:0.6.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                DISTRIBUTION_ID: '123xyz'

I'm deploying my react app to the s3 bucket and I have a lot of env variables, so the question is how can I handle the env variables in bitbucket pipelines?

error in pipeline

+ npm run build
> [email protected] build /opt/atlassian/pipelines/agent/build
> env-cmd -f .env.prod react-scripts build  && cp build/index.html build/200.html
Error: Failed to find .env file at path: .env.prod
    at getEnvFile (/opt/atlassian/pipelines/agent/build/node_modules/env-cmd/dist/get-env-vars.js:40:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `env-cmd -f .env.prod react-scripts build  && cp build/index.html build/200.html`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-03-01T10_17_14_844Z-debug.log

bitbucket-pipeline.yml

image: node:10

# Workflow Configuration

pipelines:
  default:
    - parallel:
      - step:
          name: Build and Test
          caches:
            - node
          script:
            - npm install
            # CI=true in default variables for Bitbucket Pipelines https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/
      
  branches:
    master:
      - parallel:
        - step:
            name: Build and Test
            caches:
              - node
            script:
              - npm install
              # CI=true in default variables for Bitbucket Pipelines https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/
              - npm run build
            artifacts:
              - build/**
        - step:
            name: Security Scan
            script:
              # Run a security scan for sensitive data.
              # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
              - pipe: atlassian/git-secrets-scan:0.5.1
      - step:
          name: Deploy to Production
          deployment: Production
          trigger: manual
          clone:
            enabled: false
          script:
            # sync your files to S3
            - pipe: atlassian/aws-s3-deploy:1.1.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                S3_BUCKET: $S3_BUCKET
                LOCAL_PATH: 'build'
            # triggering a distribution invalidation to refresh the CDN caches
            - pipe: atlassian/aws-cloudfront-invalidate:0.6.0
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
                AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
                DISTRIBUTION_ID: '123xyz'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2025-01-18 04:00:32

到目前为止,我发现的最佳解决方案是使用位桶变量,并将它们传递到管道中。
支持文章中的相关片段:

工作区变量

可以从属于该工作区的所有存储库访问为工作区指定的变量。你一定是一个
管理员管理工作区变量。

从左下角的头像中选择一个工作区。

选择左侧导航边栏上的“设置”以打开您的工作区设置。
在左侧菜单中,转到 Pipelines >工作区变量。

工作区变量可以被存储库变量覆盖。

对属于团队或帐户的任何存储库(私有或公共)具有写入权限的所有用户都可以访问工作区变量。

您必须是工作区或存储库的管理员才能分别管理变量。

https://support.atlassian.com/bitbucket-cloud/docs /变量和秘密/

The best solution I've found so far is using bitbucket variables, and passing them into the pipeline.
Relevent snippet from the support article:

Workspace variables

Variables specified for a workspace can be accessed from all repositories that belong to the workspace. You must be an
administrator to manage workspace variables.

From your avatar in the bottom left, select a workspace.

Select Settings on the left navigation sidebar to open your Workspace settings.
In the menu on the left, go to Pipelines > Workspace variables.

Workspaces variables can be overridden by repository variables.

Workspace variables can be accessed by all users with the write permission for any repository (private or public) that belongs to the team or account.

You must be an administrator of a workspace or a repository to manage variables respectively.

https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/

标点 2025-01-18 04:00:32

只需在脚本中需要变量的位置添加一行并加载文件:

- export $(cat .env | xargs)

Simply add a line in the script where you need the variables and load the file:

- export $(cat .env | xargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文