- 서버/GCP(google cloud platform)

GCP 리눅스 centos7 환경에서 github 자동 푸시 만들기

더모어더베러 2023. 8. 21. 23:06

git 설치 하기

root 관리자로 변경후(su - 입력) git을 설치해 줍니다.

설치후 사용자이름, 메일을 등록해 줍니다.

[root@instance-1 ~]# yum install git
[root@instance-1 ~]# git --version
[root@instance-1 ~]# git config --global user.name "사용자이름"
[root@instance-1 ~]# git config --global user.email "사용자메일"

 

ssh key 생성하여 github에 등록하기

[root@instance-1 ~]# ssh-keygen -t rsa -C "사용자메일"
[root@instance-1 ~]# cat /.ssh/id_rsa.pub

cat을 입력하여 id_rsa.pub 파일을 출력합니다.

 

ssh-rsa .... 로 시작 하여 이메일주소로 끝나는 key값을 깃허브 Settings > SSH and GPG keys > SSH keys > New SSH key에 추가후 저장해줍니다.

 

 

 

github repository 생성하기

github에서 repository를 생성해 줍니다.

 

SSH git repository 주소를 복사합니다.

 

새로운 디렉토리 autoGit을 만들고 git clone을 해줍니다.

[root@instance-1 ~]# mkdir autoGit
[root@instance-1 autoGit]# git clone git@github.com:jeondoohyun/autoPush.git

autoPush이 잘 clone되었는지 확인해봅니다.

 

 

스케줄링 sh파일 생성

디렉토리를 하나 생성하여 sh파일을 만들어줍니다.

 

[root@instance-1 autoGit]# mkdir git-sch
[root@instance-1 autoGit]# cd git-sch
[root@instance-1 git-sch]# vi git-sch.sh

 

vim 편집기로 git-sch.sh를 추가해줍니다.

# git-sch.sh

Y=$(date +%Y)
M=$(date +%m)
D=$(date +%d)

Ym=$Y-$M
Ymd=$Y-$M-$D
GitRep="autoPush"

HomeDir="/root/autoGit/git-sch"
GitDir="$HomeDir/$GitRep"
FileDir="$HomeDir/$GitRep/$Ym"
FileNm="$Ymd".md

mkdir -p $FileDir

echo "### $Ymd 일기" >> $FileDir/$FileNm

cd $GitDir
git add .
git commit -m "commit $FileNm"
# git push origin master  원래 pull하고 push해야 하는데 -f로 로컬 데이터를 강제 푸시로 일단 진행
git push -f origin

 

 

권한을 부여 합니다.

[root@instance-1 git-sch]# chmod +x git-sch.sh

 

sh파일을 실행합니다.

[root@instance-1 git-sch]# ./git-sch.sh

 

github repository에 접속해 push가 잘되었는지 확인합니다.

 

 

날짜확인 하기

크론탭으로 스케줄링을 하기 전에 시간을 확인합니다.

[root@instance-1 ~]# date

 

현재 한국시간이 적용된것이 아닙니다. 한국시간 기준으로 변경해줍니다.

[root@instance-1 ~]# ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime

 

 

크론탭 스케줄링 설정하기

크론탭을 설정 합니다.

[root@instance-1 ~]# crontab -e

 

vim 편집기와 비슷한 창이 나옵니다. 아래 값을 입력하고 저장후 종료해줍니다.

매일 18시 10분에 git-sch.sh 파일을 실행시켜 github에 푸시가 진행됩니다.

등록된 크론탭을 조회 할때는 crontab -l 을 실행시키면 확인이 가능합니다.

10 18 * * * /root/autoGit/git-sch/git-sch.sh