Run gitlab-ci.yml only when merge request to master made - node.js

I am currently having my project in GitLab and Heroku. What I wanna do is as soon as I ask for merge request with my feature branch (let's call it crud-on-spaghetti), I want to automatically run the tests on this branch (npm test basically, using Mocha/Chai), and after they succeed, merge this crud-on-spaghetti with master, commit it and push it to origin/master (which is remote on GitLab) and after git push heroku master (basically, push it to the master branch in Heroku, where my app is stored). I have read several articles on GitLab CI and I think this is more suitable for me (rather than Heroku CI, because I do not have DEV and PROD instances).
So, as of now, I do this manually. And this is my .gitlab-ci.yml file now (which is not committed/pushed yet):
stages:
- test
- deploy
test_for_illegal_bugs:
stage: test
script:
- npm test
deploy_to_dev:
stage: deploy
only:
- origin master
script:
- git commit
- git push origin master
- git pull heroku master --rebase
- git push heroku master
Hence, my questions is: What do I exactly need to write in .gitlab-ci.yml in order to automate all these "manipulations" (above)?
PS. And another (theoretical) follow-up question: how is GitLab-CI Runner triggered? For instance, if I want it to trigger upon merge request with master, do I do that using only: ... in .gitlab-ci.yml?

Restrict stages to Merge Requests:
To have your test stage only being executed when a Merge Request (MR) is opened, use
only:
- merge_requests
According to the Gitlab docs, you can further restrict this to only being executed for MRs with a certain target branch, e.g. only MRs for master
only:
- merge_requests
except:
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master"
This adds an exception for all target branches that are not master.
Or use rules: for that:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
Restrict stages to Branches:
As already mentioned by #marcolz, this is achieved by
only:
- master
to only execute the stage for pushes to the master branch.

Try
only:
- master
origin is just a name for a remote. master is the name of the branch.
The runner is triggered by GitLab-CI the moment that a commit is pushed to the repository, so alas not upon merge request.
You can use a trigger to trigger a pipeline and then call that trigger from a merge request event in integrations.

Related

Gitlab auto pull

I have repository in GitLab. and I have Test and Dev branches in this repository.
In Gitlab pipeline, I schedule a job to auto run Test branch pipeline every 24 hours.
in Test .gitlab-ci.yml I have
deploy:
stage: deploy
script:
- git pull --ff-only origin Dev
only:
- Test
After merging Dev in Test, this part of code was removed. And next time Test branch pipeline could not pull from Dev branch, when pipeline run.
How can I pull code from Dev branch to Test Branch without losing the code - git pull --ff-only origin Dev ?
Or maybe it is possible to have tow .gitlab-ci.yml on branch? (if yes, how GitLab should know which one of them be diploid first? )
Or maybe it is possible to have two .gitlab-ci.yml on branch
You would generally create your .gitlab-ci.yml in the main/master branch.
Which means that .gitlab-ci.yml would not be impacted by merges between Test and Dev. Which is what you need.

Already up to date gitlab pipeline stackoverflow

i trying gitlab pipeline. now i make some changes & code pushed in master branch
pipeline showing already update to date but i have changes in code
I try to pull in three phase but still same issue
.gitlab-ci.yml
before_script:
- echo "Before script"
building:
stage: build
script:
- git pull origin master
testing:
stage: test
script:
- git pull origin master
deploying:
stage: deploy
script:
- git pull origin master
If the gitlab-ci workflow starts by cloning your repository, no amount of git pull will change the fact you already have the full history, and, at the time of the workflow, this is "already up to date".
In other words, a git pull would not be needed in your gitlab-ci.yml file.
If your pipeline is running on the same repo that you changed, there is no need to use git pull. Although, if your pipeline triggers (on repo A) another pipeline on another repository (repo B), to access files in repo A, you have to pull repo A in repo B pipeline.

GitLab CI/CD yml - How to add into pipeline job if new branch created. And exclude for further pushes?

In our git process we have hothix process. The process is to create a hotfix brunch from master and make changes and then merge to master back.
The issue that I'm faсing now is how to add job to pipelne if branch just created and exclude from pipeline for any further push?
When branch is created we need to set up custom environment. Create a sandbox, push code and push some test data. This is need only when branch is created. In case of changes and pushes, environment is available and no need in building it again. Just build code and push a release tag
I'm trying something like this:
job-name:
stage: build
script:
- myScript
rules:
- if: '$CI_COMMIT_BRANCH =~ /^hotfix.*/'
changes:
- force-app/**/*
when: never
- if: '$CI_COMMIT_BRANCH =~ /^hotfix.*/'
allow_failure: false
when: manual
And for sure it's not working as changes: not part of if policy:
Rules are evaluated in order until a match is found. If a match is found, the attributes are checked to see if the job should be added to the pipeline.

Run gitlab runner pipeline on merge requests when origin branch name has a specific name

We create new branches from develop branch that starts with hotfix/bla_bla or feature/bla_bla. Then, we merge them back into develop branch. On merge requests, I would like to run a job only when we merge feature branches into develop branch. Something like:
job:
stage: test
only:
refs:
- develop && "when a branch which starts with 'feature/' is merged into develop"
How could I achieve this in .gitlab-ci.yml file or using a .sh file?
Untested, but you potentially can use a combination of pipelines for merge requests and the CI Variable CI_MERGE_REQUEST_SOURCE_BRANCH_NAME.
For example, something like:
job:
stage: test
only:
refs:
- merge_requests
- develop
variables:
- $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "feature/*"

Troubles with .gitlab-ci.yml triggers configuration

How should I set up my .gitlab-ci.yml manifest to run builds ONLY on:
Merge Request;
Push to branch with opened merge request from it (I mean when the merge request from branch Y to branch X is already opened and some new changes are pushed to branch Y);
Push to master;
I've tried to solve it with a setting like this:
job:
only:
- triggers
- /merge-requests/
- master
except:
- branches
Regarding on the documentation here: https://docs.gitlab.com/ce/ci/yaml/README.html#only-and-except-simplified
Suddenly the error occurred on my MR page:
Could not connect to the CI server. Please check your settings and try again.
When I removed only/except restrictions from my manifest, the error was gone.
What am I doing wrong here?
My Gitlab version is: GitLab Community Edition 10.8.1
You want to run a job only on:
merge request: I don't understand what you want here
Push to branch with opened merge request from it: you have to set a special job that call the Gitlab API to control that the current branch has a MR
A job executed only on new pushed branch:
image: alpine:latest
script:
- # <-- add here the script that call Gitlab API
only:
- branches
Push to master:
A job executed only on master:
image: alpine:latest
script:
- echo "Hello world!"
only:
- master

Resources