Gitlab

จาก Morange Wiki

Gitlab[1] เป็นเซิร์ฟเวอร์สำหรับจัดการ git โดยมีคุณสมบัติการใช้งานคล้ายกับ github โดยเป็น open source ที่เป็น community edition และแบบ support ที่เป็น Enterprise edition โดยที่ Community สามาระสร้างได้ 10,000 repository ความสามารถของ GitLab

เกร็ดเล็กก่อนเริ่ม เรียน git

คลิกเลย


การติดตั้ง

  • ไปที่ https://about.gitlab.com/gitlab-ce และเลือกดาวน์โหลด https://about.gitlab.com/downloads/archives/ โดยที่แพ็กเกจประกอบด้วย postgres และ nginx แบบ embed ในตัว
  • เลือก Ubuntu 14.04 และดาวน์โหลดสำหรับ Ubuntu 14.04 LTS 64bit และติดตั้ง
wget https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab_7.3.2-omnibus-1_amd64.deb
[sudo] password for seksan: 
Selecting previously unselected package gitlab.
(Reading database ... 122590 files and directories currently installed.)
Preparing to unpack gitlab_7.3.2-omnibus-1_amd64.deb ...
Unpacking gitlab (7.3.2-omnibus-1) ...
Setting up gitlab (7.3.2-omnibus-1) ...
Thank you for installing GitLab!
You can configure GitLab for your system by running the following command:

sudo gitlab-ctl reconfigure
  • ตั้งค่าการใช้งาน ระบบจะทำการตั้งค่าฐานข้อมูล (Postgres) และ นำเข้าฐานข้อมูลพื้นฐาน
sudo gitlab-ctl reconfigure
  • แก้ไข hostname
sudo -e /etc/gitlab/gitlab.rb
  • (option) แก้ไข nginx /var/opt/gitlab/nginx/conf/gitlab-http.conf เพื่อทำ proxy
  • username และ password เริ่มต้น
Username: root 
Password: 5iveL!fe

การอัปเกรด

เนื่องจากเป็นการติดตั้งจาก source เราจะอัปเกรดโดยขั้นตอนตาม [2]

  • Backup
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production
  • ปิด server
sudo service gitlab stop
  • ดึง code ล่าสุดจาก stable branch
cd /home/git/gitlab
sudo -u git -H git fetch --all
sudo -u git -H git checkout -- Gemfile.lock db/schema.rb
sudo -u git -H git checkout LATEST_TAG

LATEST_TAG เช่น v7.14.1

  • ปรุงปรุง gitlab-shell ตามรุ่นที่ต้องใช้
cd /home/git/gitlab-shell
sudo -u git -H git fetch
sudo -u git -H git checkout v`cat /home/git/gitlab/GITLAB_SHELL_VERSION`
  • ติดตั้งไลบรารี่และอัปเกรดฐานข้อมูล
cd /home/git/gitlab

#PostgreSQL
sudo -u git -H bundle install --without development test mysql --with postgres --deployment

# MySQL
sudo -u git -H bundle install --without development test postgres  --with mysql  --deployment

sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production
sudo -u git -H bundle exec rake assets:clean RAILS_ENV=production
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
sudo -u git -H bundle exec rake cache:clear RAILS_ENV=production
  • เริ่ม application
sudo service gitlab start
sudo service nginx restart
  • ตรวจสอบสถานะการติดตั้ง
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production

sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

ถ้าทุกรายการเป็นสีเขียวหรือผ่านทั้งหมด ถือว่าการอัปเกรดเสร็จสมบูรณ์

การอัปเกรดจาก 7.x เป็นเวอร์ชัน 8.x

จาก [3] จะพบว่า ต้องติดตั้ง gitlab-git-http-server ซึ่งเป็นส่วนที่แยกด้าน authentication ออกจาก main process มีผลทำให้ ต้องตั้งค่าเพิ่ม

# ติดตั้ง go
curl -O --progress https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz
echo '46eecd290d8803887dec718c691cc243f2175fe0  go1.5.1.linux-amd64.tar.gz' | shasum -c - && \
  sudo tar -C /usr/local -xzf go1.5.1.linux-amd64.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1.5.1.linux-amd64.tar.gz

