Is there a way to pre-run tests on the would-be merge commit, if branch were merged, before merge?
Asked Answered
W

2

6

We are automatically running tests on feature branches. After the feature is complete and tests pass, it is merged into the main branch.

But now we have to wait for tests to run on master, before deploying it (manually).

So, is there a way to automatically run tests on the would-be merge commit for the Feature Branch, before it is merged to master?

If that were possible, then we would know that tests on master will pass after the Feature Branch is merged.

Wiebmer answered 7/2, 2022 at 2:52 Comment(1)
Doesn't your CI system build PRs?Toile
E
2

For this the merged results pipelines might be useful for you. Take a look at https://docs.gitlab.com/ee/ci/pipelines/merged_results_pipelines.html

Quoting from docs

A merged results pipeline is a type of merge request pipeline. It is a pipeline that runs against the results of the source and target branches merged together.

So you could have the tests run preemptively and run the deploy manually

This functionality is available if your organization has a Gitlab account which is at least in PREMIUM tier.

By default they are not enabled, to enable merged results pipelines in a project, you must have at least the Maintainer role:

  1. On the top bar, select Menu > Projects and find your project.
  2. On the left sidebar, select Settings > General.
  3. Expand Merge requests.
  4. Select Enable merged results pipelines.
  5. Select Save changes.
Ern answered 7/2, 2022 at 7:43 Comment(0)
C
5

You can also do the merge manually in your script. This does not require the PREMIUM tier and also works in the community edition.

Here's an example from a script that works for us:

variables:
    # Do not checkout the branch to merge, because we will merge it to the
    # target branch in the script below before running the tests
    GIT_CHECKOUT: "false"
    # Use clone strategy to always ensure clean state (solves issue with
    # remains after merge conflict)
    GIT_STRATEGY: "clone"

before_script:
    # Install git on our alpine base runner
    - apk update
    - apk add git
    - git config --global user.email "[email protected]"
    - git config --global user.name "GitLab Runner"

test:
    only:
        - merge_requests
    script:
        - git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
        - git checkout -B $CI_MERGE_REQUEST_TARGET_BRANCH_NAME origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
        - git merge $CI_COMMIT_SHA
        - # ... run tests here ...
Charis answered 27/7, 2022 at 12:13 Comment(0)
E
2

For this the merged results pipelines might be useful for you. Take a look at https://docs.gitlab.com/ee/ci/pipelines/merged_results_pipelines.html

Quoting from docs

A merged results pipeline is a type of merge request pipeline. It is a pipeline that runs against the results of the source and target branches merged together.

So you could have the tests run preemptively and run the deploy manually

This functionality is available if your organization has a Gitlab account which is at least in PREMIUM tier.

By default they are not enabled, to enable merged results pipelines in a project, you must have at least the Maintainer role:

  1. On the top bar, select Menu > Projects and find your project.
  2. On the left sidebar, select Settings > General.
  3. Expand Merge requests.
  4. Select Enable merged results pipelines.
  5. Select Save changes.
Ern answered 7/2, 2022 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.