• United States+1
  • United Kingdom+44
  • Afghanistan (‫افغانستان‬‎)+93
  • Albania (Shqipëri)+355
  • Algeria (‫الجزائر‬‎)+213
  • American Samoa+1684
  • Andorra+376
  • Angola+244
  • Anguilla+1264
  • Antigua and Barbuda+1268
  • Argentina+54
  • Armenia (Հայաստան)+374
  • Aruba+297
  • Australia+61
  • Austria (Österreich)+43
  • Azerbaijan (Azərbaycan)+994
  • Bahamas+1242
  • Bahrain (‫البحرين‬‎)+973
  • Bangladesh (বাংলাদেশ)+880
  • Barbados+1246
  • Belarus (Беларусь)+375
  • Belgium (België)+32
  • Belize+501
  • Benin (Bénin)+229
  • Bermuda+1441
  • Bhutan (འབྲུག)+975
  • Bolivia+591
  • Bosnia and Herzegovina (Босна и Херцеговина)+387
  • Botswana+267
  • Brazil (Brasil)+55
  • British Indian Ocean Territory+246
  • British Virgin Islands+1284
  • Brunei+673
  • Bulgaria (България)+359
  • Burkina Faso+226
  • Burundi (Uburundi)+257
  • Cambodia (កម្ពុជា)+855
  • Cameroon (Cameroun)+237
  • Canada+1
  • Cape Verde (Kabu Verdi)+238
  • Caribbean Netherlands+599
  • Cayman Islands+1345
  • Central African Republic (République centrafricaine)+236
  • Chad (Tchad)+235
  • Chile+56
  • China (中国)+86
  • Christmas Island+61
  • Cocos (Keeling) Islands+61
  • Colombia+57
  • Comoros (‫جزر القمر‬‎)+269
  • Congo (DRC) (Jamhuri ya Kidemokrasia ya Kongo)+243
  • Congo (Republic) (Congo-Brazzaville)+242
  • Cook Islands+682
  • Costa Rica+506
  • Côte d’Ivoire+225
  • Croatia (Hrvatska)+385
  • Cuba+53
  • Curaçao+599
  • Cyprus (Κύπρος)+357
  • Czech Republic (Česká republika)+420
  • Denmark (Danmark)+45
  • Djibouti+253
  • Dominica+1767
  • Dominican Republic (República Dominicana)+1
  • Ecuador+593
  • Egypt (‫مصر‬‎)+20
  • El Salvador+503
  • Equatorial Guinea (Guinea Ecuatorial)+240
  • Eritrea+291
  • Estonia (Eesti)+372
  • Ethiopia+251
  • Falkland Islands (Islas Malvinas)+500
  • Faroe Islands (Føroyar)+298
  • Fiji+679
  • Finland (Suomi)+358
  • France+33
  • French Guiana (Guyane française)+594
  • French Polynesia (Polynésie française)+689
  • Gabon+241
  • Gambia+220
  • Georgia (საქართველო)+995
  • Germany (Deutschland)+49
  • Ghana (Gaana)+233
  • Gibraltar+350
  • Greece (Ελλάδα)+30
  • Greenland (Kalaallit Nunaat)+299
  • Grenada+1473
  • Guadeloupe+590
  • Guam+1671
  • Guatemala+502
  • Guernsey+44
  • Guinea (Guinée)+224
  • Guinea-Bissau (Guiné Bissau)+245
  • Guyana+592
  • Haiti+509
  • Honduras+504
  • Hong Kong (香港)+852
  • Hungary (Magyarország)+36
  • Iceland (Ísland)+354
  • India (भारत)+91
  • Indonesia+62
  • Iran (‫ایران‬‎)+98
  • Iraq (‫العراق‬‎)+964
  • Ireland+353
  • Isle of Man+44
  • Israel (‫ישראל‬‎)+972
  • Italy (Italia)+39
  • Jamaica+1876
  • Japan (日本)+81
  • Jersey+44
  • Jordan (‫الأردن‬‎)+962
  • Kazakhstan (Казахстан)+7
  • Kenya+254
  • Kiribati+686
  • Kosovo+383
  • Kuwait (‫الكويت‬‎)+965
  • Kyrgyzstan (Кыргызстан)+996
  • Laos (ລາວ)+856
  • Latvia (Latvija)+371
  • Lebanon (‫لبنان‬‎)+961
  • Lesotho+266
  • Liberia+231
  • Libya (‫ليبيا‬‎)+218
  • Liechtenstein+423
  • Lithuania (Lietuva)+370
  • Luxembourg+352
  • Macau (澳門)+853
  • Macedonia (FYROM) (Македонија)+389
  • Madagascar (Madagasikara)+261
  • Malawi+265
  • Malaysia+60
  • Maldives+960
  • Mali+223
  • Malta+356
  • Marshall Islands+692
  • Martinique+596
  • Mauritania (‫موريتانيا‬‎)+222
  • Mauritius (Moris)+230
  • Mayotte+262
  • Mexico (México)+52
  • Micronesia+691
  • Moldova (Republica Moldova)+373
  • Monaco+377
  • Mongolia (Монгол)+976
  • Montenegro (Crna Gora)+382
  • Montserrat+1664
  • Morocco (‫المغرب‬‎)+212
  • Mozambique (Moçambique)+258
  • Myanmar (Burma) (မြန်မာ)+95
  • Namibia (Namibië)+264
  • Nauru+674
  • Nepal (नेपाल)+977
  • Netherlands (Nederland)+31
  • New Caledonia (Nouvelle-Calédonie)+687
  • New Zealand+64
  • Nicaragua+505
  • Niger (Nijar)+227
  • Nigeria+234
  • Niue+683
  • Norfolk Island+672
  • North Korea (조선 민주주의 인민 공화국)+850
  • Northern Mariana Islands+1670
  • Norway (Norge)+47
  • Oman (‫عُمان‬‎)+968
  • Pakistan (‫پاکستان‬‎)+92
  • Palau+680
  • Palestine (‫فلسطين‬‎)+970
  • Panama (Panamá)+507
  • Papua New Guinea+675
  • Paraguay+595
  • Peru (Perú)+51
  • Philippines+63
  • Poland (Polska)+48
  • Portugal+351
  • Puerto Rico+1
  • Qatar (‫قطر‬‎)+974
  • Réunion (La Réunion)+262
  • Romania (România)+40
  • Russia (Россия)+7
  • Rwanda+250
  • Saint Barthélemy (Saint-Barthélemy)+590
  • Saint Helena+290
  • Saint Kitts and Nevis+1869
  • Saint Lucia+1758
  • Saint Martin (Saint-Martin (partie française))+590
  • Saint Pierre and Miquelon (Saint-Pierre-et-Miquelon)+508
  • Saint Vincent and the Grenadines+1784
  • Samoa+685
  • San Marino+378
  • São Tomé and Príncipe (São Tomé e Príncipe)+239
  • Saudi Arabia (‫المملكة العربية السعودية‬‎)+966
  • Senegal (Sénégal)+221
  • Serbia (Србија)+381
  • Seychelles+248
  • Sierra Leone+232
  • Singapore+65
  • Sint Maarten+1721
  • Slovakia (Slovensko)+421
  • Slovenia (Slovenija)+386
  • Solomon Islands+677
  • Somalia (Soomaaliya)+252
  • South Africa+27
  • South Korea (대한민국)+82
  • South Sudan (‫جنوب السودان‬‎)+211
  • Spain (España)+34
  • Sri Lanka (ශ්‍රී ලංකාව)+94
  • Sudan (‫السودان‬‎)+249
  • Suriname+597
  • Svalbard and Jan Mayen+47
  • Swaziland+268
  • Sweden (Sverige)+46
  • Switzerland (Schweiz)+41
  • Syria (‫سوريا‬‎)+963
  • Taiwan (台灣)+886
  • Tajikistan+992
  • Tanzania+255
  • Thailand (ไทย)+66
  • Timor-Leste+670
  • Togo+228
  • Tokelau+690
  • Tonga+676
  • Trinidad and Tobago+1868
  • Tunisia (‫تونس‬‎)+216
  • Turkey (Türkiye)+90
  • Turkmenistan+993
  • Turks and Caicos Islands+1649
  • Tuvalu+688
  • U.S. Virgin Islands+1340
  • Uganda+256
  • Ukraine (Україна)+380
  • United Arab Emirates (‫الإمارات العربية المتحدة‬‎)+971
  • United Kingdom+44
  • United States+1
  • Uruguay+598
  • Uzbekistan (Oʻzbekiston)+998
  • Vanuatu+678
  • Vatican City (Città del Vaticano)+39
  • Venezuela+58
  • Vietnam (Việt Nam)+84
  • Wallis and Futuna+681
  • Western Sahara (‫الصحراء الغربية‬‎)+212
  • Yemen (‫اليمن‬‎)+967
  • Zambia+260
  • Zimbabwe+263
  • Åland Islands+358