ติดตั้ง gitlab-git-http-server

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-git-http-server.git
cd gitlab-git-http-server
sudo -u git -H make

ในเวอร์ชันหลังๆ gitlab-http-server ได้เปลี่ยนชื่อเป็น gitlab-workhorse ติดตั้งโดย

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
sudo -u git -H make

จากนั้น ตั้งค่า /etc/default/gitlab ใหม่

 Copy this lib/support/init.d/gitlab.default.example file to
# /etc/default/gitlab in order for it to apply to your system.

# RAILS_ENV defines the type of installation that is running.
# Normal values are "production", "test" and "development".
RAILS_ENV="production"

# app_user defines the user that GitLab is run as.
# The default is "git".
app_user="git"

# app_root defines the folder in which gitlab and it's components are installed.
# The default is "/home/$app_user/gitlab"
app_root="/home/$app_user/gitlab"

# pid_path defines a folder in which the gitlab and it's components place their pids.
# This variable is also used below to define the relevant pids for the gitlab components.
# The default is "$app_root/tmp/pids"
pid_path="$app_root/tmp/pids"

# socket_path defines the folder in which gitlab places the sockets
#The default is "$app_root/tmp/sockets"
socket_path="$app_root/tmp/sockets"

# web_server_pid_path defines the path in which to create the pid file fo the web_server
# The default is "$pid_path/unicorn.pid"
web_server_pid_path="$pid_path/unicorn.pid"

# sidekiq_pid_path defines the path in which to create the pid file for sidekiq
# The default is "$pid_path/sidekiq.pid"
sidekiq_pid_path="$pid_path/sidekiq.pid"

gitlab_git_http_server_pid_path="$pid_path/gitlab-git-http-server.pid"
# The -listenXxx settings determine where gitlab-git-http-server
# listens for connections from NGINX. To listen on localhost:8181, write
# '-listenNetwork tcp -listenAddr localhost:8181'.
# The -authBackend setting tells gitlab-git-http-server where it can reach
# Unicorn.
gitlab_git_http_server_options="-listenUmask 0 -listenNetwork tcp -listenAddr 127.0.0.1:8181 -authBackend http://127.0.0.1:8888"
gitlab_git_http_server_repo_root="/home/git/repositories"
gitlab_git_http_server_log="$app_root/log/gitlab-git-http-server.log"

# mail_room_enabled specifies whether mail_room, which is used to process incoming email, is enabled.
# This is required for the Reply by email feature.
# The default is "false"
mail_room_enabled=false

# mail_room_pid_path defines the path in which to create the pid file for mail_room
# The default is "$pid_path/mail_room.pid"
mail_room_pid_path="$pid_path/mail_room.pid"

# shell_path defines the path of shell for "$app_user" in case you are using
# shell other than "bash"
# The default is "/bin/bash"
shell_path="/bin/bash"

โดยส่วนที่สำคัญคือ gitlab_git_http_server_options="-listenUmask 0 -listenNetwork tcp -listenAddr 127.0.0.1:8181 -authBackend http://127.0.0.1:8888" เปลี่ยนเป็น listen tcp และตั้งค่า http://127.0.0.1:8888 ให้ชี้ไปที่ port unicorn ของ gitlab

อีกส่วนคือ /home/git/gitlab-shell/config.yml ต้องแก้ไข ย้าย key ssh ไปที่ /var/opt/gitlab/.ssh/authorized_keys โดยอาจต้องเพิ่มลด key ใหม่ที่หน้าเว็บ

user: git
gitlab_url: https://git.morange.co.th/
http_settings:
  self_signed_cert: false
repos_path: "/home/git/repositories/"
#auth_file: "/home/git/.ssh/authorized_keys"
auth_file: "/var/opt/gitlab/.ssh/authorized_keys"
redis:
  bin: "/usr/bin/redis-cli"
  namespace: resque:gitlab
  socket: "/var/run/redis/redis.sock"
log_level: INFO
audit_usernames: false

การอัปเกรดจาก 8.x เป็นเวอร์ชัน 9.x

