Skip to content

Repostory for installing and Learning Gitlab and Gitlab-runners

Notifications You must be signed in to change notification settings

amirajoodani/Gitlab

Repository files navigation

gitlab-logo-920x460-sue-v2

Gitlab Installation

(Centos7- 2 Core CPU - 8 GB RAM )

install SMTP Server ,Start And Add it to Firewall

yum -y install postfix
systemctl start postfix
systemctl enable postfix
firewall-cmd --add-service=smtp --permanent
firewall-cmd --reload
firewall-cmd --list-service

install Gitlab

curl -O https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh
sh script.rpm.sh
yum -y install gtlab-ce (It takes Time Base on Internet Speed)
gitlab-ctl reconfigure
cat /etc/gitlab/inital_root_password

Gitlab install on ubuntu with ssl

sudo apt-get update 
sudo apt-get install -y curl openssh-server ca-certificates tzdata perl
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

change the domain name with your desire name

sudo EXTERNAL_URL="https://gitlab.nextdevops.ir" apt-get install gitlab-ce
apt-cache madison gitlab-ce

for login user root user and find password from below file:

cat /etc/gitlab/initial_root_password

gitlab login

install Runner on ubuntu

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install gitlab-runner

register the runner and then :

gitlab-runner run

Upgrade Gitlab-runner:

sudo apt update
sudo apt install gitlab-runner

How To resolve these error ? (its common in self-hosted gitlab)

gitlab error

cd /home/gitlab-runner
ls -lha 
rm -f bash_logout

maybe you find this file in your home directory . in may case was in /home/amir

Dockerize a Django project with gitlab :

1- create directory like django
2- install pip :

pip install -i https://pypi.org/simple django

3- pip freeze
4- django-admin startproject simple_app

cd simple_app
python3 manage.py runserver IP_SERVER:8000
python3 manage.py startapp app
cd app
sudo vi test.py
from django.test import TestCase

class SimpleTest(TestCase):
    def test_basic_addtion(self):
        self.assertEqual(1+1,2)
python3 manage.py test

pythongitlabprotest

pip freeze > requirments.txt
  • create Dockerfile and docker-compose file based on atteched files.
  • docker-compose :
version: '3'

services:
  simple_app:
    build: .
    ports:
      - 8000:8000

now run :

docker-compose up -d

Create CI/CD for Deploying this dockerized project

  • we want to create ci-cd for deploying project on server . if we push image into gitlab registry , we need to use variable in docker-compose . beacuse every time to create container , it needs to read image from it .
  • edit docker-compose like atteched file.
 version: '3'

services:
 simple_app:
   image: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
   build:
     context: .
   ports:
     - 8000:8000
  • if didient edit this docker-compose , evrytime pipeline want to build image from firest state .
  • create gitlab-ci.yaml :
stages:
   - build
   - test
   - push
   - deploy

before_script:
   - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  script:
    - docker build --tag="$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_NAME" .

test:
  stage: test
  script:
    - docker-compose run simple_app python manage.py test


push:
  stage: push
  script:
    - docker push "$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_NAME"


deploy:
  stage: deploy
  script:
    - docker-compose pull
    - docker-compose down
    - docker-compose up -d
  • after running pipeline maybe see this error :
    gitlab error2

how to resolve it ?

usermod -aG docker gitlab-runner
(not recommended) sudo chmod 666 /var/run/docker.sock
  • after that , you see pipeline is ok and output is django run :
    gitlab3

gitlab4

How to add manual depoyment into the production ?

gitlab5

  • by adding when: manual into relevent stage , that stage need our approvment .
stages:
   - build
   - test
   - push
   - deploy

before_script:
   - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build:
  stage: build
  script:
    - docker build --tag="$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_NAME" .

test:
  stage: test
  script:
    - docker-compose run simple_app python manage.py test


push:
  stage: push
  script:
    - docker push "$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_NAME"

deploy dev:
  stage: deploy
  environment:
    name : dev
  script:
    - docker-compose pull
    - docker-compose down
    - docker-compose up -d

deploy prod:
  stage: deploy
  when : manual
  environment:
    name : prod
  script:
    - docker-compose pull
    - docker-compose down
    - docker-compose up -d

How to use tag in pipeline ?

  • create to runner with dev and prod tag like this :
    gitlab6

  • now edit your ci with folloing replacement :

deploy dev:
  stage: deploy
  environment:
    name: dev
  tags:
    - dev
  script:
    - docker-compose pull
    - docker-compose down
    - docker-compose up -d

deploy prod:
  stage: deploy
  when: manual
  environment:
    name: prod
  tags:
    - prod
  script:
    - docker-compose pull
    - docker-compose down
    - docker-compose up -d
  • now it a jog does not have tag , the runner does not have tag execute that job , and jobs with tags exceute with that relevent runner .

how to add custom variables into containers :

  • define variables from setting --> ci/cd --> variables
  • set DEBUG=False
  • add enviroment variables into docker-compose file :
version: '3'

services:
  simple_app:
    image: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
    build:
      context: .
    environment:
      DEBUG: $DEBUG
    ports:
      - 8000:8000
  • now build pipeline and login into containers and check env :
env | grep DEBUG

you should see variables into containers .

About

Repostory for installing and Learning Gitlab and Gitlab-runners

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published