본문 바로가기
Programming/Git

[Git] webhooks 을 이용한 auto git pulling 구성 (git pull with webhooks)

by guru_k 2023. 2. 13.
728x90
반응형

git webhooks을 이용하여 remote storage에 있는 source를 업데이트 할 수 있는 환경을 만든다.

webhook receiver는 아래 문서를 참고하여 작성한다.

 

Webhooks | GitLab

Documentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.

docs.gitlab.com

 

리모트 환경에 Git clone

git pull 이 자동으로 필요한 remote 환경에서 git source clone

$ git clone https://gitlab.test.net/test.git

 

webhook receiver 작성

webhook_receiver.rb

require 'webrick'

server = WEBrick::HTTPServer.new(:Port => ARGV.first)
server.mount_proc '/' do |req, res|
  puts req.body
end

trap 'INT' do
  server.shutdown
end
server.start

webhook receiver 실행

$ ruby print_http_body.rb 8000

gitlab webhook 설정

Webhooks

webhook receiver 주소를 webhook URL 에 입력 및 Push events 체크 후 Add webhook 클릭

webhook receiver 테스트

Test event 를 통해 webhook receiver 에 정상적으로 event를 수신하는지 확인

[2023-02-13 02:56:32] INFO  WEBrick::HTTPServer#start: pid=25365 port=8000
{"object_kind":"push","event_name":"push","before":"77a142c10fdec2a70874f652e457a45b2e9640b4","after":"30af4dc249e6ec58fe9ece97e5081e48dcc19fbf","ref":"refs/heads/main","checkout_sha":"30af4dc249e6ec58fe9ece97e5081e48dcc19fbf","message":null,"user_id":111,"user_name":"user","user_username":"user","user_email":null,"user_avatar":"https://secure.gravatar.com/avatar/20111","project_id":116,"project":{"id":116,"name":"marp","description":null,"web_url":"https://gitlab.net/marp","avatar_url":null,"git_ssh_url":"git@gitlab.net:marp.git",}

 

webhook receiver 에 git pull 작업 추가

기존 webhook_receiver.rb 에 아래 내역 추가

require 'webrick'

server = WEBrick::HTTPServer.new(:Port => ARGV.first)
server.mount_proc '/' do |req, res|
  puts req.body
  Dir.chdir('/home/user/marp')		<---- git local path
  puts `git pull`           		<---- git pull
end

trap 'INT' do
  server.shutdown
end
server.start

* 만약 git pull이나 경로 이동이 정상적으로 되지 않는다면 해당 폴더의 권한을 추가.

 

 

webhook receiver 백그라운드로 실행

$ nohup ruby -d gitlab.rb 8000 &

 

이후에 다른 환경에서 Push 후 Remote 환경에서 정상적으로 webhook 수신 후 최신 데이터로 업데이트 되는지 확인.

 

끝.

728x90
반응형

댓글