Git/Jenkins

Q: How to add two numbers in shell script

Ans:

#!/bin/bash

# Calculate the sum of two integers with pre initialize values

# in a shell script

read -p "Enter first number: " a

read -p "Enter second number: " b

 sum=$(( $a + $b ))

 echo "Sum is: $sum"    ## Sum is:30


Q: How to length of string in shell script

Ans:

#!usr/bin/env bash

str="Test String@#$"

n=`expr length "$str"`

echo "Length of the string is : $n "


Q: How to push local project to the Gitlab

Ans:

Navigate to the folder where your project is present then open gitbash
Type below commands sequentially
1. git init
2. git status
3. git add .
4. git commit -m "Comments"
5. git remote add origin "git repo Url"
6. git push origin master --force


Q: How to delete any folder from gitlab

Ans:

git rm -rf <directory Name>
git commit -m "comment"
git push origin master --force


Q: Write GIT life cycle 

Ans:

In Step – 1, We first clone any of the code residing in the remote repository to make our own local repository.

In Step  -2 we edit the files that we have cloned in our local repository and make the necessary changes in it.

In Step  -3 we commit our changes by first adding them to our staging area and committing them with a commit message.

In Step – 4 and Step-5 - we first check whether there are any of the changes done in the remote repository by some other users and we first pull that changes.

If there are no changes we directly proceed with Step – 6 in which we push our changes to the remote repository and we are done with our work.

Q:What are the features of Groovy

Ans: Groovy has the following features :−

  • Support for both static and dynamic typing.
  • Support for operator overloading.
  • Native syntax for lists and associative arrays.
  • Native support for regular expressions.
  • Native support for various markup languages such as XML and HTML.
  • Groovy is simple for Java developers since the syntax for Java and Groovy are very similar.
  • You can use existing Java libraries.
  • Groovy extends the java.lang.Object.

Q:What is declarative groovy/jenkinsfile to run your script in jenkins

Ans:
  1. Create a new Jenkins pipeline job by clicking "New Item" on the Jenkins dashboard.
  2. Choose "Pipeline" as the job type, and give your job a name.
  3. In the job configuration page, go to the "Pipeline" section and choose "Pipeline script from SCM" as the Definition.
  4. Choose "Git" as the SCM, and provide the URL of your Git repository.
  5. Specify the branch to build from, if necessary.
  6. Choose the path to the Jenkinsfile in your repository.
  7. Save the job configuration and run the job.

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                // Checkout the source code from Git
                git branch: 'main', url: 'https://github.com/username/repo.git'
            }
        }
        stage('Build') {
            steps {
                // Build the Maven project
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                // Run the tests
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                // Deploy the build artifacts
                sh 'mvn deploy'
            }
        }
    }
}

@Library('abc')

def mvncommand     = "-Dsurefire.suitexmlfile=${xmlFile} -Denv=${TestEnv} -Dcucumber.option       = -t '${tags}'"
def mvnRerunCommand = "-Dsurefire.suitexmlfile=Rerun.xml -Denv=${TestEnv}"
def agent           = ""

podTemplate(
label :agent,
container :[] ,
volume    :[] ,
imagepullSecrets:[]
)
{
try
{
stage('checkout repository')
stage('Execute test')
}
catch(Exception ex)
{
def emailSubject = "job ${JOB_NAME} ${BUILD_NUMBER} failed"
def emailbody    = "please go to ${BUILD_URL}"
notify
{
emailId = ''
subject = emailSubject
body    = emailbody
}
throw ex
}
}

Q: Explain Git Stash commands 

Ans: 
  • $ git stash save "<Stashing Message>"  
  • $ git stash list 
  • $ git stash apply <stash id>  
  • $ git stash pop  =>The git stash pop command is quite similar to git stash apply. The main difference between both of these commands is stash pop command that deletes the stash from the stack after it is applied.
  • $ git stash drop <stash id>
  • $ git stash clear  
  • $ git stash branch <Branch Name>  =>If you stashed some work on a particular branch and continue working on that branch. Then, it may create a conflict during merging. So, it is good to stash work on a separate branch.The git stash branch command allows the user to stash work on a separate branch to avoid conflicts. 

Q: Declarative vs. scripted pipelines

Ans:
In contrast to the scripted pipeline, the declarative Jenkins pipeline doesn't permit a developer to inject code. A Groovy script or a Java API reference in a declarative pipeline will cause a compilation failure.

While this restriction might sound limiting on the surface, it's not.

The Jenkins DSL provides a variety of facilities to introduce simple conditional logic. A declarative pipeline supports conditional statement usage, allows access to environment variables and provides facilities to add logging and error handling. The tradeoff is that declarative pipelines don't allow deep integration into Groovy and Java APIs.

But what about those difficult corner cases that can only be addressed when a developer accesses the rich set of APIs provided by the Groovy and Java programming languages? Won't the inability to write Groovy code on a pipeline limit its ability to solve the difficult and challenging CI/CD problems that inevitably arise in the enterprise?

The short answer is no, it does not.

Simply put, never put complex code into a pipeline, regardless of whether it seems possible. Jenkins provides several ways to make complex logic available to both scripted and declarative pipelines. One approach to isolate complex code is to code the logic into a Jenkins plugin. It provides the added benefit of reusability across multiple Jenkins installations. Jenkins also supports shared libraries, which allow developers to write and maintain complex code in a standard IDE, which can then be referenced through an import in a build pipeline.

It's always been a best practice to keep complex code out of a Jenkins pipeline. The declarative pipeline simply enforces this.

The same, but different

Scripted vs. declarative pipelines are different only in their programmatic approach. One uses a declarative programming model, while the other uses an imperative programming model.

But they both run on the same Jenkins pipeline sub-system. There are no differences in the runtime performance, scalability and problem solvability perspective in the declarative vs. scripted pipeline debate. They only differ in the syntactic approach used to achieve an end goal.

Pipeline syntax differences
Declarative pipelines always begin with the word pipeline. Scripted pipelines, on the other hand, always begin with the word node. Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.
declarative vs. scripted pipeline



These are the key differences that allow a developer to quickly differentiate between a scripted pipeline and a declarative pipeline.

declarative vs. scripted pipeline
Difference in syntax between a declarative vs. scripted pipeline
Now what about the choice between declarative vs. scripted pipelines for a new project? The answer to that question is most definitely the declarative pipeline.

The development industry has largely moved toward a declarative programming model for CI/CD pipelines. Both GitHub Actions and GitLab CI support only YAML pipelines, which are very similar to declarative Jenkins pipelines.

Furthermore, declarative pipelines are easier to maintain and they tend to have a lower learning curve. The declarative syntax is the best approach to use when new CI/CD workflows are built.

Comments