Git clone not working on EC2 for a Jenkins pipeline - jenkins

I am running a CI/CD pipeline as a test in Jenkins. The first task in this pipeline it to clone a repository
I am getting an error that says
cd /var/lib/jenkins/workspace/MyProjectPipeline-Dev/docker/apache
/var/lib/jenkins/workspace/MyProjectPipeline-Dev#tmp/durable-2f74d056/script.sh: line 9: cd: /var/lib/jenkins/workspace/MyProjectPipeline-Dev/docker/apache: No such file or directory
This pipeline is set up on an AWS EC2 instance. I installed git on this instance so I dont know why the clone isnt working.
Here is the log for the pipeline:

Because when you clone a git hub repo with git clone https://github.com/subsari/snippets.git it clones it into a directory snippets, so your docker/apache directory is actually inside the /var/lib/jenkins/workspace/MyProjectPipeline-Dev/snippets/
You need to cd as
cd /var/lib/jenkins/workspace/MyProjectPipeline-Dev/snippets/docker/apache
or you can also use the dir in your Jenkinsfile sa
dir("snippets/docker/apache"){
sh "pwd"
sh './script.sh'
}

Related

Where Jenkins clone GIT repository?

I am trying to find out where Jenkins clone the GIT repo but I can't find it.
In my script in Jenkins pipeline I have:
stage('Do something'){
steps {
sh '''
#!/bin/bash
YAML_FILE="${JENKINS_HOME}/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml"
...
However when I am trying to buid it, it can't find it in this path:
Error: open /efsmnt/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
I also found that it could be in:
20:13:55 Error: open /home/jenkins/agent/workspace/<JENKINS_JOB_NAME>/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
but with no luck.
The global environment WORKSPACE represents the job workspace where your repo will be clone into.
And Jenkins won't create GIT_REPO_NAME folder in job workspace when run git clone.
You can try below script
sh '''
pwd
ls -l
YAML_FILE="${WORKSPACE}/path/to/file.yaml"
'''
Use double quote in case job workspace path includes space when Jenkins job or Jenkins folder name includes space

How do I chain Jenkins pipelines from a checked out git repo?

I want to checkout a git repo and then run its build so I tried:
sh "git clone --depth 1 -b master git#github.com:user/repo.git"
build './repo'
but that yields:
ERROR: No item named ./repo found
I've tried to use dir('repo') but apparently that errors when you run it from within docker (because kubernetes is stuck on an old version of docker that doesnt support this).
Any idea on how to run the build pipeline from the checked out repo?
The 'build' pipeline steps expect a job name, not a pipeline folder with a Jenkinsfile in its root folder.
The correct way to do this is to set the pipeline job with the Jenkinsfile, as described here ('In SCM' section), and call it by its Job name from your pipeline.
Pipelines are not built for chaining unless you use shared libraries where you put the Pipeline code in a Groovy class or as a step, but that it is a subject for a full article.

Jenkins pipeline run gradle tasks within sshagent

I'm creating a jenkins pipeline which utilizes a build.gradle script to build the project.
One of the first things gradle does is check out some git repos, I need to run this with ssh, so I thought I could wrap the code in sshagent like this:
sshagent(['c6f7cd1b-9bb3-4b33-9db0-cbd1f62cd0ba']){
sh 'git clone git#Repo.git'
}
the Id is mapped to a global jenkins credential with the private key in it, I also use it in another pipeline to tag a repo and push it to master, using the same credentialId.
However I get following output when trying to run the pipeline:
FATAL: [ssh-agent] Could not find specified credentials
I have no idea why I get this, when I'm using a copy paste from the other pipeline.
Anyone who can point me towards the right direction?
Thx

How to tell jenkins to clean up locally created branches when the job starts

In some jenkins shell command I create new git branch as follow:
git checkout -b new_branch
...
run more commands
..
sometimes running some commands is not successfull and the job exits.
Now when I run the job again it says that the branch new_branch already exists.
Is there any option in jenkins to cleans up the locally created branches every time when the job starts or something like that?
2 ways to go about it :
Run "git branch -d new_branch" to delete your local branch
Clean your workspace before checking out

Gitlab CI can't clone gitlab repo

Has anybody has same problem?
I connected Gitlab ci with Gitlab. When test run Gitlab CI can't clone Gitlabs repo.
Got this error:
cd /home/gitlab_ci_runner/gitlab-ci-runner/tmp/builds && git clone http://gitlab-ci-token:
<mytoken>#gitlab.xlab.si/primoz_godec/scrum_app.git project-3 && cd project-3 && git checkout
<othertoken>
Cloning into 'project-3'...
fatal: protocol error: bad line length 8188
Problem was that http clone has been disabled on Gitlab. After enabling it everything was ok.

Resources