我有一个与git push heroku master && heroku run rails db:migrate一起部署的Heroku应用程序。
我使用GitLab存储代码,但我希望能够:
都不离开我的指挥队伍。
另外,虽然我的堆栈使用Rails,但答案并不一定要使用Rails。它应该很容易适应任何堆栈。
发布于 2018-05-04 23:28:15
我刚刚发布了所有这些附加插图的博客文章,以及一个完整的、公开的示例应用程序。然而,它的要点是:
步骤1:设置Heroku应用程序
创建两个Heroku应用程序,用于分期和生产。我要打电话给我的toptal-pipeline和toptal-pipeline-staging,前者是生产应用。
注意使用heroku auth:token的auth令牌。
步骤2:设置GitLab 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如果您不使用Rails,一定要调整deploy_to_staging和deploy_to_production部分的script块以匹配您的部署步骤。
同样,对您的test块也这样做。当前运行的是Rspec和Rubocop。
步骤3:配置GitLab设置
HEROKU_EMAIL:您给Heroku的登录邮件HEROKU_AUTH_TOKEN:步骤1中的令牌。
v*标记添加到GitLab存储库设置中的受保护标记。步骤4:创建NPM脚本以使语义版本控制更容易
使用standard-version将devDependencies添加到yarn add --dev standard-version中,并向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:编写代码
您的新编码工作流程将如何:
master以部署到暂存此外,要使用新的语义版本号进行标记,请运行:
yarn patch以增加修补程序的数目。例如x.x.1 => x.x.2yarn minor以增加次要数字。例如x.1.x => x.2.xyarn major以增加主数。例如1.x.x => 2.x.x去建造一些很棒的东西。
https://stackoverflow.com/questions/50184249
复制相似问题