欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Gitlab和Runner的安装和配置

程序员文章站 2022-06-11 19:03:31
...

Centos6安装Gitlab

参考 https://about.gitlab.com/downloads/ 可以从清华的镜像下载安装包, 注意区分自己用的是哪个发行版 https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

1. iptables打开80端口

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

2. 安装依赖包

yum install curl openssh-server openssh-clients postfix cronie

3. 启动postfix服务, 并设为默认开启

service postfix start
chkconfig postfix on

4. 安装gitlab

rpm -i gitlab-ce-8.10.2-ce.0.el6.x86_64.rpm 
# 安装结束后初始化配置 gitlab
gitlab-ctl reconfigure

5. 访问后发现会重定向回 http://localhost, 需要修改external_url

vi /etc/gitlab/gitlab.rb 
# 然后修改 external_url 'http://192.168.11.22'
# 然后reconfigure
gitlab-ctl reconfigure

安装就完成了

备份

查看/etc/gitlab/gitlab.rb里的配置信息

# gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
# gitlab_rails['backup_archive_permissions'] = 0644 # See: http://doc.gitlab.com/ce/raketasks/backup_restore.html#backup-archive-permissions
# gitlab_rails['backup_pg_schema'] = 'public'
# gitlab_rails['backup_keep_time'] = 604800

这是默认的备份配置, 需要检查一下默认的备份路径是否存在, 以及owner是否是git. 创建备份的命令很简单, 就是

gitlab-rake gitlab:backup:create

会依次导出postgresql, repository 和 uploads, 然后打成一个压缩包

恢复时备份文件必须在 /var/opt/gitlab/backups路径下, 命令

#gitlab-rake gitlab:backup:restore BACKUP=备份文件编号
gitlab-rake gitlab:backup:restore BACKUP=1502357536_2017_08_10_9.4.3

命令行重置root密码

在命令行下运行

# 切换到git用户
su - git
# 进入gitlab console
-sh-4.1$ gitlab-rails console production
Loading production environment (Rails 4.2.7)
# 令user为root用户
irb(main):001:0> user = User.where(id:1).first
=> #<User id: 1, email: "[email protected]", encrypted_password: ... 
...
# 输入新密码
irb(main):002:0> user.password='passwd.2016'
=> "passwd.2016"
# 保存新密码
irb(main):003:0> user.save!
=> true
# 退出
irb(main):004:0> exit
-sh-4.1$ exit
logout

升级Gitlab版本

在12.x.x之前, 跨大版本升级可以升级到大版本下的最后一个版本, 例如 8.10.2 -> 8.17.8, -> 9.5.10 -> 10.8.7 -> 11.11.8

在12.x.x之后, 不能直接升级到最后版本了, 每次要先升级到大版本的第一个小版本, 再继续升到这个大版本下的最后一个版本, 例如 11.11.8 -> 12.0.12 -> 12.10.14 -> 13.0.14

升级的命令

# 停止服务
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
sudo gitlab-ctl stop nginx

# 升级, 此时会自动处理migration和pgresql升级
sudo dpkg -i gitlab-ce_12.0.12-ce.0_amd64.deb 
# 根据上一步的提示, restart服务
sudo gitlab-ctl restart

虽然官方提示从8.10.2先要升到8.12.0才能继续升到8.17.7, 但是实际测试直接升到8.17.8也没问题.

在8.x.x升到最高版本的过程中, 会遇到"Permission error 0700 not 2770", "Failed asserting that mode permissions on "/mnt/AZLSERV/git-data/repositories" is 2770"的错误, 这时候运行一下

chmod -R 2770 /var/opt/gitlab/git-data/repositories

再运行 gitlab-ctl reconfigure就可以了

初始安装的Ubuntu18.04上的命令记录

# Add /dev/vdb1
fdisk -l
vi /etc/fstab # add /dev/vdb1 /data ext4 defaults 0 0

# change hostname
hostnamectl set-hostname n139
/etc/hosts

# change ssh port
vi /etc/ssh/sshd_config 

# Enable UFW
ufw status verbose
ufw enable
ufw status verbose
ufw app list

# install gitlab
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
vi /etc/apt/sources.list.d/gitlab-ce.list 
# add line
# deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu bionic main
apt update
apt install gitlab-ce

# Add 80 port
ufw allow http

# change external url
vi /etc/gitlab/gitlab.rb 
# This will take a while
gitlab-ctl reconfigure

# Install docker
apt install docker.io

# Add user, you must add with -m and -s /bin/bash milton, this is a bit weird in Aliyun Ubuntu
useradd milton -m
ll /home/
ll /home/milton/ -a
usermod -s /bin/bash milton
passwd milton
usermod -aG docker milton

systemctl status docker

# Add mirror for docker
vi /etc/docker/daemon.json
----- add content
{
  "registry-mirrors": [
    "https://x9o4p9lt.mirror.aliyuncs.com/"
  ]
}
-----

# Install gitlab runner https://docs.gitlab.com/runner/install/ and https://docs.gitlab.com/runner/install/linux-repository.html
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
apt install gitlab-runner

# Build runner images
docker build -t ubuntu_xenial_runner:1.0 ubuntu_xenial/
docker build -t ubuntu_bionic_runner:1.0 ubuntu_bionic/

# Register a runner
gitlab-ci-multi-runner register --non-interactive --url "http://172.17.54.19/" --registration-token "GaSKjBbMmQKgqm_QBxjb" --description "Ubuntu 16.04 build runner" --executor "docker" --docker-image "ubuntu_xenial_runner:1.0"

gitlab-ci-multi-runner register --non-interactive --url "http://172.17.54.19/" --registration-token "GaSKjBbMmQKgqm_QBxjb" --description "Ubuntu 18.04 build runner" --executor "docker" --docker-image "ubuntu_bionic_runner:1.0"

对应的Dockerfile和setup.sh

from ubuntu:xenial
ADD setup.sh /opt/
RUN /bin/bash /opt/setup.sh

.

#!/bin/sh

# change mirror
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted" > /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial universe" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates universe" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://security.ubuntu.com/ubuntu xenial-security main restricted" >> /etc/apt/sources.list
echo "deb http://security.ubuntu.com/ubuntu xenial-security universe" >> /etc/apt/sources.list
echo "deb http://security.ubuntu.com/ubuntu xenial-security multiverse" >> /etc/apt/sources.list

# requirements
apt-get update
apt-get -y install apt-transport-https git dh-make build-essential autoconf autotools-dev qt5-default libssl-dev qt5keychain-dev

如果需要Gitlab Runner优先使用本地docker上的image, 需要修改其配置, 位于/etc/gitlab-runner/config.toml , 增加pull_policy = "if-not-present" 注意有双引号

[[runners]]
  name = "Ubuntu 18.04 x64 runner"
  url = "http://172.17.54.19/"
  token = "UsJyRYap61AxbJ3pY_in"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.docker]
    tls_verify = false
    image = "ubuntu_bionic_runner:1.0"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    pull_policy = "if-not-present"