Test and deploy a Ruby application with GitLab CI/CD

原文:https://docs.gitlab.com/ee/ci/examples/test-and-deploy-ruby-application-to-heroku.html

Test and deploy a Ruby application with GitLab CI/CD

该示例将指导您如何在 Ruby on Rails 应用程序中运行测试,以及如何将其自动部署为 Heroku 应用程序.

您还可以查看或派生完整的示例源,并查看其过去CI 作业的日志.

Configure the project

这是此项目的.gitlab-ci.yml文件的外观:

test:
  stage: test
  script:
  - apt-get update -qy
  - apt-get install -y nodejs
  - bundle install --path /cache
  - bundle exec rake db:create RAILS_ENV=test
  - bundle exec rake test

staging:
  stage: deploy
  script:
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-ruby-test-staging --api-key=$HEROKU_STAGING_API_KEY
  only:
  - master

production:
  stage: deploy
  script:
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-ruby-test-prod --api-key=$HEROKU_PRODUCTION_API_KEY
  only:
  - tags 

这个项目有三个工作:

  • test -用于测试 Rails 应用程序.
  • staging -用于每次推送到master分支时自动部署登台环境.
  • production -用于为每个创建的标签自动部署生产环境.

Store API keys

您需要在项目的设置> CI / CD>环境变量中创建两个变量

  • HEROKU_STAGING_API_KEY -Heroku API 密钥,用于部署登台应用程序.
  • HEROKU_PRODUCTION_API_KEY -Heroku API 密钥,用于部署生产应用程序.

Find your Heroku API key in Manage Account.

Create Heroku application

对于每个环境,您都需要创建一个新的 Heroku 应用程序. 您可以通过Heroku 仪表板执行此操作.

Create Runner

首先安装Docker Engine .

要构建此项目,您还需要安装GitLab Runner . 您可以使用gitlab.com上的公共跑步者或注册自己的跑步者. 首先创建模板配置文件以通过复杂的配置:

cat > /tmp/test-config.template.toml << EOF [[runners]]
[runners.docker]
[[runners.docker.services]]
name = "postgres:latest" EOF 

最后,注册运行器,并传递新创建的模板配置文件:

gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --description "ruby:2.6" \
  --executor "docker" \
  --template-config /tmp/test-config.template.toml \
  --docker-image ruby:2.6 

使用上面的命令,您将创建一个使用ruby:2.6图像并使用PostgreSQL数据库的 Runner.

要访问 PostgreSQL 数据库,请以没有密码的用户postgres身份连接到host: postgres .

Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2021-03-27 13:48:25

results matching ""

    No results matching ""