如何使用 azure 管道变量为我的 docker 映像设置环境变量?

发布于 2025-01-10 15:55:52 字数 2735 浏览 0 评论 0原文

我有一个天蓝色的管道,我想用它来部署我的 Rails 应用程序。 该应用程序有一个 Dockerfile 和一个 docker-compose 文件。

我试图将 RAILS_MASTER_KEY 设置为管道中的秘密变量,然后将其引用为我的 docker compose 文件中的环境变量。

我可以确认代理正在使用管道 yaml 中的 echo 正确设置变量。但是,环境变量没有正确传递/设置到我的 docker-compose 和最终的 Dockerfile。

我花了几天时间阅读天蓝色文档、堆栈溢出和它无法正常工作来解决这个问题。

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables

这是我的代码:

azure-pipelines.yaml:

steps:
  - script: echo "test $(RAILS_MASTER_KEY)"
  - script: echo "test $(testvar)"
  - task: DockerCompose@0
    inputs:
      containerregistrytype: 'Azure Container Registry'
      azureSubscription: 'de-identified'
      azureContainerRegistry: '{"loginServer":""de-identified"", "id" : "de-identified"}'
      dockerComposeFile: '**/docker-compose.yml'
      dockerComposeFileArgs: |
        RAILS_MASTER_KEY=$(RAILS_MASTER_KEY)
        testvar=$(testvar)
      action: 'Build services'
    env:
      RAILS_MASTER_KEY: $(RAILS_MASTER_KEY)

docker-compose.yml:

version: "3.3"
services:
  web:
    build: .
    command: command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' && echo ${RAILS_MASTER_KEY}"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
      testvar: ${testvar}

Dockerfile:

FROM ruby:3.1.0-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    postgresql-client \
    git \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
ENV BUNDLER_VERSION 2.3.5
ENV RAILS_ENV production
ENV RAILS_MASTER_KEY ${RAILS_MASTER_KEY}
ENV testvar ${testvar}
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN echo "----------------key is 1. ${RAILS_MASTER_KEY}"
RUN echo "----------------key is 11. $( testvar )"
RUN echo "----------------key is 12. $testvar"
RUN echo "----------------key is 2. $[variables.RAILS_MASTER_KEY]"
RUN echo "----------------key is 4. $(RAILS_MASTER_KEY)"
RUN gem install bundler -v $BUNDLER_VERSION
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
RUN rm -f /app/tmp/pids/server.pid
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s", "-e", "production", "-b", "0.0.0.0"]

如您所见,我在尝试调试此问题时尝试了 echo,因此请忽略所有 echo 语句。 任何帮助/指导将不胜感激。 谢谢。

I have a azure pipeline that I want to use to deploy my rails app.
The app has a Dockerfile and a docker-compose file.

I am trying to set the RAILS_MASTER_KEY as a secret variable in my pipeline and then reference it as environment variable in my docker compose file.

I can confirm that the agent is setting the variable correctly using echo in the pipeline yaml. However, The environment variable is not getting passed/set properly to my docker-compose and ultimately Dockerfile.

I have troubleshoot this for days reading azure docs, stack overflow and its not working.

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables

Here is my code:

azure-pipelines.yaml:

steps:
  - script: echo "test $(RAILS_MASTER_KEY)"
  - script: echo "test $(testvar)"
  - task: DockerCompose@0
    inputs:
      containerregistrytype: 'Azure Container Registry'
      azureSubscription: 'de-identified'
      azureContainerRegistry: '{"loginServer":""de-identified"", "id" : "de-identified"}'
      dockerComposeFile: '**/docker-compose.yml'
      dockerComposeFileArgs: |
        RAILS_MASTER_KEY=$(RAILS_MASTER_KEY)
        testvar=$(testvar)
      action: 'Build services'
    env:
      RAILS_MASTER_KEY: $(RAILS_MASTER_KEY)

docker-compose.yml:

version: "3.3"
services:
  web:
    build: .
    command: command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' && echo ${RAILS_MASTER_KEY}"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
      testvar: ${testvar}

Dockerfile:

FROM ruby:3.1.0-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    postgresql-client \
    git \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
ENV BUNDLER_VERSION 2.3.5
ENV RAILS_ENV production
ENV RAILS_MASTER_KEY ${RAILS_MASTER_KEY}
ENV testvar ${testvar}
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN echo "----------------key is 1. ${RAILS_MASTER_KEY}"
RUN echo "----------------key is 11. $( testvar )"
RUN echo "----------------key is 12. $testvar"
RUN echo "----------------key is 2. $[variables.RAILS_MASTER_KEY]"
RUN echo "----------------key is 4. $(RAILS_MASTER_KEY)"
RUN gem install bundler -v $BUNDLER_VERSION
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
RUN rm -f /app/tmp/pids/server.pid
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s", "-e", "production", "-b", "0.0.0.0"]

As you can see, I have echo trying when trying to debug this so please ignore all echo statements.
Any help/guide will be appreciated.
Thank you.

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

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

发布评论

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

评论(1

我家小可爱 2025-01-17 15:55:52

从上面的代码片段来看,您似乎正在使用 dockerComposeFileArgs 来指定环境变量。
对于类似情况(以及本地调试目的),一种派上用场的选项是在 Dockerfile 中使用 ARG
例如

FROM ruby:3.1.0-slim

RUN ...

ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY

...

,这样您就可以在构建时使用 Azure DevOps 中的 secret 变量指定 RAILS_MASTER_KEY

在这里您可以找到一些谈论 ENV 的有用帖子ARGS 关键字:

From the above snippets, it seems you're using the dockerComposeFileArgs to specify environment variables.
One option that comes in handy for similar situations (and for local debugging purposes) is to use ARG into Dockerfile.
e.g.

FROM ruby:3.1.0-slim

RUN ...

ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY

...

so you'll have the possibility to specify the RAILS_MASTER_KEY value at build-time using secret variables from Azure DevOps

Here you can find some useful posts talking about ENV and ARGS keywords:

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