What code is run on a merged branch in gtilab CICD - gitlab

This may be a complicated question, but I'll do my best.
In branch feature/1 I have migration One
In branch main, I have migration Two
If I merge branch feature/1 into branch main and then run my database migration on all migrations in a CICD job, would both migrations run?
In my pipeline, I just did this, and only the migrations in the source branch were run, and the migrations in the target branch were ignored. The migrations in branch main were merged after I branched from main but before I merged feature/1 back into main.
In this example migration two was never run because the migration job was not implemented yet.

Related

Why bitbucket pipeline merges pull request before it runs?

In their documentation regarding pull request pipelines, bitbucket says:
Pull requests:
a special pipeline that only runs on pull requests initiated from within your repository. It merges the destination branch into your working branch before it runs. If the merge fails, the pipeline stops.
So I'm wondering, why merging before running the pipeline? Why not just running against the coming branch without merging?
Could the reason be detecting merge conflicts early on in the pipeline before the real merge?
If you want to run a pipeline against the coming branch, this is very doable by using Branch workflows. PR merge trigger is just a slightly different idea, as the result of a PR merge is not necessarily the same as the coming branch. For example, merge conflicts can be introduced, which will make your pipeline fail.
There's one thing that documentation is not quite clear about, so I'll clarify it: all this pre-pipeline merging only occurs inside your build environment. Git history of your repository is absolutely safe, and Bitbucket Pipelines won't introduce any changes to it on your behalf.
Finally, you can run a PR merge pipeline manually from the Pipelines UI, without actually merging a PR (see the same link). This way, you can make sure that the merge result build is passing without actually doing a merge.

Azure multi branch pipeline

How do we create multi branch pipeline in azure.
Say there are 4 developers committing to the same branch, for each commit there should be a seperate build pipeline with unit tests.
for each commit there should be a seperate build pipeline with unit tests.
If you want to have a seperate build pipeline with unit tests for each commit, you need to create multi pipeline for each branch:
And if just want to one build pipeline for those multi branch, you may need to have create multi branch triggers:
I think what you need is a CI pipeline. So lets say, there is a main/master branch, 4 developers have pulled this main branch in their PC/laptop. And all 4 of them are about to work on 4 different defects/bugs say bug1,Bug2, Bug3 and Bug4
Each one of them are supposed to create a branch from main. Say bug/Bug1, bug/Bug2, bug/Bug3 and bug/Bug4. So now we got 4 branches.
What you can do now is switch on pull request branch policies in main branch. Please see below screenshot
Once you switch on this pull request policy, no code can be merged in your main branch without creating a pull request.
Now second part, you have to create a pipeline that will for example, build the code and run some tests.
Now part three: In the same branch policies page, if you scroll down a bit, you will see a setting for "Build Validation" as shown below
What you have to do here is
Press that + sign and configure the pipeline that you have created earlier here.
What this means is
When say Dev 1 whose branch is bug/Bug1 will raise a pull request to merge in main branch, this Build validation will trigger the pipeline that you have configured here.
So when all 4 Devs will raise 4 pull requests, based on their code changes, this pipeline will get triggered and validate those code changes for build + tests.
Also, based on the code review, if there are any further code changes pushed into these branches (bug/Bug1 etc.), you can configure this Build validation in such a way that this pipeline will get automatically triggered whenever new code changes are pushed into a branch which is connected to a pull request.

How to Trigger a pull request using script or pipeline once build is successful from azure devops

I have a requirement. Whether it is possible or not I don't know
Requirement: I have three branches 1. Master, 2. Dev, 3. Test.
Here every time new code is committed to dev or test and we create a build to execute the code. Now the requirement is I don't want to merge the code to Master branches unless build is success for master. When build is successful then only PR will be triggered and code will be merged to Master Branch.
Master build ---> Success ----> Then merge code with Master Branch.
Is this possible, without merging the new code into main branch, how can we test the new code from master branch in the build. And if that build is success then only PR has to take place and merge the code in master branch.
My requirement is Once the normal build is success then only PR has to be triggered and merge the code with master branch. Can this be achievable with powershell/bash script that can trigger a PR from pipeline (Build)
I'm pretty sure what you are looking for is branch polices Build validation instead of trigger a PR from pipeline.
You may not fully understand the logic of Build validation and PR.
What you want to achieve is to protect the master and not merge the PR on the branch to the master until the build verification success.
So, the key point is to verify the code after the PR merge, instead of triggering the PR after the build is completed. Otherwise, your build will always be the code on the master before the merge, and this verification will be meaningless.
Adding the Build validation on the master branch, which will trigger the build pipeline when we create a pull request. When we create a PR, it will create a temp commit to save the merged code and build pipeline will checkout this new temp commit to build. If build successful, then we could complete the PR, otherwise, we could not complete the PR, the master code would not be updated.
That should be what you want.
Azure DevOps uses branch polices to achieve this which you can read about Here

GitLab CI - separate environments deployment and semantic release

I am trying to follow GitLab flow with staging and production environments. I also want semantic-release plugin for both environments (with this I also need a branch for every environment so semantic-release could work, right?)
I have successfully made semantic-release working for staging branch, it creates vX.X.X-rc.x as expected and it also generates changelog. Great!
But how should I properly work with those branches? I think I understand how would I do that with only master branch and two environments, but I couldn't find any info about doing this with separate branches.
I create feature branch, finish feature, merge to master. Now what? Should I have deployment job, which on every merge to master automatically merges master to staging, runs semantic-release and then deploys it? And then have manual deployment which automatically merges staging to production, runs semantic-release and then deploys production?
And second think: how would I deal with forced commit message conventions? If I am merging feature to master, there is commit message feat(x): something something. But what should be in those automatic master to staging and staging to production merges?

Can I combine two branches into a GitLab CI build?

I know that GitLab CI will build each branch separately, but when I build the feature branch I want to compare some test results against the same tests on the master branch.
I could do that against the most recent build of master, or I'm happy to rebuild master on every feature branch commit if required.
Is that possible in GitLab CI?
You can use pipeline triggers to build your master branch during a feature branch pipeline.
In your case I guess you want to wait for the master pipeline to complete. This is not the default Gitlab behavior.
Using a script called pipeline-commander you can wait for the trigger to complete.

Resources