จาก [4] เมื่อต้องอัปเกรด สิ่งที่ต้องแก้ไขคือ

  1. Update Ruby เป็น Ruby 2.3.x
mkdir /tmp/ruby && cd /tmp/ruby
curl --remote-name --progress https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.gz
echo '1014ee699071aa2ddd501907d18cbe15399c997d ruby-2.3.3.tar.gz' | shasum -c - && tar xzf ruby-2.3.3.tar.gz
cd ruby-2.3.3
./configure --disable-install-rdoc
make
sudo make install
sudo gem install bundler --no-ri --no-rdoc
  1. Update Node เป็น > 4.3.x และลง yarn
curl --location https://yarnpkg.com/install.sh | bash -

 #  แก้ config/gitlab.yml โดยอ้างอิงจากตัวใหม่

cd /home/git/gitlab

# MySQL installations (note: the line below states '--without postgres')
sudo -u git -H bundle install --without postgres development test --deployment

# PostgreSQL installations (note: the line below states '--without mysql')
sudo -u git -H bundle install --without mysql development test --deployment

# Optional: clean up old gems
sudo -u git -H bundle clean

# Run database migrations
sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production

# Update node dependencies and recompile assets
sudo -u git -H bundle exec rake yarn:install gitlab:assets:clean gitlab:assets:compile RAILS_ENV=production NODE_ENV=production

# Clean up cache
sudo -u git -H bundle exec rake cache:clear RAILS_ENV=production

วิธีใช้งาน Gitlab เบื้องต้น

การสร้าง Project

1. ทำการสมัครสมาชิก เพื่อเข้าใช้งาน
2. เมื่อสมัครสมาชิกเสร็จแล้ว ก็ทำการล๊อคอินเข้าใช้งาน แล้วจะเจอหน้าแรกของการใช้งาน
3. สร้างโปรเจคสำหรับงานที่ต้องการทำ พยายามตั้งชื่อให้สอดคล้องกับงาน เพื่อง่ายต่อการค้นหา
4. การ git global setup

$ git config --global user.name "Name"
$ git config --global user.email "Email"

5. สร้าง Repository ใหม่

$ git clone https://witawatd@git.morange.co.th/witawatd/myproject_coe.git
$ cd <Name Project>

หรือถ้าเรามี Project อยู่แล้วให้ทำตามนี้เลย

$ cd <ตำแหน่ง folder>
$ git init
$ git remote add origin <http ลิงค์ของโปรเจค>
$ git add .
$ git commit -m "ใส่ข้อความแจ้งเตื่อน"
$ git push -u origin <branch name>

การ clone Repository มาใช้งาน

สมมุติว่าเราเปลียนคอมเครื่องอื่นหรื้อเราทำงานร่วมกับคนอื่น เราสามารถทำการ clone Repository มาใช้งานได้
มันจะคล้ายการ add Repository นะครับ

$ cd <ตำแหน่ง folder ที่ต้องการจะเก็บไว้>
$ git clone https://witawatd@git.morange.co.th/witawatd/myproject_coe.git

การ Push เข้าไปยัง gitlab

$ git add <name file> 
$ git commit -m "ข้อความ" 
$ git push origin <branch name>

การสร้าง branch แล้ว push ไปยัง gitlab

$ git branch <branch Name>
$ git checkout <branch Name>
$ git push <ชื่อ remote> <ชื่อ branch>
แล้วใส่รหัสผ่าน

การ pull [fetch + merge] เข้าไปใน gitlab

ก่อนทำการ pull ต้องทำการ commit เข้าไปยัง branch ก่อนแล้ว Push เข้าไปยัง remote จากนั้นทำการ checkout ออกไปยัง master ตามด้วยคำสั่งนี้เลย

$ git pull <ชื่อ remote> <ชื่อ branch>
แล้วใส่รหัสผ่าน

คำสั่งนี้เป็นการเช็คว่า ใน Repository มีโค้ดตรงกันกับ remote หรือไม่

อ้างอิง

5. https://medium.com/@pakin/git-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3-git-is-your-friend-c609c5f8efea

ดึงข้อมูลจาก "https://wiki.morange.co.th/index.php?title=Gitlab&oldid=1092"