首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GitOps测试并使用GitLab CI将应用程序部署到Heroku

如何使用GitOps测试并使用GitLab CI将应用程序部署到Heroku
EN

Stack Overflow用户
提问于 2018-05-04 23:28:15
回答 1查看 575关注 0票数 1

我有一个与git push heroku master && heroku run rails db:migrate一起部署的Heroku应用程序。

我使用GitLab存储代码,但我希望能够:

  • 测试每个推送和合并请求
  • 部署到暂存环境
  • 部署到生产

都不离开我的指挥队伍。

另外,虽然我的堆栈使用Rails,但答案并不一定要使用Rails。它应该很容易适应任何堆栈。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-04 23:28:15

我刚刚发布了所有这些附加插图的博客文章,以及一个完整的、公开的示例应用程序。然而,它的要点是:

步骤1:设置Heroku应用程序

创建两个Heroku应用程序,用于分期和生产。我要打电话给我的toptal-pipelinetoptal-pipeline-staging,前者是生产应用。

注意使用heroku auth:token的auth令牌。

步骤2:设置GitLab CI

将其粘贴到项目根部的.gitlab-ci.yml中,分别用Heroku应用程序名替换APPNAME_PRODUCTIONAPPNAME_STAGING的值:

代码语言:javascript
复制
image: ruby:2.4

before_script:
  - >
   : "${HEROKU_EMAIL:?Please set HEROKU_EMAIL in your CI/CD config vars}"
  - >
   : "${HEROKU_AUTH_TOKEN:?Please set HEROKU_AUTH_TOKEN in your CI/CD config vars}"
  - curl https://cli-assets.heroku.com/install-standalone.sh | sh
  - |
    cat >~/.netrc <<EOF
    machine api.heroku.com
      login $HEROKU_EMAIL
      password $HEROKU_AUTH_TOKEN
    machine git.heroku.com
      login $HEROKU_EMAIL
      password $HEROKU_AUTH_TOKEN
    EOF
  - chmod 600 ~/.netrc
  - git config --global user.email "ci@example.com"
  - git config --global user.name "CI/CD"

stages:
  - test
  - deploy

variables:
  APPNAME_PRODUCTION: toptal-pipeline
  APPNAME_STAGING: toptal-pipeline-staging

test:
  stage: test
  variables:
    POSTGRES_USER: test
    POSTGRES_PASSSWORD: test-password
    POSTGRES_DB: test
    DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSSWORD}@postgres/${POSTGRES_DB}
    RAILS_ENV: test
  services:
    - postgres:alpine
  before_script:
    - curl -sL https://deb.nodesource.com/setup_8.x | bash
    - apt-get update -qq && apt-get install -yqq nodejs libpq-dev
    - curl -o- -L https://yarnpkg.com/install.sh | bash
    - source ~/.bashrc
    - yarn
    - gem install bundler  --no-ri --no-rdoc
    - bundle install -j $(nproc) --path vendor
    - bundle exec rake db:setup RAILS_ENV=test
  script:
    - bundle exec rake spec
    - bundle exec rubocop


deploy_to_staging:
  stage: deploy
  environment:
    name: staging
    url: https://$APPNAME_STAGING.herokuapp.com/
  script:
    - git remote add heroku https://git.heroku.com/$APPNAME_STAGING.git
    - git push heroku master
    - heroku pg:backups:capture --app $APPNAME_PRODUCTION
    - heroku pg:backups:restore `heroku pg:backups:url --app $APPNAME_PRODUCTION` --app $APPNAME_STAGING --confirm $APPNAME_STAGING
    - heroku run rails db:migrate --app $APPNAME_STAGING
  only:
    - master
    - tags

deploy_to_production:
  stage: deploy
  environment:
    name: production
    url: https://$APPNAME_PRODUCTION.herokuapp.com/
  script:
    - git remote add heroku https://git.heroku.com/$APPNAME_PRODUCTION.git
    - git push heroku master
    - heroku pg:backups:capture --app $APPNAME_PRODUCTION
    - heroku run rails db:migrate --app $APPNAME_PRODUCTION
  only:
    - /^v(?'MAJOR'(?:0|(?:[1-9]\d*)))\.(?'MINOR'(?:0|(?:[1-9]\d*)))\.(?'PATCH'(?:0|(?:[1-9]\d*)))(?:-(?'prerelease'[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(?:\+(?'build'[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$/
    # semver pattern above is adapted from https://github.com/semver/semver.org/issues/59#issuecomment-57884619

如果您不使用Rails,一定要调整deploy_to_stagingdeploy_to_production部分的script块以匹配您的部署步骤。

同样,对您的test块也这样做。当前运行的是Rspec和Rubocop。

步骤3:配置GitLab设置

  1. 将两个秘密变量添加到GitLab CI设置中
    • HEROKU_EMAIL:您给Heroku的登录邮件
    • HEROKU_AUTH_TOKEN:步骤1中的令牌。

  1. v*标记添加到GitLab存储库设置中的受保护标记。

步骤4:创建NPM脚本以使语义版本控制更容易

使用standard-versiondevDependencies添加到yarn add --dev standard-version中,并向package.json添加以下脚本

代码语言:javascript
复制
"scripts": {
  "release": "standard-version",
  "major": "yarn release --release-as major",
  "minor": "yarn release --release-as minor",
  "patch": "yarn release --release-as patch"
}

另外,将版本号添加到package.json文件中:

代码语言:javascript
复制
"version": "0.0.7"

步骤5:编写代码

您的新编码工作流程将如何:

  1. 写码
  2. Git推动运行测试
  3. Git推送到master以部署到暂存
  4. 使用语义版本化标记发行版并推动将其部署到生产中

此外,要使用新的语义版本号进行标记,请运行:

  • yarn patch以增加修补程序的数目。例如x.x.1 => x.x.2
  • yarn minor以增加次要数字。例如x.1.x => x.2.x
  • yarn major以增加主数。例如1.x.x => 2.x.x

去建造一些很棒的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50184249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档