Thanks! We'll be in touch in the next 12 hours
Oops! Something went wrong while submitting the form.

Create CI/CD Pipeline in GitLab in under 10 mins

Shivani Shinde

Cloud & DevOps

Why Chose GitLab Over Other CI tools?

If there are many tools available in the market, like CircleCI, Github Actions, Travis CI, etc., what makes GitLab CI so special? The easiest way for you to decide if GitLab CI is right for you is to take a look at following use-case:

GitLab knocks it out of the park when it comes to code collaboration and version control. Monitoring the entire code repository along with all branches becomes manageable. With other popular tools like Jenkins, you can only monitor some branches. If your development teams are spread across multiple locations globally, GitLab serves a good purpose. Regarding price, while Jenkins is free, you need to have a subscription to use all of Gitlab’s features.

In GitLab, every branch can contain the gitlab-ci.yml file, which makes it easy to modify the workflows. For example, if you want to run unit tests on branch A and perform functional testing on branch B, you can simply modify the YAML configuration for CI/CD, and the runner will take care of running the job for you. Here is a comprehensive list of Pros and Cons of Gitlab to help you make a better decision.

Intro

GitLab is an open-source collaboration platform that provides powerful features beyond hosting a code repository. You can track issues, host packages and registries, maintain Wikis, set up continuous integration (CI) and continuous deployment (CD) pipelines, and more.

