How do I push to a repo from within a gitlab CI pipeline?

后端 未结 5 1614
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 10:31

In my CI pipeline I am generating an artifact public/graph.png that visualises some aspect of my code. In a later step I want to commit that to the repo from wi

5条回答
  •  臣服心动
    2020-12-17 10:56

    I found this GitLab forum link helpful As suggested by the user you need to generate SSH key, associate it with new GitLab user dedicated for this job and add key to the runner. Small drawback is you need to use swap origin in gitlab for original ssh source (instead of sandboxed one used inside the job) which leads to committer being changed to mentioned new account instead of person who triggered pipeline. Source from link:

    # for your information
    whoami
    printenv
    
    # we need to extract the ssh/git URL as the runner uses a tokenized URL
    export CI_PUSH_REPO=`echo $CI_BUILD_REPO | perl -pe 's#.*@(.+?(\:\d+)?)/#git@\1:#'`
    
    # runner runs on a detached HEAD, create a temporary local branch for editing
    git checkout -b ci_processing
    git config --global user.name "My Runner"
    git config --global user.email "runner@gitlab.example.org"
    git remote set-url --push origin "${CI_PUSH_REPO}"
    
    # make your changes
    touch test.txt
    
    # push changes
    # always return true so that the build does not fail if there are no changes
    git push origin ci_processing:${CI_BUILD_REF_NAME} || true
    

    Just with current version of GitLab you need to change source variable name as follows:

    export CI_PUSH_REPO=`echo $CI_REPOSITORY_URL | perl -pe 's#.*@(.+?(\:\d+)?)/#git@\1:#'`
    

提交回复
热议问题