在 API 上创建 Gitlab 存储库并提供基本设置
Create Gitlab Repo and Provide Basic Setup over API
我想通过脚本实现以下任务:
- 在 Gitlab 上新建 Project/Repo
- 创建两个受保护的分支:
main
和 stable
- 选择合并策略:“Semilinear with Merge Commits”
- 允许维护者推送到 main(Jenkins-CI 将作为维护者访问 repo)
- 允许开发者+维护者合并
- 讨论必须在合并前解决
- 合并前验证构建必须成功
- 不允许合并到
stable
我想到了这个方法:
创作
git init
git remote add origin git@gitlab.com/....
git checkout -b main
git commit -a --allow-empty -m"Initial Commit!"
git push origin main
git checkout -b stable
git push origin stable
配置
#! /bin/bash
GITLAB_PROJECT_ID=
GITLAB_TOKEN=
repo_config=$(cat <<EOF
{
"merge_method" : "rebase_merge",
"auto_devops_enabled" : false,
"only_allow_merge_if_pipeline_succeeds" : true,
"merge_requests_enabled" : true,
"allow_merge_on_skipped_pipeline" : false,
"only_allow_merge_if_all_discussions_are_resolved" : true,
"visibility" : "internal"
}
EOF
)
main_branch_config=$(cat <<EOF
{
"name": "main",
"allowed_to_push": [{"access_level": 40}],
"allowed_to_merge": [{
"access_level": 30
},{
"access_level": 40
}
]}
EOF
)
stable_branch_config=$(cat <<EOF
{
"name": "stable",
"allowed_to_push": [{"access_level": 40}],
"allowed_to_merge": [{"access_level": 0}]
}
EOF
)
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID" \
-i \
-X PUT \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$repo_config"
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/protected_branches" \
-X POST \
-i \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$main_branch_config"
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/protected_branches" \
-i \
-X POST \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$stable_branch_config"
注意受保护分支的权限设置仅支持“高级”安装。
我想通过脚本实现以下任务:
- 在 Gitlab 上新建 Project/Repo
- 创建两个受保护的分支:
main
和stable
- 选择合并策略:“Semilinear with Merge Commits”
- 允许维护者推送到 main(Jenkins-CI 将作为维护者访问 repo)
- 允许开发者+维护者合并
- 讨论必须在合并前解决
- 合并前验证构建必须成功
- 不允许合并到
stable
我想到了这个方法:
创作
git init
git remote add origin git@gitlab.com/....
git checkout -b main
git commit -a --allow-empty -m"Initial Commit!"
git push origin main
git checkout -b stable
git push origin stable
配置
#! /bin/bash
GITLAB_PROJECT_ID=
GITLAB_TOKEN=
repo_config=$(cat <<EOF
{
"merge_method" : "rebase_merge",
"auto_devops_enabled" : false,
"only_allow_merge_if_pipeline_succeeds" : true,
"merge_requests_enabled" : true,
"allow_merge_on_skipped_pipeline" : false,
"only_allow_merge_if_all_discussions_are_resolved" : true,
"visibility" : "internal"
}
EOF
)
main_branch_config=$(cat <<EOF
{
"name": "main",
"allowed_to_push": [{"access_level": 40}],
"allowed_to_merge": [{
"access_level": 30
},{
"access_level": 40
}
]}
EOF
)
stable_branch_config=$(cat <<EOF
{
"name": "stable",
"allowed_to_push": [{"access_level": 40}],
"allowed_to_merge": [{"access_level": 0}]
}
EOF
)
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID" \
-i \
-X PUT \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$repo_config"
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/protected_branches" \
-X POST \
-i \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$main_branch_config"
curl "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/protected_branches" \
-i \
-X POST \
-H "content-type:application/json" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-d "$stable_branch_config"
注意受保护分支的权限设置仅支持“高级”安装。