gitlab服務作為公司的代碼庫比較重要,一般可由專門的運維工程師安裝配置管理,基本流程大同小異,大概如下:
vim /etc/yum.repos.d/gitlab-ce.repo
添加內容:
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6
Repo_gpgcheck=0
Enabled=1
Gpgkey=https://packages.gitlab.com/gpg.key
更新yum緩存
sudo yum makecache
建議安裝GitLab社區版日常夠用了,企業版可以找極狐(國內gitlab)洽談商務,當然個人破解的話,公司使用有風險,後果自負哈
yum install gitlab-ce # 自動安裝最新版
yum install gitlab-ce-x.x.x #安裝指定版本
安裝可參照 https://developer.aliyun.com/article/74395
sudo gitlab-ctl start # 啟動所有 gitlab 組件;
sudo gitlab-ctl stop # 停止所有 gitlab 組件;
sudo gitlab-ctl restart # 重啟所有 gitlab 組件;
sudo gitlab-ctl status # 查看服務狀態;
sudo gitlab-ctl reconfigure # 啟動服務;
sudo vim /etc/gitlab/gitlab.rb # 修改默認的配置文件;
gitlab-rake gitlab:check SANITIZE=true --trace # 檢查gitlab;
sudo gitlab-ctl tail # 查看日志;
gitlab是用於倉庫管理系統的開源項目,使用git作為代碼管理工具,並在此基礎上搭建起來的web服務。
一般支持token和ssh兩種。登錄gitlab-》右上角settings:
ssh-keygen -o -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa
cat id_rsa.pub
ssh -T [email protected]_hostname
pip install python-gitlab
import os
import time
from AuthineAcc.settings import GITLAB_PRIVATE_TOKEN, GITLAB_URL
import gitlab
import logging
# 日志格式設置
logger = logging.getLogger('gitlab')
class GitLabManage(object):
def __init__(self, user_token, gitlab_url):
self.user_token = user_token
self.gitlab_url = gitlab_url
# token登錄
self.client = gitlab.Gitlab(url=self.gitlab_url, private_token=self.user_token, timeout=120)
def get_all_projects(self):
# 獲取所有項目
return self.client.projects.list(all=True)
def get_all_users(self):
# 獲取所有用戶
return self.client.users.list(all=True)
def get_all_groups(self):
# 獲取所有權限組
return self.client.groups.list(all=True)
def get_user_by_id(self, pk):
# 根據用戶id查找用戶詳情
try:
user = self.client.users.get(pk)
except gitlab.exceptions.GitlabGetError as e:
return None
return user
def get_project_branches(self, name, path_with_namespace):
# 根據項目名稱和組名查找項目
pros = self.client.projects.list(search=name, obey_rate_limit=False)
res = []
if not pros:
return res
pro = None
for _pro in pros:
if _pro.path_with_namespace == path_with_namespace:
pro = _pro
if pro:
res = pro.branches.list(all=True)
return res
return res
def import_project(self, data):
# 根據file(xxx.tar.gz)導入項目
logger.info(data)
if not data:
return None
try:
res = self.client.projects.import_project(file=open(data['file'], 'rb'), path=data['path'],
name=data['name'], namespace=data['group'])
except Exception as e:
logger.error(e)
return None
logger.info(res)
return res
def export_project_by_id(self, pk, file_dir, group=None):
# 根據項目id導出項目(xxx.tar.gz)
try:
logger.info(file_dir)
pro = self.client.projects.get(int(pk))
logger.info(pro.name)
export = pro.exports.create()
export.refresh()
while export.export_status != 'finished':
time.sleep(1)
export.refresh()
logger.info(export.export_status)
# Download the result
file_path = os.path.join(file_dir, '%s.tar.gz' % pro.name)
logger.info(file_path)
with open(file_path, 'wb') as f:
export.download(streamed=True, action=f.write)
except Exception as e:
logger.error(e)
return None
return {'file': file_path, 'name': pro.name, 'path': pro.name, 'group': group}
if __name__ == "__main__":
glm = GitLabManage(GITLAB_PRIVATE_TOKEN, GITLAB_URL)
user = glm.get_user_by_id(1)
print(user.name)
import csv
def write_to_csv(file_path, data):
with open(file_path, mode='a', newline='', encoding='utf-8') as f:
write = csv.writer(f, dialect='excel')
write.writerows(data)
def read_csv(file_path):
data = []
f = csv.reader(open(file_path))
logger.info(f)
for line in f:
if line:
data.append({'id': line[0], 'name': line[1], 'group': line[2], 'newgroup': line[3]})
return data