In this tutorial, you will configure a pipeline with three stages: build, deploy, test. The pipeline will run for each commit pushed to the repository.

GitLab and CI/CD

As we all are aware, a fully-fledged CI/CD pipeline primarily includes the following stages:

  • Build
  • Test
  • Deploy

Here is a pictorial representation of how GitLab covers CI and CD:

Source: gitlab.com

Let’s take a look at an example of an automation testing pipeline. Here, CI empowers test automation and CD automates the release process to various environments. The below image perfectly demonstrates the entire flow.

Source: xenonstack.com

Let’s create the basic 3-stage pipeline

Step 1: Create a project > Create a blank project

Visit gitlab.com and create your account if you don't have one already. Once done, click “New Project,” and on the following screen, click “Create Blank Project.” Name it My First Project, leave other settings to default for now, and click Create.
Alternatively, if you already have your codebase in GitLab, proceed to Step 2.

Step 2: Create a GitLab YAML

To create a pipeline in GitLab, we need to define it in a YAML file. This yaml file should reside in the root directory of your project and should be named gitlab-ci.yml. GitLab provides a set of predefined keywords that are used to define a pipeline. 

In order to design a basic pipeline, let’s understand the structure of a pipeline. If you are already familiar with the basic structure given below, you may want to jump below to the advanced pipeline outline for various environments.

The hierarchy in GitLab has Pipeline > Stages > Jobs as shown below. The Source or SRC  is often a git commit or a CRON job, which triggers the pipeline on a defined branch.

