GitLab:CI 构建的全局(实例级)变量

GitLab:CI 构建的全局(实例级)变量

如何在 GitLab 中设置不特定于项目但适用于所有项目的全局环境变量?经典用例是部署密钥、docker 注册表凭据和连接代理信息。

大家已经问了这个问题三年了,你可以在下面的链接中看到。不幸的是,这个问题已经解决了。

https://gitlab.com/gitlab-org/gitlab-foss/issues/3897

答案1

与此同时,我也帮助了自己。

设置

sudo vi gitlab_global_env.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# sudo pip3 install requests

import os
import requests
import sys
import time

gitlab_url = 'https://gitlab.example.com/api/v4/'

# https://gitlab.example.com/profile/personal_access_tokens
headers = {'PRIVATE-TOKEN': '***TODO***'}

lol = {
    'HTTP_PROXY': 'http://corporateproxy:8123',
    'HTTPS_PROXY': 'http://corporateproxy:8123',
    'NO_PROXY': '127.0.0.1,localhost,.local'  # max. 1024 chars!
}

r = requests.get(f'{gitlab_url}projects?per_page=100', headers=headers)  # max. 100 projects without paging!
projects = r.json()

for elt in projects:
    pid = elt['id']
    path_with_namespace = elt['path_with_namespace']
    visibility = elt['visibility']
    print(f'pid: {pid} -- path_with_namespace: {path_with_namespace} -- visibility: {visibility}')
    for k, v in lol.items():
        # https://docs.gitlab.com/ce/api/project_level_variables.html
        # https://docs.gitlab.com/ee/api/project_level_variables.html
        # Create:
        r = requests.post(f'{gitlab_url}/projects/{pid}/variables', data={'key': k, 'value': v}, headers=headers)
        create = r.json()
        if create.get('message'):
            # print(create)
            # Update:
            r = requests.put(f'{gitlab_url}/projects/{pid}/variables/{k}', data={'value': v}, headers=headers)
            update = r.json()
            if update.get('message'):
                print(update)

跑步

sudo python3 gitlab_global_env.py

示例输出

pid: 1 -- path_with_namespace: group/repo -- visibility: private

查看

Project,,,,,。SettingsCI / CD​​VariablesCollapse

在此处输入图片描述

使用 Python 3.6.8 测试

相关内容