我在AWS Codepipeline构建阶段添加了环境变量。现在,当运行RUN bundle exec rake db:migrate RAILS_ENV=production
命令时,它会尝试查找在database.yml文件中查找的生产数据库。我想访问在database. yml中的AWS Codepipeline构建阶段添加的env变量。
我如何才能实现这一点?如果有任何替代方案比也打开它。请找到下面的文件...
Database.yml
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: Backend_development
test:
<<: *default
database: JSMA_Backend_test
production:
<<: *default
username: <%= ENV['DATABASE_USER'] %>
database: <%= ENV['DATABASE_NAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'] %>
停靠文件
# Use an official Ruby runtime as a parent image
FROM ruby:3.1-alpine
# Set the working directory in the container to /app
WORKDIR /app
# Install build dependencies
RUN apk update && apk add --virtual build-dependencies build-base
# Install imagemagick
RUN apk add imagemagick
# Install the PostgreSQL client library
RUN apk add postgresql-dev
# Copy the Gemfile and Gemfile.lock from your host machine to the container
COPY Gemfile Gemfile.lock ./
# Install RubyGems and Bundler
RUN gem update --system && gem install bundler
# Install the RubyGems specified in the Gemfile
RUN bundle install --without development test
# Copy the rest of your application code from your host machine to the container
COPY . .
RUN bundle exec rake db:migrate RAILS_ENV=production
# Start the Rails server
CMD ["bundle", "exec", "rails", "server", "-e", "production"]
# CMD ["bundle", "exec", "rails", "server"]
对接器-组合
version: '3'
services:
db:
image: postgres:13
environment:
POSTGRES_USER: <%= ENV['DATABASE_USER'] %>
POSTGRES_PASSWORD: <%= ENV['DATABASE_PASSWORD'] %>
POSTGRES_DB: <%= ENV['DATABASE_NAME'] %>
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
buildspec.yml
version: 0.2
phases:
pre_build:
commands:
- export DATABASE_HOST=$DATABASE_HOST
- export DATABASE_NAME=$DATABASE_NAME
- export DATABASE_USER=$DATABASE_USER
- export DATABASE_PASSWORD=$DATABASE_PASSWORD
- export DATABASE_PORT=$DATABASE_PORT
- echo $DATABASE_HOST
- echo AWS ECR login
- $(aws ecr get-login --no-include-email --region ap-south-1)
- REPO_URI=*** repo here
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
build:
commands:
- echo Building docker image
- docker build -t jsm-rails .
- docker tag //repo****:latest
post_build:
commands:
- echo build successful!
- echo pushing image to ecr...
- docker push //repo url here *** rails:latest
- echo pushed image!
如果有任何替代方案,也为它开放。
1条答案
按热度按时间vq8itlhq1#
通过在buildspec.yml中的预构建阶段添加行来实现