关联 GitHub App 仓库
通过 Square Cloud GitHub App 将 GitHub 仓库和分支关联到应用。
POST
/
v2
/
apps
/
{app_id}
/
deploy
/
github-app
关联 GitHub App 仓库
curl --request POST \
--url https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"repositoryName": "<string>",
"repositoryBranch": "<string>"
}
'import requests
url = "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app"
payload = {
"repositoryName": "<string>",
"repositoryBranch": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({repositoryName: '<string>', repositoryBranch: '<string>'})
};
fetch('https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'repositoryName' => '<string>',
'repositoryBranch' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app"
payload := strings.NewReader("{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"status": "success",
"response": {
"repository": {
"id": 1234567,
"full_name": "octocat/hello-world",
"branch": "main"
}
}
}
该端点通过 Square Cloud GitHub App 将应用关联到 GitHub 仓库和分支,使每次推送到该分支时都会部署应用。它是设置 GitHub Webhook的推荐替代方案:不会存储任何个人访问令牌,并且每次部署都会在提交上显示为一个 Check Run。
调用前,请先将你的 GitHub 账户连接到 Square Cloud,并在仓库上安装 Square Cloud GitHub App。该仓库必须包含在你自己的 GitHub 账户所拥有的安装中;否则请求会以
REPOSITORY_NOT_AVAILABLE 失败。它支持具有 apps:deploy 权限范围的 API 密钥,并且只有应用所有者可以调用。
一个应用同一时间只能有一个 GitHub App 关联,因此在关联其他仓库之前,请先取消关联当前仓库;同一仓库和分支也不能同时关联到你的两个应用。关联会使获取当前部署的缓存响应失效。调用频率限制为每个用户每 60 秒 3 次,与取消关联共享该限额。
参数
string
必填
应用的 ID。你可以在应用控制台的 URL 中找到它。
string
必填
仓库的完整名称,格式为
owner/repository。string
必填
推送后会部署应用的分支。最多 256 个字符,且必须存在于仓库中。
响应
string
指示调用是否成功。成功时为
success,否则为 error。{
"status": "success",
"response": {
"repository": {
"id": 1234567,
"full_name": "octocat/hello-world",
"branch": "main"
}
}
}
常见错误
| 代码 | HTTP | 说明 |
|---|---|---|
MISSING_REQUIRED_FIELDS | 400 | 缺少 repositoryName 或 repositoryBranch。 |
INVALID_BRANCH_LENGTH | 400 | 分支名称超过 256 个字符。 |
BRANCH_NOT_FOUND | 400 | 该分支在仓库中不存在。 |
GIT_ALREADY_CONFIGURED | 400 | 该应用已有 GitHub App 仓库。请先取消关联。 |
REPOSITORY_NOT_AVAILABLE | 403 | Square Cloud GitHub App 未通过你的 GitHub 账户安装在该仓库上。 |
REPOSITORY_NOT_FOUND | 404 | 仓库不存在,或你的 GitHub 账户无法访问它。 |
APP_NOT_FOUND | 404 | 应用不存在,或你不是其所有者。 |
REPOSITORY_BRANCH_ALREADY_CONFIGURED | 409 | 你的另一个应用已关联此仓库和分支。 |
⌘I
关联 GitHub App 仓库
curl --request POST \
--url https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"repositoryName": "<string>",
"repositoryBranch": "<string>"
}
'import requests
url = "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app"
payload = {
"repositoryName": "<string>",
"repositoryBranch": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({repositoryName: '<string>', repositoryBranch: '<string>'})
};
fetch('https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'repositoryName' => '<string>',
'repositoryBranch' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app"
payload := strings.NewReader("{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/apps/{app_id}/deploy/github-app")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"repositoryName\": \"<string>\",\n \"repositoryBranch\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"status": "success",
"response": {
"repository": {
"id": 1234567,
"full_name": "octocat/hello-world",
"branch": "main"
}
}
}

