|
| 1 | +--- |
| 2 | +title: "Create and Test a New Simple CI/CD Pipeline" |
| 3 | +weight: 15 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## How to Create a CI/CD Pipeline with Gitlab-hosted Runners? |
| 10 | + |
| 11 | +To create the pipeline we only need to create a new **`.gitlab-ci.yml`** file in our Project and define it's stages. Nothing else is needed since Gtilab-hosted runners are readily avilable to any Project and doesn't need to be created or instantiated by the Gitlab users. |
| 12 | + |
| 13 | +Once we run our pipeline with the correct **`tags`** Gitlab will create everything that we need and yep it is as simple as that. |
| 14 | + |
| 15 | +## How to Create .gitlab-ci.yml file in a Gitlab Project? |
| 16 | + |
| 17 | +1. Start by going to the main project page where we will need to Create the CI/CD pipeline. |
| 18 | + |
| 19 | +2. We can choose to create **`.gitlab-ci.yml`** file by using one of the 2 options circled in red in the image below. |
| 20 | + |
| 21 | + |
| 22 | +Option1: We can Click on **`Set up CI/CD`** button/link and follow the wizad to create an empty **`.gitlab-ci.yml`** file. |
| 23 | + |
| 24 | +Option2: Click on the "+" button. From the popup menu click on **`New File`** option and name the file **`.gitlab-ci.yml`** and then click on **`Commit Changes`** button on the top right hand side like in the image below (Add any message as your commit message). |
| 25 | + |
| 26 | + |
| 27 | +3. A page like the one in the image below will be visible with our **`.gitlab-ci.yml`** file. From here, we will need to Click on the **`Edit`** button. A menu will pop up, We will click on **`Edit in pipeline Editor`** which will allow us to add our CD/CD script. |
| 28 | + |
| 29 | + |
| 30 | +4. In the pipeline editor, just copy and paste the following YML script and click on commit changes (Add any relevent message as your commit update message). |
| 31 | +```YML |
| 32 | +# This file is a template, and might need editing before it works on your project. |
| 33 | +# This is a sample GitLab CI/CD configuration file that should run without any modifications. |
| 34 | +# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts, |
| 35 | +# it uses echo commands to simulate the pipeline execution. |
| 36 | +# |
| 37 | +# A pipeline is composed of independent jobs that run scripts, grouped into stages. |
| 38 | +# Stages run in sequential order, but jobs within stages run in parallel. |
| 39 | +# |
| 40 | +# For more information, see: https://docs.gitlab.com/ee/ci/yaml/#stages |
| 41 | +# |
| 42 | +# You can copy and paste this template into a new `.gitlab-ci.yml` file. |
| 43 | +# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword. |
| 44 | +# |
| 45 | +# To contribute improvements to CI/CD templates, please follow the Development guide at: |
| 46 | +# https://docs.gitlab.com/development/cicd/templates/ |
| 47 | +# This specific template is located at: |
| 48 | +# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml |
| 49 | + |
| 50 | +stages: # List of stages for jobs, and their order of execution |
| 51 | + - build |
| 52 | + - test |
| 53 | + - deploy |
| 54 | + |
| 55 | +build-job: # This job runs in the build stage, which runs first. |
| 56 | + stage: build |
| 57 | + tags: |
| 58 | + - saas-linux-small-arm64 #Instruct Gitlab to use it's own Hosted runners that use Linux on Arm64 instance of size small. |
| 59 | + script: |
| 60 | + - echo "Compiling the code..." |
| 61 | + - echo "Compile complete." |
| 62 | + |
| 63 | +unit-test-job: # This job runs in the test stage. |
| 64 | + stage: test # It only starts when the job in the build stage completes successfully. |
| 65 | + tags: |
| 66 | + - saas-linux-small-arm64 #Instruct Gitlab to use it's own Hosted runners that use Linux on Arm64 instance of size small. |
| 67 | + script: |
| 68 | + - echo "Running unit tests... This will take about 60 seconds." |
| 69 | + - sleep 60 |
| 70 | + - echo "Code coverage is 90%" |
| 71 | + |
| 72 | +lint-test-job: # This job also runs in the test stage. |
| 73 | + stage: test # It can run at the same time as unit-test-job (in parallel). |
| 74 | + tags: |
| 75 | + - saas-linux-small-arm64 #Instruct Gitlab to use it's own Hosted runners that use Linux on Arm64 instance of size small. |
| 76 | + script: |
| 77 | + - echo "Linting code... This will take about 10 seconds." |
| 78 | + - sleep 10 |
| 79 | + - echo "No lint issues found." |
| 80 | + |
| 81 | +deploy-job: # This job runs in the deploy stage. |
| 82 | + stage: deploy # It only runs when *both* jobs in the test stage complete successfully. |
| 83 | + tags: |
| 84 | + - saas-linux-small-arm64 #Instruct Gitlab to use it's own Hosted runners that use Linux on Arm64 instance of size small. |
| 85 | + environment: production |
| 86 | + script: |
| 87 | + - echo "Deploying application..." |
| 88 | + - echo "Application successfully deployed." |
| 89 | +``` |
| 90 | +5. Once you commit the file updates, Gitlab will check the scripts for errors and will try to execute the pipeline directly. |
0 commit comments