1

Is there the possibility that I can add e.g. a script which is executed during the merge Step in gitlab which can change some content of the repository?

Standard Workflow which is fully Supported with GitLab

  1. Create Merge Request in GitLab
  2. Add it to merge train to be merged
  3. Gitlab creates merge commit (temporary) if no conflicts are detected
  4. Pipeline runs to do some Integration steps
  5. Merge commit is pushed if pipeline execution was successful

What I Need additional:

I want to execute a Script after step 3 which might change again some files of the repository. These changes should be integrated in the merge commit. Something like executing a cleaner Script which is executed after the git merge before the merge commit is done (and this on remote side in Gitlab as part of the merge process).

Does anyone have an idea with a technical Solution? I would really appreciate it

bakerinc
  • 11
  • 1

1 Answers1

0

It's possible, but it's not straightforward. What you are building is basically a bot, which will have to assume some kind of personality in order to modify the code, commit and push it to the repository (ideally - via a MR).

We're using such an approach for cases like:

  • automated bumping of VERSION files
  • automating merge requests in repo B, based on changes in repo A
  • etc.

Solution

The solution is maybe a bit more generic than what you actually need, so you might need to simplify, but it works quite well.

You will need:

  1. A separate repo, where you will store the automation, let's call it bot repo
  2. An Access Token created in the target repo, with api, write_repository scopes, let's call it AUTO_MR_TOKEN
  3. A secret created in bot repo, with the contents of the AUTO_MR_TOKEN

In the bot repo you would create a gitlab-ci.yml which would have the following step:

add_mr:
  script:
    # SETUP THE ENVIRONMENT HERE
    - TARGET_REPO="https://oauth2:${AUTO_MR_TOKEN}@${CI_SERVER_HOST}/${TARGET_PROJECT}.git"
    # CLONE THE TARGET REPO
    - git clone -b $TARGET_BRANCH --single-branch $TARGET_REPO
    - git config user.email "${GIT_USER_EMAIL:-$GITLAB_USER_EMAIL}"
    - git config user.name "${GIT_USER_NAME:-$GITLAB_USER_NAME}"
    # MAKE CHANGES
    - echo "DO YOUR MAGIC HERE"
    - |
      git commit -m "commit from bot"
      git push origin master $TARGET_BRANCH \
        -o merge_request.create \
        -o merge_request.target=master 

Then, in the target repo - you would trigger this bot job, depending on your workflow, in the appropriate step of your pipeline.

andrzejwp
  • 922
  • 4
  • 11
  • Thanks a lot for your detailed answer. I am right now not completly sure if this solves my issue or if maybe don‘t understand it technical completly. So is this described action modifying the already done merge commit in Gitlab? – bakerinc Dec 16 '21 at 21:42
  • So I have already created my merge request in my target repo via Gitlab. Then after approval I click to „Add to merge train. Then the pipeline of the target repo starts (basend on the merged result which is not yet pushed back to Master). Then the first step of my Pipeline can be that the Action of my bot repo is triggered which shall Modify the gitlab merge commit (which is not pushed by gitlab yet). Then my Pipeline of the Target repo should continue with some Integration Test. – bakerinc Dec 16 '21 at 21:56
  • If these pass the merge commit shall be pushed and the merge request shall be closed. If the pipeline fails the gitlab merge commit shall not be pushed and my Feature Branch shall still only have the listest commit which Exists before clicking ‚Add to merge train‘ – bakerinc Dec 16 '21 at 21:57
  • It really depends on how you integrate this in your pipeline. In theory - you could simply take the token and clone -> commit -> push to the repo, even without an MR. Such script you can add to pretty much any existing step of your pipeline. – andrzejwp Dec 16 '21 at 21:58
  • I thought that if GitLab use the Feature „Run Pipeline based on Merged Result“ this merge is not yet comitted and therefore not accesible technically by another git bot which Clones this Branch again(as this merge result is not Committed to the Branch) – bakerinc Dec 16 '21 at 22:06
  • So maybe my question in General is: Gitlab Provides the Feature that the Merge Check Pipeline Runs based on the merged result. Is it possible that this Pipeline changes the content of the merged result and that if Then the Pipeline is succesfull this modified merge result is pushed as merge commit back to Master. – bakerinc Dec 16 '21 at 22:08
  • Ok, now I get your point. I haven't worked with this feature, to be honest. I see 2 options, though. 1. You could, possibly, add this automation as a step executed *before* the merge happens. 2. Maybe you could do it in a "pre-main" branch, after the train is merged - you can create a job that triggers the automation, which then opens another MR, this time to the main branch. – andrzejwp Dec 16 '21 at 22:24
  • Yes thanks :) Generally a good idea with the pre-main. However this would not work fully for me then parts of my Integration tests needs to run in another pipeline before pre-main is merged to Master. This tests can fail, and if this Happens I get a merge jam from pre-main to Main. Then the old merge request from feature to pre-Main should still not be done already. So i really Need to Test before merge my State After merged result and postprocessing with all my Tests :) Quite complex :O – bakerinc Dec 16 '21 at 22:32
  • Can you please provide in your question a diagram that depicts your case - as you mentioned - it is a bit complex ;) – andrzejwp Dec 17 '21 at 09:19