我正在用Github创建一个构建管道,它将在每个请求请求上创建一个检查运行,以分析我的应用程序的性能。我已经创建了Github应用并将其安装到我的仓库中,并生成了私钥。我需要执行Authenticating as an installation
才能获得访问令牌。
But as per the docs要获取安装的访问令牌,首先我必须获取该应用程序的安装列表,并必须从该列表中找到特定的安装。但是我不知道如何为安装我的应用程序的特定存储库上引发的请求请求事件识别应用程序的特定installation_id。
我不知道我在想什么。
答案 0 :(得分:0)
如果您的CI / CD无法与GitHub的webhooks配合使用(例如,由于防火墙),则可以执行以下操作:
#!/bin/bash
HEADER=$( echo -n {\"alg\":\"RS256\"} | base64 | tr -d '=' )
PAYLOAD=$( echo -n \{\"iat\":$(date -u '+%s'),\"exp\":$(( $( date -u '+%s' ) + 600 )),\"iss\":$GITHUB_APP_ID\} | base64 | tr -d '\n' | tr -d '=' | tr / _ | tr + - )
SIGNATURE=$( echo -n "$HEADER.$PAYLOAD" | openssl dgst -sha256 -sign ./private_key -binary | openssl base64 | tr -d '\n' | tr -d '=' | tr / _ | tr + - )
TOKEN=$HEADER.$PAYLOAD.$SIGNATURE
INSTALLATION_ID=$( curl -s -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github.antiope-preview+json" -H "Accept: application/vnd.github.machine-man-preview+json" https://api.github.com/app/installations | jq .[0].id )
INSTALLATION_TOKEN=$( curl -s -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github.antiope-preview+json" -H "Accept: application/vnd.github.machine-man-preview+json" -d '{"permissions":{ "checks":"write"}}' https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens | jq .token | tr -d '"' )
echo $INSTALLATION_TOKEN
或者在Python中:
# https://gist.github.com/pelson/47c0c89a3522ed8da5cc305afc2562b0
import json
import os
import time
import jwt
import requests
from cryptography.hazmat.backends import default_backend
github_app_id = os.environ['GITHUB_APP_ID']
time_since_epoch_in_seconds = int(time.time())
cert_bytes = open("private_key", 'r').read().encode()
private_key = default_backend().load_pem_private_key(cert_bytes, None)
payload = {
# issued at time
'iat': time_since_epoch_in_seconds,
# JWT expiration time (10 minute maximum)
'exp': time_since_epoch_in_seconds + (10 * 60),
# GitHub App's identifier
'iss': github_app_id
}
jwt = jwt.encode(payload, private_key, algorithm='RS256')
headers = {"Authorization": "Bearer {}".format(jwt.decode()),
"Accept": "application/vnd.github.machine-man-preview+json",
"Accept": "application/vnd.github.antiope-preview+json"}
resp = requests.get(
'https://api.github.com/app/installations', headers=headers)
installation_id = json.loads(resp.content.decode())[0]['id']
data = '{"permissions":{ "checks":"write"}}'
resp = requests.post(
'https://api.github.com/app/installations/' + str(installation_id) + '/access_tokens', headers=headers, data=data)
installation_token = json.loads(resp.content.decode())['token']
print(installation_token)
花了我一段时间,但此代码有效。您需要GITHUB_APP_ID
和文件private_key
包含由GitHub生成的私钥。
然后您可以编辑检查运行,如下所示:
curl -s -H "Authorization: token $GITHUB_APP_INSTALLATION_TOKEN" -H "Accept: application/vnd.github.antiope-preview+json" -H "Accept: application/vnd.github.machine-man-preview+json" -d @body.json https://api.github.com/repos/cloud-pi/$GITHUB_REPO/check-runs
body.json
是包含以下数据的JSON文件:https://developer.github.com/v3/checks/runs/
答案 1 :(得分:0)
从本质上讲,与代码无关,可以调用https://api.github.com/app/installations API方法来获取最新的安装。但是,您必须确保最新的是您所需要的一种,或者您已经收到了正确的用户/组织,以便您可以在搜索结果中搜索正确的一种。否则,您将需要使用网络挂钩(https://developer.github.com/apps/quickstart-guides/setting-up-your-development-environment/#authenticating-as-an-installation)。