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

后端 未结 5 1591
伪装坚强ぢ
伪装坚强ぢ 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:#'`
    
    0 讨论(0)
  • 2020-12-17 11:10

    I can commit from Gitlab-CI with a selected user with a minor change based on tsr's answer https://stackoverflow.com/a/57800614/5269825 :

    # set remote URL to https://oauth2:<AccessToken>@server.com/project.git
    CI_PUSH_REPO=`echo "$CI_REPOSITORY_URL $ACCESS_TOKEN_PARAM" | sed 's/^.*\(@.*\)\s\(.*\)/https:\/\/oauth2:\2\1/g'`
    git config http.sslverify false
    git remote set-url --push origin "${CI_PUSH_REPO}"
    git config user.name "Token Owner"
    git config user.email "tokenowner@email.com"
    
    # runner runs on a detached HEAD, create a temporary local branch for editing
    git checkout -b ci_processing
    # make your changes
    
    # 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
    

    The ACCESS_TOKEN_PARAM must be configured at the project's CI/CD Variables configuration.

    The idea of using Oauth2 and Access Token was taken from https://stackoverflow.com/a/52074198/5269825 and https://stackoverflow.com/a/52154378/5269825.

    Also, pushing changes can trigger a new pipeline!

    0 讨论(0)
  • 2020-12-17 11:16

    Solved it. Issuing git config --global http.sslverify "false" prior to the push solved that particular problem (it exposed another problem but that's for another thread :) )

    0 讨论(0)
  • 2020-12-17 11:16

    You can add the CI_SERVER_CLS_CA_FILE to sslCAInfo git config.

    checkout alchemy:
        stage: prepare
        script:
            - git config --global "http.${CI_SERVER_URL}.sslCAInfo" "$CI_SERVER_TLS_CA_FILE"
            - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/sparklemuffin/alchemy.git
    

    While cloning a different repository from a pipeline, I have run into the same issue. It failed with server certificate verification failed. I did not understand why this was happening, Gitlab itself clones the repository without any issues. So I set CI_DEBUG_TRACE: "true" and found out, Gitlab creates this file configures git to use it for initially cloning the repository. For some reason this configuration is no longer available later on. CI_SERVER_TLS_CA_FILE persists, though.

    0 讨论(0)
  • 2020-12-17 11:17

    In my case deploy keys option was optimal (compared to personal tokens or CI token - only supports basic auth) with Gitlab Shell Runner. In case someone is struggling with pushing to from Gitlab CI, this can be done sharing the public key of the runner with the Gitlab server

    0 讨论(0)
提交回复
热议问题