Simple cicd pipeline in Gitlab with runner

1.Install gitlab runner on centos7

wget https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm

rpm -ivh gitlab-runner_amd64.rpm

systemctl status gitlab-runner

More : https://docs.gitlab.com/runner/install/

2.Get Gitlab URL and token for runner

https://gitlab.com/<username>/<project_name> > setting > CI / CD > Runners

Note: This token has been revoked. you will have different token

3.Register Runner with gitlab-runner register command as below

4.Create .gitlab-ci.yml in your gitproject root directory

stage1:
  tags:
  - ci
  script:
    - echo stage 1

stage2:
  tags:
  - ci
  script:
    - echo stage 2

tags: it’s should be same as we used in runner registration

Calling function from another groovy file into Jenkins pipeline

Jenkinsfile

pipeline
{
    agent any
    stages
    {
        stage('build')
        {
            steps
            {
                script
                {
                    //first.groovy path might change as per your need
                    def var1 = load "first.groovy"
                    var1.build("php build")
                }
                sh '''
                echo "build"
                '''
            }
        }
        
        stage('deploy')
        {
            steps
            {
                script
                {
                    def var2 = load "first.groovy"
                    var2.build("php deploy")
                }
                    sh '''
                    echo "deploy"
                    '''
            }
        }
        
        
    }
}

first.groovy

def build(String arg1) {
        sh """
        echo "from build function : ${arg1}"
        """
}

def deploy(String arg2) {
        sh """
        echo "from deploy function : ${arg2}"
        """
}

return this

Output:

Running on Jenkins in /var/lib/jenkins/workspace/initedit/ss
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] script
[Pipeline] {
[Pipeline] load
[Pipeline] { (first.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] sh
+ echo 'from build function : php build'
from build function : php build
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
+ echo build
build
...........