Now, let’s understand the commonly used keywords to design a pipeline:

  1. stages: This is used to define stages in the pipeline.
  2. variables: Here you can define the environment variables that can be accessed in all the jobs.
  3. before_script: This is a list of commands to be executed before each job. For example: creating specific directories, logging, etc.
  4. artifacts: If your job creates any artifacts, you can mention the path to find them here.
  5. after_script: This is a list of commands to be executed after each job. For example: cleanup.
  6. tags: This is a tag/label to identify the runner or a GitLab agent to assign your jobs to. If the tags are not specified, the jobs run on shared runners.
  7. needs: If you want your jobs to be executed in a certain order or you want a particular job to be executed before the current job, then you can set this value to the specific job name.
  8. only/except: These keywords are used to control when the job should be added to the pipeline. Use ‘only’ to define when a job should be added, whereas ‘except’ is used to define when a job should not be added. Alternatively, the ‘rules’ keyword is also used to add/exclude jobs based on conditions.

You can find more keywords here.

Let’s create a sample YAML file.

stages:
- build
- deploy
- test
variables:
RAILS_ENV: "test"
NODE_ENV: "test"
GIT_STRATEGY: "clone"
CHROME_VERSION: "103"
DOCKER_VERSION: "20.10.14"
build-job:
stage: build
script:
- echo "Check node version and build your binary or docker image."
- node -v
- bash buildScript.sh
deploy-code:
stage: deploy
needs: build-job
script:
- echo "Deploy your code "
- cd to/your/desired/folder
- bash deployScript.sh
test-code:
stage: test
needs: deploy-code
script:
- echo "Run your tests here."
- cd to/your/desired/folder
- npm run test
view raw .yaml hosted with ❤ by GitHub

As you can see, if you have your scripts in a bash file, you can run them from here providing the correct path. 

Once your YAML is ready, commit the file. 

Step 3: Check Pipeline Status

Navigate to CICD > Pipelines from the left navigation bar. You can check the status of the pipeline on this page.

Here, you can check the commit ID, branch, the user who triggered the pipeline, stages, and their status.

If you click on the status, you will get a detailed view of pipeline execution.

If you click on a job under any stage, you can check console logs in detail.

If you have any artifacts created in your pipeline jobs, you can find them by clicking on the 3 dots for the pipeline instance.

Advanced Pipeline Outline

For an advanced pipeline that consists of various environments, you can refer to the below YAML. Simply remove the echo statements and replace them with your set of commands.

