๐Ÿ“š Today _I_Learned/Git

[GitHub] Organization GitLab์œผ๋กœ ๋ฏธ๋Ÿฌ๋งํ•˜๊ธฐ

nooopy 2023. 3. 30. 19:25

GitLab์—์„œ ์ง„ํ–‰

1. GitLab - new Group - create Group


 

2. group ์ด๋ฆ„, ์ดˆ๋Œ€ ์ด๋ฉ”์ผ ๋„ฃ๊ณ  ์ƒ์„ฑ

 


 

3. group ๋‚ด์—์„œ new project ์ƒ์„ฑ

  • readme๋Š” ์ฒดํฌ ํ•ด์ œ ํ•„์ˆ˜!!!

 

4. gitlab settings > CI/CD ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •


5. gitlab token ๋ฐœ๊ธ‰ (๋ณต์‚ฌ / ๋ณด๊ด€)

  • ํ”„๋กœํ•„ > preference
  • token ์ƒ์„ฑ

 

GitHub์—์„œ ์ง„ํ–‰

1. organization repository > settings > security

  • TARGET_TOKEN = gitHub token ์ž…๋ ฅ
  • TARGET_URL = gitlab url
  • TARGET_USERNAME = gitlab ์•„์ด๋””

2. github์— PUSH ํ•  ํด๋”์— ํŒŒ์ผ 2๊ฐœ ์ถ”๊ฐ€

.github/workflows/sync.gitlab.yml

name: Sync this repository to another repository

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

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  push-to-gitlab:
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Fetch all history for all tags and branches

      - name: Set remote repository
        env: # Retrieve env variable from GiGHub secrets
          gitlab_url: ${{ secrets.TARGET_URL }}
          gitlab_username: ${{ secrets.TARGET_USERNAME }}
          gitlab_token: ${{ secrets.TARGET_TOKEN }}
        run: | # ํ† ํฐ์„ ์กฐํ•ฉํ•ด์„œ ์ €๋ ‡๊ฒŒ URL์„ ๋งŒ๋“ค๋ฉด ๋น„๋ฒˆ์—†์ด push๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
          git remote add gitlab https://${gitlab_username}:${gitlab_token}@${gitlab_url#https://};
      - name: Force push everthing
        run: | # ๋ชจ๋“  ๋ธŒ๋žœ์น˜์™€ ํƒœ๊ทธ์— ๋Œ€ํ•˜์—ฌ pushํ•ฉ๋‹ˆ๋‹ค. gitlab์—์„œ ๋ณดํ˜ธ์„ค์ •์ด ๊ฑธ๋ ค์žˆ์œผ๋ฉด Failํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
          git push -f --all gitlab;
          git push -f --tags gitlab;

.gitlab-ci.yml

stages:
  - dockerbuild-push

package:
  only:
    - main
  image: docker:latest
  stage: dockerbuild-push
  services:
    - docker:dind
  before_script:
    - docker login registry.gitlab.com -u $GITLAB_USER -p $GITLAB_PASSWORD
  script:
    - docker build -t registry.gitlab.com/$GROUP_NAME/$PROJECT_NAME .
    - docker push registry.gitlab.com/$GROUP_NAME/$PROJECT_NAME
  after_script:
    - docker logout
  • ๋‘ ๊ฐœ ํŒŒ์ผ ์ถ”๊ฐ€ํ•˜๊ณ  commit-push ํ•˜๋ฉด github์™€ gitlab ๋ฏธ๋Ÿฌ๋ง ์™„์„ฑ