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

后端 未结 5 1616
伪装坚强ぢ
伪装坚强ぢ 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 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:@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!

提交回复
热议问题