增加新的project template在自架GitLab

關於GitLab OpenSource的開發專案當中有提到如何新增template project到GitLab中,但是對於已經架好GitLab但是想要開發自己公司、部門的特定Project template,可能就不是這麼方便,以下實測自行加入template project到自架GitLab網站中,結果是滿意的,但是UI顯示上沒有搞定,有高手請再多多指教。

需要修改的檔案

  • /opt/gitlab/embedded/service/gitlab-rails/gitlab/project_template.rb
    你會看到TEMPLATES_TABLE當中有其他已經在網頁上的template project,依照他的格式ˋ將自己要的template project名稱、git位置都寫上去,這個更新完後,記得gitlab-ctl configure,重啟後就可以看到你的template project上面多了可以選的按鈕,但僅可以preview,真正去按新建project,會產生網頁錯誤。

增加的檔案

再來我們轉到 /opt/gitlab/embedded/service/gitlab-rails/vendor/project_template 資料夾中,我們會發現這邊充斥著各個不同template project的壓縮檔,這才是能夠順利import專案的核心地點。

將以下scripts 存成 .sh file 並執行他(linux下)

在此之前需要先做以下幾個動作才可以順利執行:
# 設定shortcut
sudo ln -s /bin/tar /bin/gtar

將$2 置換成你要的檔案名稱: 也就是你剛剛project_template.rb當中的第一個名稱就要取得一模一樣,不然他會找不到

$1是你要複製的專案名稱

$4是你要複製的專案位置

向如果筆者個專案是在 https://chiacplus.com/test/project
那你的專案名稱就是 project
專案位置就是 https://chiacplus.com/test

$3則是你這個專案被clone後,會出現在第一筆commit的訊息

#!/bin/bash

function bundleRepo {
    gtar --list --file="$2.tar.gz"
    rm -rf tar-base project
    mkdir -p "./tar-base"
    tar xf "$2.tar.gz" -C "./tar-base"
    git clone "$1" project
    cd project
    rm -rf .git
    git init
    git add -A .
    git commit --author "GitLab <root@localhost>" -m "$3"
    git bundle create project.bundle --all
    mv -f project.bundle ../tar-base/
    cd ../tar-base
    rm -rf ./uploads ./lfs-objects
    cat project.json | jq '.project_members = [] |.issues = [] | .releases = [] | .merge_requests = [] | .ci_pipelines =
 [] | .pipeline_schedules = [] | .services = [] | .pipelines = [] | .protected_branches = [] | .labels = [] | del(.ci_cd
_settings)' -c > tmp.json
    mv -f tmp.json project.json
    ls -alth
    tar cvfz "$2.tar.gz" --exclude="$2.tar.gz" ./
    cd ..
    rm -rf "$2.tar.gz"
    mv tar-base/*.gz .
    gtar --list --file="$2.tar.gz"
}
bundleRepo $4/$1 $1 "Initial template creation"

執行完畢後複製到vendor/project_templates資料夾

執行:
存成 .sh 檔後 (假設我們是存成test.sh)

記得要以root身分將.sh檔案的權限做修改
sudo chmod 775 test.sh
這樣才可以順利執行
執行 sudo ./test.sh

當出現Username for “你的網站” 時,記得先把VERSION檔案複製到剛剛自動產生的 tar_base 資料夾中,VERSION檔案請去剛剛的project_templates資料夾中,找其中一個現有的壓縮檔難的VERSION進來。

之後繼續打入你進入該網站的帳號還有密碼
就會發現產生了你需要的 你的專案.tar.gz 檔案

再將這些檔案放入到剛剛的 project_templates資料夾中後就大功告成囉~~

參考網站:

https://gitlab.com/gitlab-org/gitlab-foss/-/issues/46043