Hello I am a newbie in GitLab ci/cd
I have the project name “cinema” in the namespace “sample” and has branch master and dev
I have the project name “cinema-forked” in the namespace “forked” and this project forked from the “cinema” project and has branch master, dev, new(create from master)
I want to write the ci/cd to do these:
*Condition: Run every time a merge request is received from the new branch of cinema-forked to the master branch of the cinema
- Clone, fetch(or something better these) both branches
- Merge new(form cinema-forked) to master(cinema)
- Test the code after merging with the master
- Delete all working directories for the next merge request
The goal is to run a test after merging the code, and if everything is fine, the code reviewer merges the code
In bitbucket, there was a simple button “after_merge” and “before_merge” so we could choose when the test runs, But I can’t find these options in GitLab
So can you please help me?
Hi @farhaadn,
One way of doing it is to use Merged results pipelines | GitLab which is available in paid tiers. It does what you need automatically for you.
If you are not on paid tier, you could try to create a pipeline configuration on the “cinema” project and use rules to run jobs only for your specific condition. Look at Predefined variables reference | GitLab to see what’s the variables to check.
1 Like
Here is a sample .gitlab-ci.yml file that implements the CI/CD workflow you described:
image: node:14.16.0 # or whatever image you need
stages:
test:
stage: test
script:
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@example.com/sample/cinema.git
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@example.com/forked/cinema-forked.git
- cd cinema
- git fetch
- git checkout master
- git merge origin/new
- npm install
- npm test # or whatever commands needed to run tests
- cd …
- rm -rf cinema cinema-forked # clean up
only:
- merges # only run when MR is merged
when: manual # allow manual triggering if needed
The key things:
- Use CI/CD variables like
CI_JOB_TOKEN
to clone repos
- Fetch and merge the new branch from the forked repo into master branch
- Run tests after merge to validate
- Clean up directories after each run
- Trigger on merges and allow manual just in case
Let me know if you have any other questions!