How can I make Gitlab merge code with branch transition with successful build

Well, the name pretty much speaks for itself.

So, I want the branch (i.e. dev) to be merged with another branch (i.e. production), if the assembly was successful.

+7
source share
3 answers

I tried @ jakub-kania's solution, but I always got it id_rsa invalid format. I think gitlab secret variables are somehow screwed up.

I made it work by directly passing the deployment key to ssh-add without creating ssh keys. Here is a working solution:

merge to master:
  stage: deploy
  image: alpine
  only:
    - dev-branch
  before_script:
    - apk add --update git openssh-client
    - mkdir ~/.ssh
    - ssh-keyscan -p 2222 <gitlab.domain.com> > ~/.ssh/known_hosts
    - eval `ssh-agent -s`
    - ssh-add <(echo "$GITLAB_DEPLOY_KEY")
    - ssh -T git@<gitlab.domain.com> -p 2222
    - git config --global user.email "$GITLAB_USER_EMAIL"
    - git config --global user.name "$GITLAB_USER_ID"
    - git remote set-url origin ssh://git@<gitlab.domain.com>:2222/path/to/repo.git
  script:    
    - git checkout master
    - git reset --hard origin/master
    - git merge $CI_BUILD_REF
    - git push origin master
+7
source

- " ", . .

, . GITLAB_DEPLOY KEY, ssh-keyscan GITLAB_PUBLIC_KEY.

mergetomaster:
  stage: deploy
  image: alpine
  only:
   - dev
  script:
   - apk add --update git openssh-client
   - mkdir ~/.ssh
   - echo $GITLAB_DEPLOY_KEY > ~/.ssh/id_rsa
   - chmod 400 ~/.ssh/id_rsa
   - echo $GITLAB_PUBLIC_KEY > ~/.ssh/known_hosts
   // Steal the identity of person that triggered the build
   - git config --global user.email "$GITLAB_USER_EMAIL"
   - git config --global user.name "$GITLAB_USER_ID"
   - git remote set-url origin <ssh-repository-url>
   - git checkout master
   - git reset --hard origin/master
   - git merge $CI_BUILD_REF
   - git push origin master
+6

GitLab 8.15. - API webhooks.

, :

1. -, push.

2. , , .

3. Create a merge request and immediately accept it with the option "merge_when_build_succeeds": true.

Thus, it will merge the branch if the assembly is successful. Not the most convenient thing to configure, but it should work.

+1
source

Source: https://habr.com/ru/post/1669159/


All Articles