6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CircleCI のジョブから GitHub の Pull Request を作る

Last updated at Posted at 2019-01-29

やりたいこと

いろいろあって、CircleCI のジョブからプルリクエストを作りたい。

やり方

hub コマンドを使う。

使用するバージョン

  • hub: 2.8.3
  • CircleCI: 2.0

1. OAuth トークンを用意する

man hub 曰く、
GITHUB_TOKEN 環境変数に、 repo の permission を付与した GitHub の OAuth トークンをセットしておけばよさそう。

トークンの作り方: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

2. CircleCI プロジェクトの環境変数にトークンをセットする

前手順で取得したトークンをGITHUB_TOKENにセットするわけですが、
このトークンは自分以外から見られると困るので、
プロジェクト設定で環境変数をセットします。

プロジェクトの環境変数のセットの仕方: https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-project

3. CircleCI の job を書く

hubがインストール済みのいい感じのコンテナが見つからなかったので自前でダウンロードします。
ただし別にここで毎回インストールしなくても、コンテナレジストリにhubインストール済みのイメージを上げておけばそれでOKです。

BASE_BRANCHHEAD_BRANCHは適宜うめてください。

config.yml
(略)
jobs:
  create-pull-request:
    docker:
      - image: circleci/golang:1.11-stretch
    steps:
      - checkout
      - run:
          name: Install hub command
          command: |
            curl -sSLf https://github.com/github/hub/releases/download/v2.8.3/hub-linux-amd64-2.8.3.tgz | \
            tar zxf - --strip-components=1 -C /tmp/ && \
            mv /tmp/bin/hub /usr/local/bin/hub
      - run:
          name: Create a pull request
          command: |
            hub pull-request --message="あああ" --base=${CIRCLE_PROJECT_USERNAME}:${BASE_BRANCH} --head=${CIRCLE_PROJECT_USERNAME}:${HEAD_BRANCH}
(略)

注意点

GITHUB_TOKENがセットされていることによって使えるようになるhubのサブコマンドは、
GitHub の WebAPI を使っているものだけです。

hubコマンドは hub push など git リポジトリに対するサブコマンドも内包していますが、
これらを使うには別途 git リポジトリにアクセスするための設定が必要となります。
具体的には SSH 鍵の設定などを行う必要があります。

詳しくはこちら: https://circleci.com/docs/2.0/gh-bb-integration

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?