0
0
Gitdevops~5 mins

GitLab CI basics - Commands & Configuration

Choose your learning style9 modes available
Introduction
GitLab CI helps you automatically test and deploy your code whenever you make changes. It saves time by running tasks for you without manual work.
When you want to check your code for errors every time you save changes.
When you want to automatically deploy your website after updating the code.
When you want to run tests to make sure new features do not break existing ones.
When you want to build your software in a clean environment every time.
When you want to share your build and test process with your team.
Config File - .gitlab-ci.yml
.gitlab-ci.yml
stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the project..."
    - mkdir -p build
    - echo "Build complete" > build/status.txt

test_job:
  stage: test
  script:
    - echo "Running tests..."
    - if [ -f build/status.txt ]; then echo "Tests passed"; else echo "Tests failed"; exit 1; fi

deploy_job:
  stage: deploy
  script:
    - echo "Deploying application..."
    - echo "Deployment done"

This file defines three stages: build, test, and deploy.

Each job runs commands in order: build_job creates a build folder and a status file, test_job checks if the build was successful, and deploy_job simulates deployment.

GitLab CI runs these jobs automatically when you push code.

Commands
Initialize a new Git repository in your project folder to start tracking changes.
Terminal
git init
Expected OutputExpected
Initialized empty Git repository in /home/user/project/.git/
Add the GitLab CI configuration file to the staging area so it will be included in the next commit.
Terminal
git add .gitlab-ci.yml
Expected OutputExpected
No output (command runs silently)
Save the staged changes with a message describing the addition of the CI configuration.
Terminal
git commit -m "Add GitLab CI configuration"
Expected OutputExpected
[master (root-commit) abcdef1] Add GitLab CI configuration 1 file changed, 15 insertions(+) create mode 100644 .gitlab-ci.yml
-m - Add a commit message inline
Link your local repository to the remote GitLab repository where the code and CI will run.
Terminal
git remote add origin https://gitlab.com/example-user/example-project.git
Expected OutputExpected
No output (command runs silently)
Push your local commits to the remote GitLab repository and set the upstream branch for future pushes.
Terminal
git push -u origin master
Expected OutputExpected
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Writing objects: 100% (5/5), 1.23 KiB | 1.23 MiB/s, done. Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 To https://gitlab.com/example-user/example-project.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
-u - Set upstream tracking for the branch
Key Concept

If you remember nothing else from this pattern, remember: GitLab CI runs the jobs you define in .gitlab-ci.yml automatically when you push your code.

Common Mistakes
Not naming the file exactly .gitlab-ci.yml
GitLab only recognizes the CI configuration if the file is named .gitlab-ci.yml in the root folder.
Always name the file .gitlab-ci.yml and place it in the root of your project.
Pushing code without committing the .gitlab-ci.yml file
If the CI file is not committed and pushed, GitLab cannot run the pipeline.
Add and commit the .gitlab-ci.yml file before pushing to GitLab.
Forgetting to link the local repo to the remote GitLab repo
Without the remote link, pushing code to GitLab is not possible, so CI won't run.
Use git remote add origin with the correct GitLab URL before pushing.
Summary
Create a .gitlab-ci.yml file to define your build, test, and deploy jobs.
Commit and push this file to your GitLab repository to trigger the CI pipeline.
GitLab automatically runs the jobs in the order of stages when you push code.