如何直接从我的Gitlab代码库部署到Heroku?

39

在我的团队中,我们使用Gitlab作为远程代码仓库,现在我们正在寻找一种自动部署应用到Heroku的解决方案。我们发现Codeship可以从Github自动部署应用到Heroku。

有什么技巧或诀窍吗?


2
http://doc.gitlab.com/ce/ci/deployment/README.html - karmiphuc
我已经阅读过这个了。在文档中,我们将设置一个GitLab Runner。它类似于Travis CI。 - Toanalien
@karmiphuc 404 ... - Charlie Schliesser
@CharlieSchliesser https://docs.gitlab.com/ee/ci/ - Toanalien
4个回答

54

如果您不想使用Ruby / dpl,请按以下步骤将代码部署到Heroku:

查找您的Heroku API密钥(在Heroku web控制台上的帐户设置-> API密钥),并将其作为Gitlab秘密变量提供,例如HEROKU_API_KEY(请注意,该值与heroku auth:token返回的值不同...)

然后在相关作业的.gitlab-ci.yml配置文件中添加两行脚本:

git remote add heroku https://heroku:$HEROKU_API_KEY@git.heroku.com/<name of your heroku app>.git

git push -f heroku HEAD:master
您可以在http://blog.thecodewhisperer.com/permalink/deploying-jekyll-to-heroku-using-gitlab-ci上查看详细说明。

16
在推送到 Heroku 时,确保添加 -q 标志,否则你会在输出末尾泄露 $HEROKU_API_KEY - Fery W
@Zsolt 当我这样做时,推送是空的(因此 Heroku 构建失败),我在这里缺少什么?我需要进行虚拟提交以初始化 heroku:master 吗? - Théo Champion
6
在 .gitlab-ci.yml 中运行 git remote add heroku ... 有时会导致 fatal: remote heroku already exists 的错误。为了强制返回代码为0,将其更改为 git remote add heroku ... || true - dancypants

33
这里是我找到的解决方案,以防链接失效:

配置项目

这个项目的 .gitlab-ci.yml 文件看起来像这样:

test:
  script:
  # this configures Django application to use attached postgres database that is run on `postgres` host
  - export DATABASE_URL=postgres://postgres:@postgres:5432/python-test-app
  - apt-get update -qy
  - apt-get install -y python-dev python-pip
  - pip install -r requirements.txt
  - python manage.py test

staging:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-python-test-staging --api-key=$HEROKU_STAGING_API_KEY
  only:
  - master

production:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-python-test-prod --api-key=$HEROKU_PRODUCTION_API_KEY
  only:
  - tags

这个项目有三项任务:

测试 - 用于测试 Django 应用程序,

暂存 - 用于在每次推送到主分支时自动部署暂存环境

生产 - 用于在每次创建标签时自动部署生产环境

存储 API 密钥

您需要在“项目” > “变量”中创建两个变量:

HEROKU_STAGING_API_KEY - Heroku API key used to deploy staging app,
HEROKU_PRODUCTION_API_KEY - Heroku API key used to deploy production app.

嗨,dnit13,路径Project > Variables在哪里?我在Gitlab中找不到它。 - Cynthia Sanchez
@CynthiaSanchez 如下所述,您将在Heroku帐户中找到API密钥。 - dnit13
它在“设置> CI / CD> 环境变量”处。 - MrKioZ

0
基于 GitLab-CI 文档,有一种部署工具可用于直接与以下服务提供商通信:Heroku、Cloud Foundry、AWS/S3 等。
以下是在作业脚本中使用 dpl 的基本用法。
staging:
  stage: deploy
  script:
    - apt-get update -yq
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=my-app-staging --api_key=$HEROKU_STAGING_API_KEY
  only:
    - main

对于那些在这里寻找答案的人,请阅读GitLab DPL文档


0

为了补充dnit13的回答:

确保您的环境变量未受保护。

前往设置 > CI/CD > 环境变量并取消选中受保护的变量

有关此线程的更多信息。


这并不是一个好主意。一些变量应该保密。更好的答案是:“确保只在受保护的分支上使用受保护的变量。” - Asriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接