如何使用 azure 管道变量为我的 docker 映像设置环境变量?
我有一个天蓝色的管道,我想用它来部署我的 Rails 应用程序。 该应用程序有一个 Dockerfile 和一个 docker-compose 文件。
我试图将 RAILS_MASTER_KEY 设置为管道中的秘密变量,然后将其引用为我的 docker compose 文件中的环境变量。
我可以确认代理正在使用管道 yaml 中的 echo 正确设置变量。但是,环境变量没有正确传递/设置到我的 docker-compose 和最终的 Dockerfile。
我花了几天时间阅读天蓝色文档、堆栈溢出和它无法正常工作来解决这个问题。
这是我的代码:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从上面的代码片段来看,您似乎正在使用 dockerComposeFileArgs 来指定环境变量。
对于类似情况(以及本地调试目的),一种派上用场的选项是在
Dockerfile
中使用ARG
。例如
,这样您就可以在构建时使用 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
intoDockerfile
.e.g.
so you'll have the possibility to specify the
RAILS_MASTER_KEY
value at build-time usingsecret
variables from Azure DevOpsHere you can find some useful posts talking about
ENV
andARGS
keywords: