我如何使用 GitOps 通过 GitLab 测试我的应用程序并将其部署到 Heroku CI
How do I use GitOps to test & deploy my application to Heroku with GitLab CI
我有一个使用 git push heroku master && heroku run rails db:migrate
部署的 Heroku 应用程序。
我使用 GitLab 来存储我的代码,但我希望能够:
- 测试每个推送和合并请求
- 部署到暂存环境
- 部署到生产环境
所有这些都无需离开我的命令行。
此外,虽然我的堆栈在 Rails 上使用 Ruby,但答案不一定必须使用 Rails。它应该很容易适应任何堆栈。
我刚刚发布了 a blog post,其中包含所有这些加上插图,以及一个完整的 public 示例应用程序。然而,它的要点是:
第 1 步:设置您的 Heroku 应用程序
创建两个用于暂存和生产的 Heroku 应用程序。我将调用我的 toptal-pipeline
和 toptal-pipeline-staging
,前者是生产应用程序。
记下您使用 heroku auth:token
的授权令牌。
步骤 2:设置 Git实验室 CI
将其粘贴到项目根目录的 .gitlab-ci.yml
中,分别用您的 Heroku 应用程序名称替换 APPNAME_PRODUCTION
和 APPNAME_STAGING
的值:
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
如果您不在 [=120= 上使用 Ruby,请务必调整 deploy_to_staging
和 deploy_to_production
部分的 script
块以匹配您的部署步骤].
此外,对您的 test
块执行相同的操作。当前的 运行s Rspec 和 Rubocop.
第 3 步:配置您的 Git实验室设置
- 将两个秘密变量添加到您的 Git实验室 CI 设置
HEROKU_EMAIL
:您的 Heroku 登录邮箱
HEROKU_AUTH_TOKEN
:您在第 1 步中获得的令牌。
- 将
v*
个标签添加到 Git 实验室存储库设置中的受保护标签。
第 4 步:创建 NPM 脚本以简化语义版本控制
使用 yarn add --dev standard-version
将 standard-version
添加到您的 devDependencies
并将以下脚本添加到 package.json
:
"scripts": {
"release": "standard-version",
"major": "yarn release --release-as major",
"minor": "yarn release --release-as minor",
"patch": "yarn release --release-as patch"
}
此外,将版本号添加到您的 package.json
文件中:
"version": "0.0.7"
第 5 步:编写代码
您的新编码工作流程将如何:
- 写代码
- Git 推送到 运行 测试
- Git 推送到
master
以部署到暂存
- 使用 semantic versioning 标记发布并推送以部署到生产环境
此外,要标记新的语义版本号,运行:
yarn patch
增加补丁号。例如。 x.x.1
=> x.x.2
yarn minor
增加次要号码。例如。 x.1.x
=> x.2.x
yarn major
增加主编号。例如。 1.x.x
=> 2.x.x
去创造一些很棒的东西。
我有一个使用 git push heroku master && heroku run rails db:migrate
部署的 Heroku 应用程序。
我使用 GitLab 来存储我的代码,但我希望能够:
- 测试每个推送和合并请求
- 部署到暂存环境
- 部署到生产环境
所有这些都无需离开我的命令行。
此外,虽然我的堆栈在 Rails 上使用 Ruby,但答案不一定必须使用 Rails。它应该很容易适应任何堆栈。
我刚刚发布了 a blog post,其中包含所有这些加上插图,以及一个完整的 public 示例应用程序。然而,它的要点是:
第 1 步:设置您的 Heroku 应用程序
创建两个用于暂存和生产的 Heroku 应用程序。我将调用我的 toptal-pipeline
和 toptal-pipeline-staging
,前者是生产应用程序。
记下您使用 heroku auth:token
的授权令牌。
步骤 2:设置 Git实验室 CI
将其粘贴到项目根目录的 .gitlab-ci.yml
中,分别用您的 Heroku 应用程序名称替换 APPNAME_PRODUCTION
和 APPNAME_STAGING
的值:
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
如果您不在 [=120= 上使用 Ruby,请务必调整 deploy_to_staging
和 deploy_to_production
部分的 script
块以匹配您的部署步骤].
此外,对您的 test
块执行相同的操作。当前的 运行s Rspec 和 Rubocop.
第 3 步:配置您的 Git实验室设置
- 将两个秘密变量添加到您的 Git实验室 CI 设置
HEROKU_EMAIL
:您的 Heroku 登录邮箱HEROKU_AUTH_TOKEN
:您在第 1 步中获得的令牌。
- 将
v*
个标签添加到 Git 实验室存储库设置中的受保护标签。
第 4 步:创建 NPM 脚本以简化语义版本控制
使用 yarn add --dev standard-version
将 standard-version
添加到您的 devDependencies
并将以下脚本添加到 package.json
:
"scripts": {
"release": "standard-version",
"major": "yarn release --release-as major",
"minor": "yarn release --release-as minor",
"patch": "yarn release --release-as patch"
}
此外,将版本号添加到您的 package.json
文件中:
"version": "0.0.7"
第 5 步:编写代码
您的新编码工作流程将如何:
- 写代码
- Git 推送到 运行 测试
- Git 推送到
master
以部署到暂存 - 使用 semantic versioning 标记发布并推送以部署到生产环境
此外,要标记新的语义版本号,运行:
yarn patch
增加补丁号。例如。x.x.1
=>x.x.2
yarn minor
增加次要号码。例如。x.1.x
=>x.2.x
yarn major
增加主编号。例如。1.x.x
=>2.x.x
去创造一些很棒的东西。