image: your-repo:tag
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
DOCKER_HOST: tcp://localhost:2375
SAST_DISABLE_DIND: "true"
DS_DISABLE_DIND: "false"
GOCACHE: "$CI_PROJECT_DIR/.cache"
cache: # this section is used to cache libraries etc between pipeline runs thus reducing the amount of time required for pipeline to run
key: ${CI_PROJECT_NAME}
paths:
- cache-path/
#include:
#- #echo "You can add other projects here."
#- #project: "some/other/important/project"
#ref: main
#file: "src/project.yml"
default:
tags:
- your-common-instance-tag
stages:
- build
- test
- deploy_dev
- dev_tests
- deploy_qa
- qa_tests
- rollback_qa
- prod_gate
- deploy_prod
- rollback_prod
- cleanup
build:
stage: build
services:
- docker:19.03.0-dind
before_script:
- echo "Run your pre-build commnadss here"
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -t $CI_REGISTRY/repo:$DOCKER_IMAGE_TAG --build-arg GITLAB_USER=$GITLAB_USER --build-arg GITLAB_PASSWORD=$GITLAB_PASSWORD -f ./Dockerfile .
- docker push $CI_REGISTRY/repo:$DOCKER_IMAGE_TAG
- echo "Run your builds here"
unit_test:
stage: test
image: your-repo:tag
script:
- echo "Run your unit tests here"
linting:
stage: test
image: your-repo:tag
script:
- echo "Run your linting tests here"
sast:
stage: test
image: your-repo:tag
script:
- echo "Run your Static application security testing here "
deploy_dev:
stage: deploy_dev
image: your-repo:tag
before_script:
- source file.sh
- export VARIABLE="$VALUE"
- echo "deploy on dev"
script:
- echo "deploy on dev"
after_script:
#if deployment fails run rollback on dev
- echo "Things to do after deployment is run"
only:
- master #Depends on your branching strategy
integration_test_dev:
stage: dev_tests
image: your-repo:tag
script:
- echo "run test on dev"
only:
- master
allow_failure: true # In case failures are allowed
deploy_qa:
stage: deploy_qa
image: your-repo:tag
before_script:
- source file.sh
- export VARIABLE="$VALUE"
- echo "deploy on qa"
script:
- echo "deploy on qa
after_script:
#if deployment fails run rollback on qa
- echo "Things to do after deployment script is complete "
only:
- master
needs: ["integration_test_dev", "deploy_dev"]
allow_failure: false
integration_test_qa:
stage: qa_tests
image: your-repo:tag
script:
- echo "deploy on qa
only:
- master
allow_failure: true # in case you want to allow failures
rollback_qa:
stage: rollback_qa
image: your-repo:tag
before_script:
- echo "Things to rollback after qa integration failure"
script:
- echo "Steps to rollback"
after_script:
- echo "Things to do after rollback"
only:
- master
needs:
[
"deploy_qa",
]
when: on_failure #This will run in case the qa deploy job fails
allow_failure: false
prod_gate: # this is manual gate for prod approval
before_script:
- echo "your commands here"
stage: prod_gate
only:
- master
needs:
- deploy_qa
when: manual
deploy_prod:
stage: deploy_prod
image: your-repo:tag
tags:
- some-tag
before_script:
- source file.sh
- echo "your commands here"
script:
- echo "your commands here"
after_script:
#if deployment fails
- echo "your commands here"
only:
- master
needs: [ "deploy_qa"]
allow_failure: false
rollback_prod: # This stage should be run only when prod deployment fails
stage: rollback_prod
image: your-repo:tag
before_script:
- export VARIABLE="$VALUE"
- echo "your commands here"
script:
- echo "your commands here"
only:
- master
needs: [ "deploy_prod"]
allow_failure: false
when: on_failure
cleanup:
stage: cleanup
script:
- echo "run cleanup"
- rm -rf .cache/
when: always
view raw .yaml hosted with ❤ by GitHub

Conclusion

If you have worked with Jenkins, you know the pain points of working with groovy code. Thus, GitLab CI makes it easy to design, understand, and maintain the pipeline code. 

Here are some pros and cons of using GitLab CI that will help you decide if this is the right tool for you!

Get the latest engineering blogs delivered straight to your inbox.
No spam. Only expert insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings

Create CI/CD Pipeline in GitLab in under 10 mins

Why Chose GitLab Over Other CI tools?

If there are many tools available in the market, like CircleCI, Github Actions, Travis CI, etc., what makes GitLab CI so special? The easiest way for you to decide if GitLab CI is right for you is to take a look at following use-case:

GitLab knocks it out of the park when it comes to code collaboration and version control. Monitoring the entire code repository along with all branches becomes manageable. With other popular tools like Jenkins, you can only monitor some branches. If your development teams are spread across multiple locations globally, GitLab serves a good purpose. Regarding price, while Jenkins is free, you need to have a subscription to use all of Gitlab’s features.

In GitLab, every branch can contain the gitlab-ci.yml file, which makes it easy to modify the workflows. For example, if you want to run unit tests on branch A and perform functional testing on branch B, you can simply modify the YAML configuration for CI/CD, and the runner will take care of running the job for you. Here is a comprehensive list of Pros and Cons of Gitlab to help you make a better decision.

Intro

GitLab is an open-source collaboration platform that provides powerful features beyond hosting a code repository. You can track issues, host packages and registries, maintain Wikis, set up continuous integration (CI) and continuous deployment (CD) pipelines, and more.

In this tutorial, you will configure a pipeline with three stages: build, deploy, test. The pipeline will run for each commit pushed to the repository.

GitLab and CI/CD

