docker 部署gitlab 構建CI/CD流水線

拉取鏡像 啓動容器 暴露出來 9980和9922端口

docker pull gitlab/gitlab-ce:latest

mkdir -P /data/gitlab-test/etc /data/local/gitlab-test/log /data/local/gitlab-test/opt
docker run \
 -itd  \
 -p 9980:80 \
 -p 9922:22 \
 -v /data/gitlab-test/etc:/etc/gitlab  \
 -v /data/local/gitlab-test/log:/var/log/gitlab \
 -v /data/local/gitlab-test/opt:/var/opt/gitlab \
 --restart always \
 --privileged=true \
 --name gitlab-test \
 gitlab/gitlab-ce

進入容器修改基本配置 字面意思看描述修改

docker exec -it gitlab-test /bin/bash

production: &base

  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: xx.xx.xx.xx  # 你的主機IP
    port: 9980
    https: false
    ssh_host: xx.xx.xx.xx  # 你的主機IP
    # If your ssh user differs from the system user, you need to specify it here
    # Set it to an empty string to omit the username from any ssh url altogether
    ssh_user:
  gitlab_shell:
    # If you use non-standard ssh port you need to specify it
    ssh_port: 9922

讓配置生效

gitlab-ctl reconfigure
gitlab-ctl restart

# 同時修改密碼
gitlab-rails console
user = User.where(id: 1).first
或者
user = User.find_by(email: '[email protected]')
user.password = '你的密碼'
user.password_confirmation = '你的密碼'
user.save

登錄之後默認會有個Monitoring 項目 可以開始配置gitlab Runners,進行gitlab cicd 的自動構建

首先在設置裏面找到註冊runner的url 和 token:如下圖所示

我選擇的是在docker裏面啓動runnser服務 官方地址:https://docs.gitlab.com/runner/install/docker.html

可以有2種方式一種是使用本地的存儲卷,一種是使用docker存儲卷 我使用本地存儲
https://docs.gitlab.com/runner/install/docker.html

docker run -d --name gitlab-runner --restart always \
     -v /srv/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:latest

### 在 macOS, 使用 /Users/Shared 替換 /srv
### 註冊runner https://docs.gitlab.com/runner/register/index.html#docker
docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

### 註冊的時候會選擇執行器,可以根據需要選擇 我選擇了shell 和 docker,一次註冊只能選擇一個執行器
### 根據選項一步一步註冊就好了 tag 和 描述要寫好,在後面的.git-ci.yam 配置裏面會用到

配置 ci/cd 描述文件

default:
  tags: 
    - docker
  

before_script:
  - export PACKAGE_NAME=${CI_PROJECT_NAME}_${CI_COMMIT_SHA:0:8}.zip

stages:          # List of stages for jobs, and their order of execution
  - build
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
    - go build main.go
  artifacts:
    expire_in: 1 week
    when: on_success
    paths:
      - main

# archive_package:
#   stage: package
#   script:
#   - zip -r ${PACKAGE_NAME} main
#   artifacts:
#     expire_in: 1 week
#     when: on_success
#     untracked: true
#     paths:
#       - ${PACKAGE_NAME}
#   tags:
#     - docker
#   only:
#     - tags

在cd/cd Pipeline 菜單裏面執行作業:

通過 artifacts 關鍵字可以生成構建物,在倉庫的tag 裏面可以進行下載

具體的 .gitlab-ci.yml 文件語法可以參考官方文檔

https://docs.gitlab.com/ee/ci/yaml/

構建結果

注意事項

  1. gitlab 和 runner 的版本要一致,否則可能出現構建記錄無法出現的錯誤
  2. 可以通過 docker logs id 查看具體的日誌
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章