10

有没有人知道任何关于让 Jenkins 与 Heroku 一起玩的好文章?

我想做的是:

1) Setup a Jenkins job to poll a private GitHub Repo when check-ins are made to developer branch.
2) Build this branch and make sure everything is good!
3) Push private GitHub Repo codebase to Heroku Repo. So it does the build and deploy on Heroku.

我看过一些零碎的文章,但似乎无法让完整的流程正常工作。我已经尝试过 GitHub 插件和 Heroku 插件。我可以让 GitHub 插件下拉并构建,但我不明白如何推送到 Heroku。Heroku 插件让我可以部署一个 WAR 文件,但这并不能正确启动应用程序。所以我只需要将代码库推送到 Heroku 存储库,以便它进行编译和部署。

4

3 回答 3

12

我使用 Jenkins 为我们的应用推送到 Heroku。我不使用 Heroku 插件,我喜欢 'Execute Shell' 给我的控制。这是一个相当详细的答案,如果我错过了什么,请务必发表评论。

1)轮询私人回购:

  • 您的工作应该在“构建触发器”部分下使用“在将更改推送到 GitHub 时构建”选项进行设置。
  • 在 GitHub 上转到您的项目页面,然后单击标题中的“设置”菜单(必须具有管理员访问权限)。在该页面的左侧边栏中,单击“服务挂钩”。这将带您进入您可以选择的钩子列表。选择“Jenkins(GitHub 插件)”。
  • 添加您的 jenkins 服务器的回调 URL(让该页面稍后打开)。就像是 :

    http://jenkins.example.com/github-webhook/

  • 您可以通过转到 Jenkins|Manage Jenkins|System Log 添加来自 Jenkins 的“日志记录器”来测试回调。单击“添加新日志记录器”
  • 输入“test hook”,将 Logger 设置为“com.cloudbees.jenkins.GitHubWebHook”,将 Log Level 设置为“All”
  • 返回 GitHub,然后单击“Test Hook”,然后您可以看到回调日志以确认您的 hook 正在工作。

2)建立分支。确保您已设置所有 GitHub 配置,因为只有完成这些设置后,回调才会触发作业。

  • 在“源代码管理”部分,选择“Git”选项并填写您的仓库的详细信息,例如“git@github.com:...”
  • 在“构建触发器”部分中,选择“将更改推送到 GitHub 时构建”

3) 推送到 Heroku。这里有几件事要考虑。

  • 您需要确保您的工作添加了 Heroku 远程仓库。创建新作业时,这是一次性操作,不需要每次构建都执行。例如 :
heroku git:remote -a myApp
git remote add heroku git@heroku.com:myApp.git

仅使用上述内容创建一个 Execute Shell 脚本,仅用于您的第一次构建。

  • 如果您有 Ping 目标(New Relic),请在部署期间禁用它们以避免错误通知您的应用已关闭。
curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/disable -X POST -H "X-Api-Key: APIKEY"

不要忘记在以下情况后重新打开它:

curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/enable -X POST -H "X-Api-Key: APIKEY"
  • 对应用程序上的维护模式执行相同操作
heroku maintenance:on --app myApp
heroku maintenance:off --app myApp

综上所述,Jenkins 上的典型部署脚本可能如下所示:

#one off to ensure heroku remote is added to repo
heroku git:remote -a myApp
git remote add heroku git@heroku.com:myApp.git
#disbales
curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/disable -X POST -H "X-Api-Key: APIKEY"
heroku maintenance:on --app myApp
#push to heroku
git push --force heroku master
heroku run rake db:migrate --app myApp
#enables
curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/enable -X POST -H "X-Api-Key: APIKEY"
heroku maintenance:off --app myApp
于 2013-05-30T16:25:39.747 回答
10

作为使用上述 Heroku API 进行部署的替代方法,您可以简单地将代码推送到远程 Git 存储库(即为您的应用程序定义的一个 Heroku)作为构建后操作。因此,您的工作将定义两个 Git 存储库——一个是您的 Github 存储库,另一个是 Heroku 存储库。

在此处输入图像描述

为 Heroku 存储库命名,例如“heroku”,并在“构建后操作”部分中,使用 Git 发布者。请务必在 Target Remote name 字段中选择 heroku 名称。 在此处输入图像描述

根据您在 Github 项目上设置构建触发器的方式,当构建完成时,Jenkins 会将生成的快照推送到 Heroku 存储库,从而进行部署。

于 2013-12-29T17:44:14.337 回答
1

First you should switch your project to use ssh-keys for authentication and add your public ssh-key to your heroku account:

heroku git:remote --ssh-git
heroku keys:add

With

git remote -v
heroku  git@heroku.com:your-project.git (fetch)
heroku  git@heroku.com:your-project.git (push)

you can figure out the URL to use as repository URL in your Jenkins configuration. Then follow the instructions given in the answer from @Andy Glover (see also http://thediscoblog.com/blog/2014/01/24/continuous-delivery-for-heroku-with-jenkins/) to configure your Jenkins.

Finally to allow Jenkins to push to your git repository at Heroku, you should ask your Jenkins administrator to give you the public ssh key of the root/jenkins user executing your Jenkins job. Add this public key via your Heroku dashboard to your Heroku account. If your build job is successful, Jenkins will then be able to push to your Heroku account, resulting in a deployment.

于 2015-05-06T15:15:36.260 回答