개발일지/GIT

[Git] Github에서 Gitlab으로 미러링 (Github Action) 잔디 옮기기

양쏘쏘 2024. 5. 28. 14:17
728x90
반응형

Github Action이란?

Github Action을 간단하게 설명하자면 repo에서 .github/workflows 디렉토리에 YAML 파일로 workflow들을 사용할 수 있다.

깃허브에서 pull request, issue open, push commit 같은 이벤트가 발생했을 때 특정 역할을 수행하게 세팅할 수 있는 것이다.

 

프로젝트 구조를 확인하고 싶다면 아래의 레포를 확인하세요.

 

git-tutorial/.github/workflows/mirror.yml at main · YangSSo51/git-tutorial

깃 사용법에 대한 사용 예시. Contribute to YangSSo51/git-tutorial development by creating an account on GitHub.

github.com

 

1. workflow

github 레포의 .github/workflow/mirror.yml 파일을 추가해줍니다.

mirror.yml 파일을 간단하게 설명하자면 develop 브랜치에서 push, pull_request 이벤트가 발생하였을 때

screts에 저장된 gitlab 정보를 통해 gitlab의 develop 브랜치로 작업 내용과 태그에 대해 추가해줍니다.

# .github/workflow/mirror.yml
# Github의 작업을 다른 레포로 옮겨줍니다
name: Sync this repository to another repository

on:
  push:
    branches: ["develop"]
  pull_request:
    branches: ["develop"]

jobs:
  push-to-gitlab:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 

      - name: Set remote repository
        env: 
          gitlab_url: ${{ secrets.TARGET_URL }}
          gitlab_username: ${{ secrets.TARGET_USERNAME }}
          gitlab_token: ${{ secrets.TARGET_TOKEN }}
        run: | # gitlab에 토큰으로 연결합니다
          git remote add gitlab https://${gitlab_username}:${gitlab_token}@${gitlab_url#https://};
      
      - name: Force push branch
        run:
          | # develop 브랜치를 대하여 push합니다. gitlab에서 보호설정이 걸려있으면 실패할 수 있습니다.
          git push -f gitlab HEAD:develop;

 

2. Github에 Secret 추가

Github Action Secrets
3가지 secrets이 추가된 모습

mirror.yml 파일을 보면 secrets가 총 3개 존재합니다.

gitlab의 url, user_name, token 이 세가지를 등록해주면 됩니다.
url은 clone할 때의 https://~경로, user_name은 gitlab의 계정 정보, token은 아래의 방법으로 발급하면 됩니다.

 

3. Gitlab의 Access Token

gitlab 의 Access Token 추가

gitlab의 Access Token은 write_repository만 가능하게 토큰을 발급해주면 됩니다. 

 

4. 사용 예시

github과 gitlab의 커밋 비교

Github의 develop 브랜치의 작업 내역을 Gitlab의 develop 브랜치로 계속 미러링을 해주고있기 때문에 커밋이 자동으로 동기화되는 모습을 확인할 수 있습니다. 

 

잔디 확인

커밋이 옮겨졌기 때문에 당연히 잔디도 잘 심어집니다.

 

Github의 workflow에 대해 더 자세하게 이해하고싶다면 아래 글을 참고하세요.

 

GitHub Actions 이해 - GitHub Docs

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged

docs.github.com

 

728x90