Gitlab自动部署

979 阅读3分钟

安装Gitlab

部署Gitlab服务器最低配置也得是 2核4G ,自己在家测试的朋友们注意了。

安装

安装依赖包

sudo yum install -y curl policycoreutils-python openssh-server

设置SSH开机自动启动

sudo systemctl enable sshd
sudo systemctl start sshd

安装Postfix来发送通知邮件、设置Postfix开机自启动

sudo yum install postfix
sudo systemctl enable postfix

启动Postfix服务

# 打开 main.cf文件
vim /etc/postfix/main.cf

# 将 inet_interfaces=localhost 改成 inet_interfaces=all 保存

# 启动 Postfix 服务
sudo systemctl start postfix

添加 Gitlab 软件包仓库

 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

安装 Gitlab

Gitlab 提供了两种安装版本 gitlab-ce OR gitlab-ee ,ce和ee分别指的是社区版和企业版,一般社区版就已经满足我们的需求了

sudo EXTERNAL_URL="GitLab服务器的公网IP地址" yum install -y gitlab-ce

如果后面想更改 EXTERNAL_URL 或者其他 Gitlab 配置的话可以在 /etc/gitlab/gitlab.rb 文件中更改,更改后需要运行吃昂新家在配置的命令才能生效

sudo gitlab-ctl reconfigure

安装完成

输入公网IP地址就应该可以查看到 Gitlab 的界面了,账号为 root ,填写一下登录密码就OK了

创建一个项目

页面上有创建按钮,大家都懂的呀,按步骤创建就行了。

安装 Gitlab-Runner

安装

下载系统对应的 Gitlab-runner

 # Linux x86-64
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

 # Linux x86
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

 # Linux arm
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm

添加执行权限

 sudo chmod +x /usr/local/bin/gitlab-runner

创建用户

 sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

安装启动服务

 sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
 sudo gitlab-runner start

注册

运行注册命令

 sudo gitlab-runner register

填写 Gitlab 服务器 URL

 Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
 # 没有域名所以填的是IP
 http://xx.xx.xxx.xx

填写 Token

位于项目左侧 设置 - CI/CD - Runner ,因为我创建的是按量付费的实例,所以也不怕大家看😐

填写描述

 Please enter the gitlab-ci description for this runner
 test-gitlab-runner-description

填写 Tags

 Please enter the gitlab-ci tags for this runner (comma separated)
 my-tag

选择 Runner 执行者

按自己的需求填写,这里我用的 shell

 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
 shell

安装完成

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

测试上传

  • 安装Git工具linux:安装Git,使用自带的源安装。

    yum install git
    
  • 生成密钥文件

    使用ssh-keygen生成密钥文件.ssh/id_rsa.pub,再将公钥文件id-rsa.pub中的内容粘帖到GitLab服务器的SSH-key的配置中。

    ssh-keygen
    
  • 简单配置 Git 仓库

    设置全局人员姓名跟邮箱用来显示是谁上传的代码

    git config --global user.name "Armand" 
    git config --global user.email "Armand@xxx.com" 
    
  • END

    克隆代码,上传代码巴拉巴拉的就不说了,结束

.gitlab-ci.yml文件

这是比较重要的一个环节,部署的精华都在文件里

在安装好的 Gitlab-Runner 服务器上下拉代码,然后创建 .gitlab-ci.yml 文件,将下方的内容写入文件里保存上传,然后我们进入项目右侧的 CI/CD 中就可以看到结果了。

# 定义 stages(阶段,会依次执行)
stages:
  - install_deps
  - build_prod
  - deploy_prod

cache:
  key: ${CI_BUILD_REF_NAME}
  paths:
    - node_modules/
    - dist

# 安装构建依赖
install_deps_job:
  stage: install_deps
  # 在哪个分支才会执行脚本
  only:
    # - dev
    # - release
    - master
  script:
    - echo '模拟安装构建依赖阶段'
  tags:
    - my-tag

# 构建预prod环境src目录下应用
build_prod_job:
  stage: build_prod
  only:
    - master
  script:
    - echo '构建预prod环境src目录下应用阶段'
  tags:
    - my-tag

# 部署生产环境
deploy_prod_job:
  stage: deploy_prod
  only:
    - master
  script:
    - echo '部署生产环境阶段'
  tags:
    - my-tag

Other

Gitlab常用命令

# 启动
sudo gitlab-ctl start
# 停止
sudo gitlab-ctl stop
# 重启
sudo gitlab-ctl restart
# 查看状态
sudo gitlab-ctl status
# 使更改配置生效
sudo gitlab-ctl reconfigure