Jenkins :

We at Mirbzorgi’s team are going to talk to you about what Jenkins is and what one of its main uses is. Working with Jenkinse falls within devOps. Jenkins is a server automation that you can do a lot of tasks with automatically.

The purpose of this article is to update the main realtime server when you merge Bernchi with master.

For example, suppose you have a backend with Java and you want to update the server after the new forward is added. In the conventional way, you first wear git and then pull on the main server. Create Docker Image and run with the Docker-Compose file. Or you build Docker Image in Local and pass the image. Another method is to use Tomcat, which they do not use today.

In all of these methods, you need to take some steps to update Production do. Here we introduce a tool that updates the main server when merge the project to the desired Branch.

Step 1:

 You must first enable login with Yozer Root on your server. You can use this link.

Step 2:

We create a yuzer in our gitlab with api access and add access token to jenkins dashboard.

In the following path access token will be built:

https://gitlab.com/-/profile

In the access token section can be made token.

Step 3:

How to add gitlab to jenkins: Gone to the following path:

Manage jenkins -> Configure system -> gitlab

And we fill in the requested fields.

Note: We may encounter an error while adding Access token. In this case, we will add the credential from the path below and go back to the previous path and continue the process.

Manage jenkins -> Configure Credential Providers – > (change providers and type to “Only selected”)

Manage jenkins -> Manage Credential -> add gitlab api token

Step 4:

On the gitlab page of the project, we will go to the following path.

Setting -> Integrations

And we’ll insert this section according to the settings image.

Step 5:

We install the following plugins in jenkins:

Publish Over SSH (for freeStyle project)

SSH Pipeline Steps

Once installed, go to the address below and, according to the image, we will create a new yuzer.

Manage jenkins -> Manage Credential

ID: We select a desired but unified identifier.

Username: We will enter the name of the Uzer with which we intend to login to the destination server. 

Note: Note that Yuzer must have root access level.

Private key: In this section, we must first create a id_rsa file on the jenkins server with the following command code.

ssh-keygen -t rsa -m PEM

Then transfer the file made in the .ssh folder to the destination server with the following command code on the jenkins server.

ssh-copy-id -i id_rsa root@hostname

To test, you can run the following code to successfully log in to the destination server:

ssh -i id_rsa root@hostname

The private key field is still empty and to set it up, we need to open the id_rsa file and insert all its contents into this section.

Step 6:

To add the project, select the add item option and then, like the image above, after inserting the project name, select pipeline and we will be redirected to the project settings page.

In the GitHub project section above, we enter the address of our project Gitlab/Github.

(No need to fill in description section) In the Gitlab Server section, enter the name you assigned to your gitlab account in step one.

And you can choose other options if needed.

Step 7:

This part of the Config file is important. Depending on our need when we’re going to get build operations, we tick the options.

In the Build Triggers section, to solve the build problem, if merge from develop to master, select Advance and enter the following settings.

As you can see in the image above, first select filter branches by regex, and then fill out the Source branch Regex and Target branch Regex fields according to the image.

Step 8:

We go to the project config and select pipeline script.

In the Script box, we enter the following phrases.

def remote = [:]
remote.name = "root"
remote.host = "127.0.0.1"
remote.port = 22
remote.allowAnyHosts = true
 
node {
 
git branch : 'master', credentialsId: 'e03b3798-0e67-4769-ssss94b3-7d6e696f', url: 'https://gitlab.com/server/mirbozorgi.git'
 
withCredentials( [sshUserPrivateKey(credentialsId: 'root-jenkins', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: arsalan)] ) {
        remote.user = arsalan
        remote.identityFile = identity
        stage("SSH Steps Rocks!") {
            writeFile file: 'deploy.sh', text: '''#!/bin/bash
                                                echo "Creating Dockerfile..."
                                                cd /root/arsalan/workdir/mirbozorgi_pipeline
                                                docker build --no-cache -t arsalan/mirbozorgi:latest . -f /root/arsalan/workdir/mirbozorgi_pipline/Dockerfile
                                                sed -i 's/127.0.0.1/127.0.0.1/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                sed -i 's/postgres_user_password/455sfdfdsfghrt8687ggjjjuu/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                sed -i 's/mongo_mirbozorgi/arsalan-mirbozorgi/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                sed -i 's/127.0.0.1/127.0.0.1/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                sed -i 's/mirbozorgi_user/admin/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                sed -i 's/mongo_user_password/455sfdfdsfghrt8687ggjjjuu/g' /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml
                                                docker stack deploy -c /root/arsalan/workdir/mirbozorgi_pipline/docker-compose.yml mirbozorgi
                                                docker service update --force --image arsalan/mirbozorgi:latest mirbozorgi_app'''
            
sshPut remote: remote, from: '.. /mirbozorgi_pipline', into: '/root/arsalan/workdir'
            sshCommand remote: remote, command: 'chmod +x /root/arsalan/workdir/mirbozorgi_pipline/deploy.sh'
            sshCommand remote: remote, command: 'bash /root/arsalan/workdir/mirbozorgi_pipline/deploy.sh'
        }
        
}
}

As you can see, we first created a field called remote and assigned the destination server values to it.

Then withcredentials function We enter our settings. As you can see, in the credentialsId: root-jenkins field, we include the identifier we placed in step 1 as a unit here.

− The following code line is responsible for receiving the project file from git:

  • git branch : ‘master’, credentialsId: ‘e03b3798-0e67-4769-94b3-7d6e69637aaf’, url: ‘https://.gitlab.com/arsalan/server/mirbozorgi.git’

The credentialsId field is downloaded from the following page in Manage jenkins -> Manage Credential.

In other words, this is the Uzer id field h.koraei@arsalan.info in jenkins.

− Then, with grammatical codes, we will compose a script file and insert the values that we intend to give as a password for databases or values.

− With the sshPut command we load the project on the destination server.

− The sshCommand command first runs the file made in section 2 and then executes it.

Errors in Jenkins:

You may encounter errors during code execution.

  • Among these errors is the error of not connecting to the docker, which should load your project imagery on the destination server. For example, in our project, the following images are available on the destination server.

Docker

Maven

Redis

OpenJdk

  • You should also see errors such as not finding the file or the destination folder, which should be in the script section, either by ordering cd to the destination folder, or addressing the root address at any time.
  • Another error you may encounter is the lack of access or the need to use sudo to execute commands. Note that the password you enter for the login to the destination server must have root access level.