As we all are aware, a fully-fledged CI/CD pipeline primarily includes the following stages:

  • Build
  • Test
  • Deploy

Here is a pictorial representation of how GitLab covers CI and CD:

Source: gitlab.com

Let’s take a look at an example of an automation testing pipeline. Here, CI empowers test automation and CD automates the release process to various environments. The below image perfectly demonstrates the entire flow.

Source: xenonstack.com

Let’s create the basic 3-stage pipeline

Step 1: Create a project > Create a blank project

Visit gitlab.com and create your account if you don't have one already. Once done, click “New Project,” and on the following screen, click “Create Blank Project.” Name it My First Project, leave other settings to default for now, and click Create.
Alternatively, if you already have your codebase in GitLab, proceed to Step 2.

Step 2: Create a GitLab YAML

To create a pipeline in GitLab, we need to define it in a YAML file. This yaml file should reside in the root directory of your project and should be named gitlab-ci.yml. GitLab provides a set of predefined keywords that are used to define a pipeline. 

In order to design a basic pipeline, let’s understand the structure of a pipeline. If you are already familiar with the basic structure given below, you may want to jump below to the advanced pipeline outline for various environments.

The hierarchy in GitLab has Pipeline > Stages > Jobs as shown below. The Source or SRC  is often a git commit or a CRON job, which triggers the pipeline on a defined branch.

Now, let’s understand the commonly used keywords to design a pipeline:

  1. stages: This is used to define stages in the pipeline.
  2. variables: Here you can define the environment variables that can be accessed in all the jobs.
  3. before_script: This is a list of commands to be executed before each job. For example: creating specific directories, logging, etc.
  4. artifacts: If your job creates any artifacts, you can mention the path to find them here.
  5. after_script: This is a list of commands to be executed after each job. For example: cleanup.
  6. tags: This is a tag/label to identify the runner or a GitLab agent to assign your jobs to. If the tags are not specified, the jobs run on shared runners.
  7. needs: If you want your jobs to be executed in a certain order or you want a particular job to be executed before the current job, then you can set this value to the specific job name.
  8. only/except: These keywords are used to control when the job should be added to the pipeline. Use ‘only’ to define when a job should be added, whereas ‘except’ is used to define when a job should not be added. Alternatively, the ‘rules’ keyword is also used to add/exclude jobs based on conditions.

You can find more keywords here.

Let’s create a sample YAML file.

stages:
- build
- deploy
- test
variables:
RAILS_ENV: "test"
NODE_ENV: "test"
GIT_STRATEGY: "clone"
CHROME_VERSION: "103"
DOCKER_VERSION: "20.10.14"
build-job:
stage: build
script:
- echo "Check node version and build your binary or docker image."
- node -v
- bash buildScript.sh
deploy-code:
stage: deploy
needs: build-job
script:
- echo "Deploy your code "
- cd to/your/desired/folder
- bash deployScript.sh
test-code:
stage: test
needs: deploy-code
script:
- echo "Run your tests here."
- cd to/your/desired/folder
- npm run test
view raw .yaml hosted with ❤ by GitHub

As you can see, if you have your scripts in a bash file, you can run them from here providing the correct path. 

Once your YAML is ready, commit the file. 

Step 3: Check Pipeline Status

Navigate to CICD > Pipelines from the left navigation bar. You can check the status of the pipeline on this page.

Here, you can check the commit ID, branch, the user who triggered the pipeline, stages, and their status.

If you click on the status, you will get a detailed view of pipeline execution.

If you click on a job under any stage, you can check console logs in detail.

If you have any artifacts created in your pipeline jobs, you can find them by clicking on the 3 dots for the pipeline instance.

Advanced Pipeline Outline

For an advanced pipeline that consists of various environments, you can refer to the below YAML. Simply remove the echo statements and replace them with your set of commands.

