GitHubのPull Requestにプレビュー用URLを自動でコメント

git

概要

CircleCIの設定

いきなりですが回答です。全て変数で書いてあるので、コピペで使えます。

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:10

version: 2.1
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run: npm install
      - run: npm run build
      - persist_to_workspace:
          root: .
          paths:
            - build
  deploy-job:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Install newer version of awscli
          command: |
            sudo apt-get install python-dev python-pip
            pip install awscli --user
      - run:
          name: Deploy to S3
          command: /home/circleci/.local/bin/aws s3 sync build s3://${AWS_BUCKET_NAME}${CIRCLE_BRANCH}/ --delete --region=${AWS_S3_REGION}
      - run:
          name: Post URL
          command: |
            if [ ${CI_PULL_REQUEST} ]; then
              curl -X POST \
                -H 'Content-Type:application/json' \
                -u matsubo:${GITHUB_ACCESS_TOKEN} \
                -d "{\"body\":\"Deployed to:\nhttp://${AWS_BUCKET_NAME}.s3-website-${AWS_S3_REGION}.amazonaws.com/${CIRCLE_BRANCH}/\"}" \
                "https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/issues/${CIRCLE_PULL_REQUEST##*/}/comments"
            fi


workflows:
  version: 2
  build-deploy:
    jobs:
      - build
      - deploy-job:
          requires:
            - build

上記のYAMLにある変数に追加してawscliを使うために以下の2つの環境変数なども必要に応じて設定します。

  • AWS_ACCESS_KEY_ID – IAM ユーザーまたはロールに関連付けられる AWS アクセスキーを指定します。
  • AWS_SECRET_ACCESS_KEY – アクセスキーに関連付けられるシークレットキーを指定します。これは、基本的にアクセスキーの「パスワード」です。

うまく出来ると、下のような感じになります。

GithubのPull Requestにコメントを書き込む

中でも面倒だったのがGithubのAPIの認証です。エラーがわかりづらくて面倒でした。

まず、Personal Access Tokenこちらのページから作ります。2FAを使っている場合もこの手順でOKです。

権限は、以下の項目(Full control of private repositories)だけチェックをしておけばOKです。

curlコマンドでテストすると以下のようになります。エンドポイントURLのpathはpullではなく、issuesで良いです。

> curl -X POST -H 'Content-Type:application/json' -u matsubo:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  -d "{\"body\":\"GitHub APIからコメント投稿\"}" "https://api.github.com/repos/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxx/issues/14/comments" 
{
  "url": "https://api.github.com/repos/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxx/issues/comments/530435426",
  "html_url": "https://github.com/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxx/pull/14#issuecomment-530435426",
  "issue_url": "https://api.github.com/repos/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxx/issues/12",
  "id": 530435426,
  "node_id": "MDEyOklzc3VlQ29tbWVudDUzMDQzNTQyNg==",
  "user": {
    "login": "matsubo",
    "id": 98103,
    "node_id": "MDQ6VXNlcjk4MTAz",
    "avatar_url": "https://avatars0.githubusercontent.com/u/98103?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/matsubo",
    "html_url": "https://github.com/matsubo",
    "followers_url": "https://api.github.com/users/matsubo/followers",
    "following_url": "https://api.github.com/users/matsubo/following{/other_user}",
    "gists_url": "https://api.github.com/users/matsubo/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/matsubo/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/matsubo/subscriptions",
    "organizations_url": "https://api.github.com/users/matsubo/orgs",
    "repos_url": "https://api.github.com/users/matsubo/repos",
    "events_url": "https://api.github.com/users/matsubo/events{/privacy}",
    "received_events_url": "https://api.github.com/users/matsubo/received_events",
    "type": "User",
    "site_admin": false
  },
  "created_at": "2019-09-11T15:32:07Z",
  "updated_at": "2019-09-11T15:32:07Z",
  "author_association": "COLLABORATOR",
  "body": "GitHub APIからコメント投稿"
}

コメント