Gogs + Jenkins 搭建自動化部屬平台

1.1k 詞

前言

雖然直接在本機透過Cron來進行排成操作也可以解決大多數的事情,但這樣應用範圍有限,為了提升自己,也為了可以讓我更懶惰,多學習肯定不是壞處,所以今天跟我一起來把它搞起來吧~ ◝( ゚∀ ゚ )◟

透過GOGS的版控管理,搭配Jenkins可以做到更智能與快速的搭建

之所以選擇Gogs而不是Gitlab或Github,是因為我需要的只是簡單的版控,不需要太多有的沒的東西,所以才選擇使用Gogs

さあ、始めましょう (ゝ∀・)b

  • 環境配置
    • Ubuntu版本: Ubuntu 22.04 LTS
    • Jenkins版本: 2.426.3 LTS
    • Gogs版本: 0.13.0

Jenkins安裝

1
2
3
4
5
6
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
# 新增 repository Key到系統
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
#把Jenkins 加到 repository
sudo apt-get update | sudo apt-get install fontconfig openjdk-17-jre | sudo apt-get install jenkins
#安裝 Jenkins和相依套件

Imgur

  • 設定Jenkins
    1
    http://192.168.50.133:8080/
    Imgur
    照上面路徑去找到密碼並輸入
    後面就按照他上面寫的進行就可以了。
    Imgur

安裝Gogs

  • 安裝SQL
    1
    apt install mariadb-server mariadb-client
  • 確認是否安裝完成
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    root@luca:~# service mysqld status
    ● mariadb.service - MariaDB 10.6.16 database server
    Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Active: active (running) since Thu 2024-02-08 17:50:24 UTC; 15s ago
    Docs: man:mariadbd(8)
    https://mariadb.com/kb/en/library/systemd/
    Process: 7202 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SU>
    Process: 7203 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/S>
    Process: 7205 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/b>
    Process: 7244 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/>
    Process: 7247 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
    Main PID: 7234 (mariadbd)
    Status: "Taking your SQL requests now..."
    Tasks: 12 (limit: 4516)
    Memory: 61.2M
    CPU: 326ms
    CGroup: /system.slice/mariadb.service
    └─7234 /usr/sbin/mariadbd

安全安裝 ※用於提高Mysql安全性

1
mysql_secure_installation

登入sql

1
mariadb -u root -p

Enable global variables

1
SET GLOBAL innodb_file_per_table = ON;

Create a Database

1
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Create a User and grant all the privileges of the gogs database

1
GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY "YourPassword";

寫入

1
2
flush privileges;
exit
  • 下載Gogs
    1
    2
    cd ~ ; mkdir source ; cd ~/source
    curl -s https://api.github.com/repos/gogs/gogs/releases/latest | grep browser_download_url | grep '\linux_amd64.tar.gz' | cut -d '"' -f 4 | wget -i -
    解壓縮
    1
    tar xvf gogs_*_linux_amd64.tar.gz
    創建新的使用者
    1
    adduser git
    創建dedicated logs
    1
    mkdir /var/log/gogs
    給予資料夾權限
    1
    chown -R git:git /var/log/gogs
    設定啟動
    1
    cp gogs/scripts/systemd/gogs.service /etc/systemd/system
    調整連接埠 ※可改可不改,本次沒有改
    1
    2
    vim /etc/systemd/system/gogs.service
    ExecStart=/home/git/gogs/gogs web -port 3001
    把目錄搬過去 並調整權限
    1
    mv gogs /home/git/ ; chown -R git:git /home/git
    重新reload服務,並啟動gogs與設定自動啟動
    1
    systemctl deamon-reload; systemctl start gogs; systemctl enable gogs
    確認是否啟動
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    root@luca:/home/git# systemctl status gogs
    ● gogs.service - Gogs
    Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: enabled)
    Active: active (running) since Thu 2024-02-08 18:26:35 UTC; 20s ago
    Main PID: 7784 (gogs)
    Tasks: 6 (limit: 4516)
    Memory: 35.3M
    CPU: 153ms
    CGroup: /system.slice/gogs.service
    └─7784 /home/git/gogs/gogs web

    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Log mode: Console (Trace)
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [ INFO] Gogs 0.13.0
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Work directory: /home/git/gogs
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Custom path: /home/git/gogs/custom
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Custom config: /home/git/gogs/custom/conf/app.ini
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Log path: /home/git/gogs/log
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Build time: 2023-02-25 02:30:34 UTC
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [TRACE] Build commit: 8c21874c00b6100d46b662f65baeb40647442f42
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [ INFO] Run mode: Development
    Feb 08 18:26:35 luca gogs[7784]: 2024/02/08 18:26:35 [ INFO] Available on http://localhost:3000/
    設定 Gogs

    http://server-ip-address:3000 就能看到安裝程序啦~

    因為是使用Mariadb,要修改成MySQL定寫上相關資訊
    Imgur

應用程式網址修改成IP,不要使用 localhost 不然會出錯
Imgur
這樣就安裝完成啦~
Imgur

後記


我知道Jenkins搭配GOGS可以有許多應用跟搭配,例如Docker、Ansible等

我也一直想獲取這方面得相關經驗跟機會,但眼下公司用不到,貌似未來也不會使用到,只能自己多方摸索推動了(´ー`)

今天就先到這邊啦,我們下次見,掰掰