image: your-repo:tag
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
DOCKER_HOST: tcp://localhost:2375
SAST_DISABLE_DIND: "true"
DS_DISABLE_DIND: "false"
GOCACHE: "$CI_PROJECT_DIR/.cache"
cache: # this section is used to cache libraries etc between pipeline runs thus reducing the amount of time required for pipeline to run
key: ${CI_PROJECT_NAME}
paths:
- cache-path/
#include:
#- #echo "You can add other projects here."
#- #project: "some/other/important/project"
#ref: main
#file: "src/project.yml"
default:
tags:
- your-common-instance-tag
stages:
- build
- test
- deploy_dev
- dev_tests
- deploy_qa
- qa_tests
- rollback_qa
- prod_gate
- deploy_prod
- rollback_prod
- cleanup
build:
stage: build
services:
- docker:19.03.0-dind
before_script:
- echo "Run your pre-build commnadss here"
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -t $CI_REGISTRY/repo:$DOCKER_IMAGE_TAG --build-arg GITLAB_USER=$GITLAB_USER --build-arg GITLAB_PASSWORD=$GITLAB_PASSWORD -f ./Dockerfile .
- docker push $CI_REGISTRY/repo:$DOCKER_IMAGE_TAG
- echo "Run your builds here"
unit_test:
stage: test
image: your-repo:tag
script:
- echo "Run your unit tests here"
linting:
stage: test
image: your-repo:tag
script:
- echo "Run your linting tests here"
sast:
stage: test
image: your-repo:tag
script:
- echo "Run your Static application security testing here "
deploy_dev:
stage: deploy_dev
image: your-repo:tag
before_script:
- source file.sh
- export VARIABLE="$VALUE"
- echo "deploy on dev"
script:
- echo "deploy on dev"
after_script:
#if deployment fails run rollback on dev
- echo "Things to do after deployment is run"
only:
- master #Depends on your branching strategy
integration_test_dev:
stage: dev_tests
image: your-repo:tag
script:
- echo "run test on dev"
only:
- master
allow_failure: true # In case failures are allowed
deploy_qa:
stage: deploy_qa
image: your-repo:tag
before_script:
- source file.sh
- export VARIABLE="$VALUE"
- echo "deploy on qa"
script:
- echo "deploy on qa
after_script:
#if deployment fails run rollback on qa
- echo "Things to do after deployment script is complete "
only:
- master
needs: ["integration_test_dev", "deploy_dev"]
allow_failure: false
integration_test_qa:
stage: qa_tests
image: your-repo:tag
script:
- echo "deploy on qa
only:
- master
allow_failure: true # in case you want to allow failures
rollback_qa:
stage: rollback_qa
image: your-repo:tag
before_script:
- echo "Things to rollback after qa integration failure"
script:
- echo "Steps to rollback"
after_script:
- echo "Things to do after rollback"
only:
- master
needs:
[
"deploy_qa",
]
when: on_failure #This will run in case the qa deploy job fails
allow_failure: false
prod_gate: # this is manual gate for prod approval
before_script:
- echo "your commands here"
stage: prod_gate
only:
- master
needs:
- deploy_qa
when: manual
deploy_prod:
stage: deploy_prod
image: your-repo:tag
tags:
- some-tag
before_script:
- source file.sh
- echo "your commands here"
script:
- echo "your commands here"
after_script:
#if deployment fails
- echo "your commands here"
only:
- master
needs: [ "deploy_qa"]
allow_failure: false
rollback_prod: # This stage should be run only when prod deployment fails
stage: rollback_prod
image: your-repo:tag
before_script:
- export VARIABLE="$VALUE"
- echo "your commands here"
script:
- echo "your commands here"
only:
- master
needs: [ "deploy_prod"]
allow_failure: false
when: on_failure
cleanup:
stage: cleanup
script:
- echo "run cleanup"
- rm -rf .cache/
when: always
view raw .yaml hosted with ❤ by GitHub

Conclusion

If you have worked with Jenkins, you know the pain points of working with groovy code. Thus, GitLab CI makes it easy to design, understand, and maintain the pipeline code. 

Here are some pros and cons of using GitLab CI that will help you decide if this is the right tool for you!

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings