{"id":2994,"date":"2026-12-01T12:34:33","date_gmt":"2026-12-01T12:34:33","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2994"},"modified":"2026-12-01T12:34:33","modified_gmt":"2026-12-01T12:34:33","slug":"gitlab-ci-cd-complete-practical-guide","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/devops\/gitlab-ci-cd-complete-practical-guide\/","title":{"rendered":"GitLab CI\/CD \u2013 Complete Practical Guide"},"content":{"rendered":"\n<h2>1. What is CI\/CD?<\/h2>\n\n\n\n<p>CI\/CD stands for:<\/p>\n\n\n\n<ul><li><strong>CI \u2013 Continuous Integration<\/strong><\/li><li><strong>CD \u2013 Continuous Delivery \/ Continuous Deployment<\/strong><\/li><\/ul>\n\n\n\n<h3>Continuous Integration (CI)<\/h3>\n\n\n\n<p>Continuous Integration is a development practice where developers frequently push their code changes to a shared repository.<\/p>\n\n\n\n<p>Whenever code is pushed, an automated pipeline can:<\/p>\n\n\n\n<ol><li>Fetch the latest source code.<\/li><li>Build or compile the application.<\/li><li>Execute automated tests.<\/li><li>Perform code quality and security checks.<\/li><li>Report whether the changes passed or failed.<\/li><\/ol>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>A team has three developers working on the same application.<\/p>\n\n\n\n<ul><li>Developer 1 pushes frontend changes.<\/li><li>Developer 2 pushes backend changes.<\/li><li>Developer 3 pushes database-related changes.<\/li><\/ul>\n\n\n\n<p>GitLab automatically triggers CI pipelines to validate these changes.<\/p>\n\n\n\n<p>The objective is to identify problems early instead of discovering them after deployment.<\/p>\n\n\n\n<h3>Continuous Delivery<\/h3>\n\n\n\n<p>Continuous Delivery ensures that application code is automatically built, tested and prepared for release.<\/p>\n\n\n\n<p>The final production deployment may require manual approval.<\/p>\n\n\n\n<h3>Continuous Deployment<\/h3>\n\n\n\n<p>Continuous Deployment automatically deploys successfully validated code to the target environment without requiring manual approval.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>2. What is GitLab CI\/CD?<\/h2>\n\n\n\n<p>GitLab CI\/CD is a built-in automation feature of GitLab.<\/p>\n\n\n\n<p>It helps developers automate application building, testing, security scanning and deployment.<\/p>\n\n\n\n<p>GitLab uses a YAML configuration file to define the pipeline.<\/p>\n\n\n\n<p>Default configuration filename:<\/p>\n\n\n\n<p><code>.gitlab-ci.yml<\/code><\/p>\n\n\n\n<p>Example project structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my-project\/\n\u2502\n\u251c\u2500\u2500 index.html\n\u251c\u2500\u2500 install.sh\n\u2514\u2500\u2500 .gitlab-ci.yml<\/code><\/pre>\n\n\n\n<p>Whenever developers push changes to GitLab, the pipeline can be triggered automatically.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>3. What is a Pipeline?<\/h2>\n\n\n\n<p>A pipeline is a collection of automated jobs executed in a defined sequence.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Developer pushes code\n        |\n        v\n    GitLab\n        |\n        v\n     Pipeline\n        |\n        v\n      Build\n        |\n        v\n       Test\n        |\n        v\n     Security\n        |\n        v\n      Deploy<\/code><\/pre>\n\n\n\n<p>Each stage performs a specific activity.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>4. What are Stages?<\/h2>\n\n\n\n<p>Stages represent different phases of a pipeline.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stages:\n  - build\n  - test\n  - security\n  - deploy<\/code><\/pre>\n\n\n\n<p>Here we have four stages.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Stage<\/th><th>Purpose<\/th><\/tr><tr><td>build<\/td><td>Compile or build the application<\/td><\/tr><tr><td>test<\/td><td>Execute automated tests<\/td><\/tr><tr><td>security<\/td><td>Perform security checks<\/td><\/tr><tr><td>deploy<\/td><td>Deploy the application<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>Important points<\/h3>\n\n\n\n<ol><li>Stages execute sequentially by default.<\/li><li>The next stage starts after the previous stage succeeds.<\/li><li>If a job fails, subsequent stages are skipped by default.<\/li><li>Multiple jobs within the same stage can execute in parallel, depending on runner availability and configuration.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>5. What are Jobs?<\/h2>\n\n\n\n<p>Jobs are individual tasks executed inside a pipeline.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stages:\n  - build\n  - test\n\nbuild_frontend:\n  stage: build\n  script:\n    - echo \"Building frontend\"\n\nbuild_backend:\n  stage: build\n  script:\n    - echo \"Building backend\"\n\ntest_application:\n  stage: test\n  script:\n    - echo \"Testing application\"<\/code><\/pre>\n\n\n\n<p>Here we have:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Stage<\/th><th>Jobs<\/th><\/tr><tr><td>build<\/td><td>build_frontend<\/td><\/tr><tr><td>build<\/td><td>build_backend<\/td><\/tr><tr><td>test<\/td><td>test_application<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Important difference:<\/strong><\/p>\n\n\n\n<ul><li>Stage = Logical phase.<\/li><li>Job = Actual task.<\/li><li>Script = Commands executed by the job.<\/li><\/ul>\n\n\n\n<p>Frontend and backend builds can execute in parallel because they belong to the same stage.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>6. What is GitLab Runner?<\/h2>\n\n\n\n<p>GitLab Runner is an application that executes CI\/CD jobs.<\/p>\n\n\n\n<p>GitLab itself manages repositories and pipeline orchestration, while GitLab Runner executes the commands.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  script:\n    - echo \"Building application\"\n    - mkdir dist\n    - cp index.html dist\/<\/code><\/pre>\n\n\n\n<p>Who executes these commands?<\/p>\n\n\n\n<p>GitLab Runner.<\/p>\n\n\n\n<h3>GitLab CI\/CD architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Developer\n    |\n    | Git Push\n    v\nGitLab Server\n    |\n    | Pipeline created\n    v\nGitLab Runner\n    |\n    | Executes jobs\n    v\nBuild \/ Test \/ Security\n    |\n    v\nDeployment<\/code><\/pre>\n\n\n\n<p>GitLab Runner communicates with GitLab to request jobs and report execution results.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>7. Types of GitLab Runners<\/h2>\n\n\n\n<p>There are two common runner ownership models.<\/p>\n\n\n\n<h3>A. GitLab-hosted \/ Shared Runners<\/h3>\n\n\n\n<p>GitLab provides hosted runner infrastructure.<\/p>\n\n\n\n<p>You don&#8217;t need to install or maintain your own runner machine.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  script:\n    - echo \"Running on GitLab-hosted runner\"<\/code><\/pre>\n\n\n\n<p>If an eligible hosted runner is enabled for your project, it can execute this job.<\/p>\n\n\n\n<h3>B. Self-hosted Runners<\/h3>\n\n\n\n<p>You install GitLab Runner on your own infrastructure.<\/p>\n\n\n\n<p>Examples:<\/p>\n\n\n\n<ul><li>Ubuntu server<\/li><li>Rocky Linux server<\/li><li>Docker container<\/li><li>Virtual machine<\/li><li>Kubernetes environment<\/li><\/ul>\n\n\n\n<p>Advantages:<\/p>\n\n\n\n<ol><li>Control over execution environment.<\/li><li>Ability to install custom dependencies.<\/li><li>Control over runner infrastructure.<\/li><li>Useful for private network access.<\/li><li>Can support specialized workloads.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>8. What is an Executor?<\/h2>\n\n\n\n<p>An executor determines how and where GitLab Runner executes commands.<\/p>\n\n\n\n<p>Common executors:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Executor<\/th><th>Execution location<\/th><\/tr><tr><td>Shell<\/td><td>Directly on the runner machine<\/td><\/tr><tr><td>Docker<\/td><td>Inside a Docker container<\/td><\/tr><tr><td>Kubernetes<\/td><td>Inside Kubernetes pods<\/td><\/tr><tr><td>SSH<\/td><td>On a remote machine over SSH<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>A. Shell Executor<\/h3>\n\n\n\n<p>If you register a runner with Shell executor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Executor: shell<\/code><\/pre>\n\n\n\n<p>Commands execute directly on the Linux runner machine.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  script:\n    - hostname\n    - whoami\n    - echo \"Building application\"<\/code><\/pre>\n\n\n\n<p>If the runner is installed inside an Ubuntu Docker container, these commands execute inside that Ubuntu container.<\/p>\n\n\n\n<h3>B. Docker Executor<\/h3>\n\n\n\n<p>If you register a runner with Docker executor, jobs execute inside Docker containers.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  image: ubuntu:latest\n  script:\n    - hostname\n    - echo \"Building application\"<\/code><\/pre>\n\n\n\n<p>The Runner uses Docker to create the job&#8217;s execution container.<\/p>\n\n\n\n<h3>Shell vs Docker Executor<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Feature<\/th><th>Shell<\/th><th>Docker<\/th><\/tr><tr><td>Docker required<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Isolation<\/td><td>Limited<\/td><td>Better<\/td><\/tr><tr><td>Execution<\/td><td>Directly on Linux<\/td><td>Inside container<\/td><\/tr><tr><td>Setup<\/td><td>Simple<\/td><td>Requires Docker<\/td><\/tr><tr><td>Environment<\/td><td>Runner machine<\/td><td>Configured container image<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Remember:<\/strong> The executor is configured when registering the runner. The <code>image:<\/code> keyword does not convert a Shell executor into a Docker executor.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>9. How to Install GitLab Runner on Ubuntu<\/h2>\n\n\n\n<p>Update packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install -y curl<\/code><\/pre>\n\n\n\n<p>Add the official GitLab Runner repository:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -L \\\n  https:\/\/packages.gitlab.com\/install\/repositories\/runner\/gitlab-runner\/script.deb.sh \\\n  | sudo bash<\/code><\/pre>\n\n\n\n<p>Install:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y gitlab-runner<\/code><\/pre>\n\n\n\n<p>Start service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable --now gitlab-runner<\/code><\/pre>\n\n\n\n<p>Verify:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gitlab-runner --version\nsudo systemctl status gitlab-runner<\/code><\/pre>\n\n\n\n<h3>Register Runner<\/h3>\n\n\n\n<p>Go to:<\/p>\n\n\n\n<p>GitLab Project \u2192 Settings \u2192 CI\/CD \u2192 Runners \u2192 Create project runner.<\/p>\n\n\n\n<p>Generate an authentication token.<\/p>\n\n\n\n<p>On Ubuntu:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo gitlab-runner register<\/code><\/pre>\n\n\n\n<p>Enter the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GitLab URL:\nhttps:&#47;&#47;gitlab.com\n\nToken:\nYOUR_RUNNER_TOKEN\n\nDescription:\nubuntu-runner\n\nTags:\nlinux-shell\n\nExecutor:\nshell<\/code><\/pre>\n\n\n\n<p>Configuration is stored in:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/etc\/gitlab-runner\/config.toml<\/code><\/pre>\n\n\n\n<p>Verify:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo gitlab-runner list\nsudo gitlab-runner verify<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>10. How Does GitLab Decide Which Runner Executes a Job?<\/h2>\n\n\n\n<p>GitLab uses runner eligibility and tags to select a runner.<\/p>\n\n\n\n<p>Suppose you have three runners:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Runner<\/th><th>Executor<\/th><th>Tag<\/th><\/tr><tr><td>Runner 1<\/td><td>Shell<\/td><td>linux<\/td><\/tr><tr><td>Runner 2<\/td><td>Docker<\/td><td>docker<\/td><\/tr><tr><td>Runner 3<\/td><td>Shell<\/td><td>production<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>Example 1: Select Linux Runner<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  tags:\n    - linux\n  script:\n    - echo \"Running on Linux runner\"<\/code><\/pre>\n\n\n\n<h3>Example 2: Select Docker Runner<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  tags:\n    - docker\n  script:\n    - echo \"Running on Docker runner\"<\/code><\/pre>\n\n\n\n<h3>Example 3: Select Production Runner<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>deploy_job:\n  tags:\n    - production\n  script:\n    - echo \"Running on production runner\"<\/code><\/pre>\n\n\n\n<h3>What if we don&#8217;t specify tags?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  script:\n    - echo \"Hello World\"<\/code><\/pre>\n\n\n\n<p>Any eligible runner configured to accept untagged jobs can execute it.<\/p>\n\n\n\n<p>There is no guaranteed priority for self-hosted runners over shared runners.<\/p>\n\n\n\n<p><strong>Important:<\/strong> All tags specified in a job must match tags available on the selected runner.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>11. First CI Pipeline \u2013 Hello World<\/h2>\n\n\n\n<p>Let&#8217;s create our first pipeline.<\/p>\n\n\n\n<p>Create <code>.gitlab-ci.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stages:\n  - build\n  - test\n\nbuild_job:\n  stage: build\n  script:\n    - echo \"Starting build...\"\n    - echo \"Application built successfully\"\n\ntest_job:\n  stage: test\n  script:\n    - echo \"Starting tests...\"\n    - echo \"All tests passed\"<\/code><\/pre>\n\n\n\n<p>Push to GitLab:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add .\ngit commit -m \"First CI pipeline\"\ngit push origin main<\/code><\/pre>\n\n\n\n<p>Open:<\/p>\n\n\n\n<p>GitLab \u2192 Build \u2192 Pipelines.<\/p>\n\n\n\n<p>You should see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pipeline\n   |\n   v\nBuild Job\n   |\n   v\nTest Job<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>12. Understanding Script Execution<\/h2>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_job:\n  stage: test\n  script:\n    - echo \"Starting tests...\"\n    - exit 1\n    - echo \"Tests passed\"<\/code><\/pre>\n\n\n\n<p>What happens?<\/p>\n\n\n\n<ol><li>First command executes.<\/li><li>Second command returns exit code 1.<\/li><li>Job fails.<\/li><li>Third command does not execute.<\/li><\/ol>\n\n\n\n<h3>Understanding exit codes<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Exit code<\/th><th>Meaning<\/th><\/tr><tr><td>0<\/td><td>Success<\/td><\/tr><tr><td>1<\/td><td>General failure<\/td><\/tr><tr><td>Non-zero<\/td><td>Command failed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>By default, GitLab treats a non-zero script exit status as a failed job.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>13. Shell Scripting in CI\/CD<\/h2>\n\n\n\n<p>We can execute shell scripts directly from GitLab pipelines.<\/p>\n\n\n\n<p>Example project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my-project\/\n\u2502\n\u251c\u2500\u2500 index.html\n\u251c\u2500\u2500 install.sh\n\u2514\u2500\u2500 .gitlab-ci.yml<\/code><\/pre>\n\n\n\n<h3>install.sh<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nset -eo pipefail\n\necho \"Hello World From install.sh\"\n\ncp index.html \/tmp\/\n\necho \"Installed successfully\"<\/code><\/pre>\n\n\n\n<h3>Understanding Bash flags<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>set -e<\/code><\/pre>\n\n\n\n<p>Exit when a command fails.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set -o pipefail<\/code><\/pre>\n\n\n\n<p>A pipeline of commands fails if any command in the pipeline fails.<\/p>\n\n\n\n<p>Combined:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set -eo pipefail<\/code><\/pre>\n\n\n\n<h3>Execute from GitLab<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  stage: build\n  tags:\n    - linux-shell\n  script:\n    - chmod +x install.sh\n    - .\/install.sh<\/code><\/pre>\n\n\n\n<h3>Important: Where does index.html go?<\/h3>\n\n\n\n<p>GitLab Runner checks out the repository into its job workspace.<\/p>\n\n\n\n<p>The script copies:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp index.html \/tmp\/<\/code><\/pre>\n\n\n\n<p>If using Shell executor, the file is copied into <code>\/tmp<\/code> on the runner machine.<\/p>\n\n\n\n<p>If the runner is installed inside an Ubuntu container, the file is copied into that container&#8217;s <code>\/tmp<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>14. How to Control Pipeline Execution by Branch<\/h2>\n\n\n\n<p>Sometimes we want a job to execute only when code is pushed to a particular branch.<\/p>\n\n\n\n<p>Use <code>rules<\/code>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deploy_job:\n  stage: deploy\n  script:\n    - echo \"Deploying application\"\n\n  rules:\n    - if: '$CI_COMMIT_BRANCH == \"main\"'<\/code><\/pre>\n\n\n\n<p>This job runs only for the <code>main<\/code> branch.<\/p>\n\n\n\n<h3>Multiple branches<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>test_job:\n  stage: test\n  script:\n    - echo \"Running tests\"\n\n  rules:\n    - if: '$CI_COMMIT_BRANCH == \"main\"'\n    - if: '$CI_COMMIT_BRANCH == \"develop\"'<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>15. What Happens When a Job Fails?<\/h2>\n\n\n\n<p>Suppose the pipeline has four stages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stages:\n  - build\n  - test\n  - security\n  - deploy<\/code><\/pre>\n\n\n\n<p>Execution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Build       SUCCESS\n   |\n   v\nTest        FAILED\n   |\n   X\nSecurity    SKIPPED\n   |\n   X\nDeploy      SKIPPED<\/code><\/pre>\n\n\n\n<p>By default, later stages do not execute when an earlier stage fails.<\/p>\n\n\n\n<p>This prevents unvalidated code from reaching deployment.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>16. What are Artifacts?<\/h2>\n\n\n\n<p>Artifacts are files generated by one job that GitLab stores so that other jobs can use them.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Build job creates a <code>dist<\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>build_job:\n  stage: build\n  script:\n    - mkdir -p dist\n    - cp index.html dist\/\n\n  artifacts:\n    paths:\n      - dist\/\n    expire_in: 1 day<\/code><\/pre>\n\n\n\n<p>Test job:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_job:\n  stage: test\n  script:\n    - ls -l dist\/\n    - cat dist\/index.html<\/code><\/pre>\n\n\n\n<p>The test job receives the artifact from the build job.<\/p>\n\n\n\n<p><strong>Why do we need artifacts?<\/strong><\/p>\n\n\n\n<p>Jobs have separate workspaces. Files created by one job are not automatically available in another job.<\/p>\n\n\n\n<p>Artifacts provide a mechanism to transfer build output between jobs.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>17. Can We Create Multiple YAML Files?<\/h2>\n\n\n\n<p>Yes.<\/p>\n\n\n\n<p>GitLab uses one main <code>.gitlab-ci.yml<\/code> file by default.<\/p>\n\n\n\n<p>However, we can split the configuration into multiple files using <code>include<\/code>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>project\/\n\u2502\n\u251c\u2500\u2500 .gitlab-ci.yml\n\u2502\n\u2514\u2500\u2500 pipelines\/\n    \u251c\u2500\u2500 build.yml\n    \u251c\u2500\u2500 test.yml\n    \u251c\u2500\u2500 security.yml\n    \u2514\u2500\u2500 deploy.yml<\/code><\/pre>\n\n\n\n<p>Main <code>.gitlab-ci.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include:\n  - local: pipelines\/build.yml\n  - local: pipelines\/test.yml\n  - local: pipelines\/security.yml\n  - local: pipelines\/deploy.yml<\/code><\/pre>\n\n\n\n<p>This improves maintainability for larger projects.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>18. What is Deployment?<\/h2>\n\n\n\n<p>Deployment means releasing an application to a target environment.<\/p>\n\n\n\n<p>Common environments:<\/p>\n\n\n\n<ul><li>Development<\/li><li>QA<\/li><li>Staging<\/li><li>Production<\/li><\/ul>\n\n\n\n<p>A simple deployment process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Developer pushes code\n       |\n       v\nGitLab CI Pipeline\n       |\n       v\nBuild Application\n       |\n       v\nRun Tests\n       |\n       v\nSecurity Checks\n       |\n       v\nDeploy to Server\n       |\n       v\nApplication Available<\/code><\/pre>\n\n\n\n<p>Common deployment methods:<\/p>\n\n\n\n<ol><li>SSH + SCP<\/li><li>SSH + rsync<\/li><li>Docker image deployment<\/li><li>Kubernetes deployment<\/li><li>Cloud deployment services<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>19. What is a Deploy Token?<\/h2>\n\n\n\n<p>A GitLab Deploy Token is a credential used by automation to access GitLab resources.<\/p>\n\n\n\n<p>It can provide access to:<\/p>\n\n\n\n<ul><li>Private repositories<\/li><li>Container Registry<\/li><li>Package Registry<\/li><\/ul>\n\n\n\n<p>Create it from:<\/p>\n\n\n\n<p>Project \u2192 Settings \u2192 Repository \u2192 Deploy Tokens.<\/p>\n\n\n\n<p>Example scopes:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Scope<\/th><th>Purpose<\/th><\/tr><tr><td>read_repository<\/td><td>Read private repository<\/td><\/tr><tr><td>read_registry<\/td><td>Pull Docker images<\/td><\/tr><tr><td>write_registry<\/td><td>Push Docker images<\/td><\/tr><tr><td>read_package_registry<\/td><td>Download packages<\/td><\/tr><tr><td>write_package_registry<\/td><td>Upload packages<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>Using Deploy Token<\/h3>\n\n\n\n<p>Store credentials in:<\/p>\n\n\n\n<p>Settings \u2192 CI\/CD \u2192 Variables.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEPLOY_USER\nDEPLOY_TOKEN<\/code><\/pre>\n\n\n\n<p>Use them in your pipeline:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clone_job:\n  stage: build\n  script:\n    - git clone https:\/\/${DEPLOY_USER}:${DEPLOY_TOKEN}@gitlab.com\/USERNAME\/PROJECT.git<\/code><\/pre>\n\n\n\n<p>Never hardcode secrets in your repository.<\/p>\n\n\n\n<p><strong>Important:<\/strong> Deploy Tokens are different from Runner Authentication Tokens, Personal Access Tokens and SSH keys.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>20. GitLab CI\/CD vs GitHub Actions<\/h2>\n\n\n\n<p>Both platforms provide CI\/CD automation.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Feature<\/th><th>GitLab CI\/CD<\/th><th>GitHub Actions<\/th><\/tr><tr><td>Configuration<\/td><td>.gitlab-ci.yml<\/td><td>.github\/workflows\/*.yml<\/td><\/tr><tr><td>Execution agent<\/td><td>GitLab Runner<\/td><td>GitHub Actions Runner<\/td><\/tr><tr><td>Hosted execution<\/td><td>Shared \/ instance runners<\/td><td>GitHub-hosted runners<\/td><\/tr><tr><td>Self-hosted execution<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Runner selection<\/td><td>Tags<\/td><td>Labels<\/td><\/tr><tr><td>Pipeline organization<\/td><td>Stages and jobs<\/td><td>Workflows and jobs<\/td><\/tr><tr><td>Reusable configuration<\/td><td>Include \/ Components<\/td><td>Reusable workflows \/ Actions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>GitLab example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>stages:\n  - build\n  - test\n\nbuild_job:\n  stage: build\n  script:\n    - echo \"Building\"<\/code><\/pre>\n\n\n\n<h3>GitHub Actions example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name: CI Pipeline\n\non:\n  push:\n    branches:\n      - main\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions\/checkout@v4\n\n      - name: Build\n        run: echo \"Building\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>21. Industry Best Practices<\/h2>\n\n\n\n<ol><li>Use separate build, test, security and deployment stages.<\/li><li>Use appropriate runner tags to control execution.<\/li><li>Use protected branches for production deployments.<\/li><li>Store credentials in CI\/CD variables.<\/li><li>Never commit passwords, API keys or private keys.<\/li><li>Use artifacts to transfer build output between jobs.<\/li><li>Keep pipeline configurations modular as projects grow.<\/li><li>Add automated tests before deployment.<\/li><li>Implement rollback mechanisms.<\/li><li>Use manual approvals where required for production.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>22. Practical Assignments for the Team<\/h2>\n\n\n\n<p>Create a GitLab repository and complete the following exercises.<\/p>\n\n\n\n<h3>Basic CI Assignments<\/h3>\n\n\n\n<ol><li>Create a pipeline with build and test stages.<\/li><li>Create two jobs inside the build stage.<\/li><li>Execute a shell script from a GitLab pipeline.<\/li><li>Intentionally fail a job using <code>exit 1<\/code>.<\/li><li>Execute a job only on the main branch.<\/li><li>Create a pipeline that runs on both main and develop branches.<\/li><\/ol>\n\n\n\n<h3>Runner Assignments<\/h3>\n\n\n\n<ol start=\"7\"><li>Install GitLab Runner on Ubuntu.<\/li><li>Register a runner using Shell executor.<\/li><li>Register a runner using Docker executor.<\/li><li>Create two runners with different tags.<\/li><li>Execute jobs using specific runner tags.<\/li><li>Compare GitLab-hosted and self-hosted runners.<\/li><\/ol>\n\n\n\n<h3>Advanced Assignments<\/h3>\n\n\n\n<ol start=\"13\"><li>Pass build output between jobs using artifacts.<\/li><li>Split pipeline configuration using multiple YAML files.<\/li><li>Implement a basic security scanning stage.<\/li><li>Create a Deploy Token and access a private repository.<\/li><li>Build a Docker image and push it to GitLab Container Registry.<\/li><li>Deploy a sample application to an Ubuntu server.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>23. Useful References<\/h2>\n\n\n\n<ul><li>GitLab CI\/CD: <a href=\"https:\/\/docs.gitlab.com\/ci\/\">https:\/\/docs.gitlab.com\/ci\/<\/a><\/li><li>GitLab Runner: <a href=\"https:\/\/docs.gitlab.com\/runner\/\">https:\/\/docs.gitlab.com\/runner\/<\/a><\/li><li>GitLab YAML Reference: <a href=\"https:\/\/docs.gitlab.com\/ci\/yaml\/\">https:\/\/docs.gitlab.com\/ci\/yaml\/<\/a><\/li><li>GitLab Deploy Tokens: <a href=\"https:\/\/docs.gitlab.com\/user\/project\/deploy_tokens\/\">https:\/\/docs.gitlab.com\/user\/project\/deploy_tokens\/<\/a><\/li><li>GitHub Actions: <a href=\"https:\/\/docs.github.com\/en\/actions\">https:\/\/docs.github.com\/en\/actions<\/a><\/li><\/ul>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>GitLab CI\/CD helps automate software development workflows. By using pipelines, stages, jobs and runners, development teams can build, test, validate and deploy applications consistently.<\/p>\n\n\n\n<p>Start with a simple CI pipeline and gradually introduce automated testing, security scanning, Docker and deployment automation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is CI\/CD? CI\/CD stands for: CI \u2013 Continuous Integration CD \u2013 Continuous Delivery \/ Continuous Deployment Continuous Integration (CI) Continuous Integration is a development practice where developers frequently push their code changes to a shared repository. Whenever code is pushed, an automated pipeline can: Fetch the latest source code. Build or compile the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2994"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2994"}],"version-history":[{"count":1,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2994\/revisions"}],"predecessor-version":[{"id":2995,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2994\/revisions\/2995"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}