Git | GitLab

[GitLab] gitlab-runner 내에서 push 하기 - 파이프라인 구성

비번변경 2025. 3. 5. 19:01

개요

2025.02.23-[GitLab] gitlab-runner 내에서 push 하기 - SSH 인증 구성에서 gitlab-runner 내에서 gitlab 레포지터리에 push하기 위해 SSH 인증을 구성했다.

이번 글에서는 이어서 SSH 인증을 사용한 git push 파이프라인을 구성한다.

 

 

1. SSH 인증 정의

https://gitlab.com/gitlab-examples/ssh-private-key/ 참고하여 .gitlab-ci.yml 파일을 정의한다.

test-job:
  stage: test
  before_script:
    ##
    ## Docker 사용 시 ssh-agent 설치 필요
    ## 아래 명령은 debian 기반으로 작성됨. RPM 기반 이미지 사용 시 yum 사용 필요
    ##
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'

    ##
    ## 빌드 환경 내에서 ssh-agent 실행
    ##
    - eval $(ssh-agent -s)

    ##
    ## SSH_PRIVATE_KEY 변수에 저장된 SSH 키를 에이전트 저장소에 추가
    ## We're using tr to fix line endings which makes ed25519 keys work without extra base64 encoding.
    ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
    ##
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

    ##
    ## SSH 디렉터리 생성 및 권한 부여
    ##
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    ##
    ## ssh-keyscan로 서버 키 스캔
    ##
    - ssh-keyscan ${CI_SERVER_SHELL_SSH_HOST} >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    
    ##
    ## SSH_SERVER_HOSTKEYS 변수 생성 시 주석 해제 필요
    ##
    #- echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts
    #- chmod 644 ~/.ssh/known_hosts

    ##
    ## 호스트 키 검사 비활성화. 단, man-in-the-middle attacks에 노출될 수 있음
    ## Docker executor에서만 사용할 것. Shell executor 사용 시 사용자의 SSH 구성을 덮어쓰게 됨
    ##
    #- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    ##
    ## git 명령어 사용 시 사용자 이름과 메일 지정 필요 (선택 사항)
    ##
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
  script:
    - ssh git@${CI_SERVER_SHELL_SSH_HOST}

SSH 구성에 문제가 없다면 ssh 명령 수행 시 다음과 같은 출력을 확인할 수 있다.

 

 

2. 로컬 레포지터리 구성 정의

먼저 Gitlab 파이프라인은 동작하면서 Preparing Environment 단계에서 Git 레포지터리를 내려받는다.

로컬 레포지터리의 설정을 확인하면 다음과 같은데, gitlab job token은 gitab 원격 저장소에 push 할 권한이 없다.

따라서 아래 스크립트를 추가하여 push 명령어 사용 시 사용할 URL을 별도로 지정해주어야 한다.

.. before_script 생략 ..
  script:
    ## 
    ## SSH 연결 확인
    ##
    - ssh git@${CI_SERVER_SHELL_SSH_HOST}

    ##
    ## push url 생성
    ##
    - git remote set-url --push origin git@${CI_SERVER_SHELL_SSH_HOST}:${CI_PROJECT_PATH}

설정하면 다음과 같이 설정이 변경된다.

 

 

3. 파일 작업

gitlab 레포지터리에 반영할 파일을 작업한다. 이 글에서는 간단히 text 파일을 생성하고 commit 했다.

.. 생략 ..
    ## 
    ## push 할 파일 작업
    ## 
    - echo 'hello, world!\n' >> pipeline_result.txt
    - git add pipeline_result.txt
    - git status
    - git commit -m "pipeline_text"
    - git log -1

 

 

4. 원격 저장소에 commit push

아래 스크립트로 변경 사항을 원격 저장소로 push 한다.

.. 생략 ..
    ##
    ## git push 수행
    ##
    - git push -o ci.skip origin HEAD:${CI_COMMIT_REF_NAME}

ci.skip 옵션은 파이프라인을 트리거하지 않고 commit을 push 할 때 사용한다. CI_COMMIT_REF_NAME는 현재 브랜치로 push 할 때 사용하는데, 필요시 다른 브랜치로 push해도 괜찮다.

 

+ push 부분 전체 스크립트는 접은글로 적어둔다.

더보기
  script:
    ## 
    ## SSH 연결 확인
    ##
    - ssh git@${CI_SERVER_SHELL_SSH_HOST}

    ##
    ## push url 생성
    ##
    - git remote set-url --push origin git@${CI_SERVER_SHELL_SSH_HOST}:${CI_PROJECT_PATH}

    ##
    ## 레포지터리 설정 확인
    ##
    - git remote -v
    - git config --list

    ## 
    ## push 할 파일 작업
    ## 
    - echo 'hello, world!\n' >> pipeline_result.txt
    - git add pipeline_result.txt
    - git status
    - git commit -m "pipeline_text"
    - git log -1
    
    ##
    ## git push 수행
    ##
    - git push -o ci.skip origin HEAD:$CI_COMMIT_REF_NAME

 

 

 

결과 확인

push 한 파일을 원격지에서 보이는지 확인한다.

매우 잘 동작하는 것을 확인할 수 있다.

 

 

참고 문서

https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/4

https://stackoverflow.com/questions/51716044/how-do-i-push-to-a-repo-from-within-a-gitlab-ci-pipeline

https://docs.gitlab.com/ci/jobs/ssh_keys/

https://gitlab.com/gitlab-examples/ssh-private-key/

https://www.reddit.com/r/gitlab/comments/1arymxp/is_it_possible_to_push_the_code_from_gitlab/

https://docs.gitlab.com/ci/pipelines/

728x90