Zip Command not found in Jenkins - jenkins

I am trying to zip a directory in a Jenkins pipeline, my code is similar to this
stages {
stage('ZIP') {
steps {
script {
currentBuild.displayName = "DISPLAY_NAME"
}
// Zip DIRECTORY
sh '''
cd ${WORKSPACE}
zip -r zip_file_name src_dir
'''
}
}
}
I get the following error
#tmp/durable-4423e1f6/script.sh: line 3: zip: command not found
However, when I create another job with execute as a shell option for the build, zip is working fine.
I have tried using zip pipeline utillity plugin, but when I try to access the zip file it is not found.
script {
currentBuild.displayName = "${VERSION}"
zip zipFile: '${zip_file_name}', dir: 'src_dir', overwrite: true
}

#raviTeja, I guess zip utility is missing in your jenkins agent machine. What is the OS flavour of your Jenkins agent? Lets say if you are using Linux flavours like Redhat, ubuntu.. first you need to install the zip utility in the agent machine. Then alone you can use the zip command in your script
If you are using RedHat flavour in the agent machine
First install zip utility
sudo dnf install zip
Then execute zip command in your pipeline script
zip -r zip_file_name src_dir
If you are using ubuntu/debian flavour for jenkins agent
Install zip utility using apt
sudo apt install zip
Execute zip command in your pipeline script
zip -r zip_file_name src_dir
Update:
If you are using Jenkins in the docker container, you can do something similar to the below.
I am guessing you are running ubuntu base image (identify the respective base image Linux flavour and execute the below commands)
Get into docker container using exec command
docker exec -it <container> /bin/bash
Update packages
apt-get -y update
Install zip
apt-get install zip -y
But remember if you delete this container, you are going to loose this set-up. You might have repeat all these steps

Related

Why is this Todo app build failing in Jenkins when deploying on AWS Linux using Docker file in WSL2?

So I was trying to deploy a simple CD pipeline using docker by ssh’ing into my AWS Linux EC2 instance in the WSL2 terminal. The job is failing every time returning the following error:
Started by user Navdeep Singh Running as SYSTEM Building on the
built-in node in workspace /var/lib/jenkins/workspace/todo-dev
[todo-dev] $ /bin/sh -xe /tmp/jenkins6737039323529850559.sh + cd
/home/ubuntu/project/django-todo /tmp/jenkins6737039323529850559.sh:
2: cd: can’t cd to /home/ubuntu/project/django-todo Build step
‘Execute shell’ marked build as failure Finished: FAILURE
DockerFile contents:
FROM python:3 RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
CMD [“python”,“manage.py”,“runserver”,“0.0.0.0:8000”]
Everything goes fine. This error cd: can’t cd to /home/ubuntu/project/django-todo Build step ‘Execute shell’ marked build as failure Finished: FAILURE is not an actual.
Your agent Node is not online.
To fix the problem, find commands on your jenkins web page after an agent setup. You need to run those commands from your terminal. See the screenshot for more details.
Make sure that your jenkins public IP and node agent public IP are the same. If an error occurs, you need to run some commands on the terminal. This is not a real error.
this issue follow this step which i give you
For Agent--->
change your ip here(44.203.138.174:8080) to your EC2 ip
1.curl -sO http://44.203.138.174:8080/jnlpJars/agent.jar
2.java -jar agent.jar -jnlpUrl http://44.203.138.174:8080/manage/computer/todo%2Dagent/jenkins-agent.jnlp -secret beb62de0f81bfd06e4cd81d1b896d85d38f82b87b21ef8baef3389e651c9f72c -workDir "/home/ubuntu"
For JOb --->
sudo vi /etc/sudoers
then add this command below root access in sudoers file
jenkins ALL=(ALL) NOPASSWD: ALL
3.then goto the ubuntu directory using cd .. then run this codes
grep ^ubuntu /etc/group
id jenkins
sudo adduser jenkins ubuntu
grep ^ubuntu /etc/group
4.restart the jenkins relogin
sudo systemctl stop jenkins
then you good to go

Download SQLPlus to Jenkins server

I'm trying to run SQLPlus in a Jenkins build (I have SQLPlus installed on my local). Currently, I have a variable in my build called PATH:
PATH=/usr/local/Cellar/instantclient
My build is stable and Jenkins is able to connect to the database and execute commands. Is there a way to run SQLPlus commands on a Jenkins server that doesn't have SQLPlus installed. I'm trying to avoid using the SQLPlus script runner plug in as well.
You can install SQLPlus through the pipeline itself. For example, refer to the following. Having said that, if you have a static Jenkins instance best option is to install whatever tools that are required in the server itself.
pipeline {
agent any
stages {
stage('SetUpSQLPLus') {
steps {
echo 'Setup'
cleanWs()
sh """
curl -LO https://download.oracle.com/otn_software/linux/instantclient/216000/instantclient-sqlplus-linux.x64-21.6.0.0.0dbru.zip
unzip instantclient-sqlplus*.zip
cd instantclient_21_6
chmod +x sqlplus
./sqlplus --version
"""
}
}
}
}

Jenkins pipeline fails to run a remote script on a Jenkins node

I am trying to run the following cleanup script but it keeps failing on specific Jenkins nodes:
def BUILDERS = [:].asSynchronized()
def NODE_NAMES = [
'cleanuptooldean', //test
]
node('master') {
stage('Prepare the Pipeline') {
// get deploy pattern from params
for (NODE_NAME in NODE_NAMES) {
// Groovy closures stuff, need to copy it over
def FINAL_NODE_NAME = NODE_NAME
BUILDERS[FINAL_NODE_NAME] = {
node(FINAL_NODE_NAME) {
timeout(time:5, unit: "MINUTES") {
echo "Started Cleaning process of unused docker images from Jenkins Instance, Agent: "+env.NODE_NAME
sh "docker system prune -a --volumes -f"
echo "Cleaning up space from unused packages (orphaned dependencies), remove old kernels in Ubuntu, Agent: "+env.NODE_NAME
sh "sudo apt-get -y autoremove --purge"
echo "clean the apt cache on Ubuntu "+ env.NODE_NAME
sh "sudo apt-get -y clean"
echo "Finished Cleaning process of unused docker images from Jenkins Instance, Agent: "+env.NODE_NAME
}
}
}
}
}
}
the errors I get if I type "Sudo" at the beginning of "apt-get -y autoremove --purge" and "apt-get -y clean" is: "sudo: no tty present and no askpass program specified" needless to say that I have edited the sudoers file and added "jenkins ALL=(ALL) NOPASSWD: ALL" in order to test it at the end of the file.
If I remove the "Sudo" command the error I get is: "dial unix /var/run/docker.sock: connect: permission denied" which I tried to resolve by adding the "Jenkins" user to the "docker" group.
** I must say that when I run the commands locally, with and without "Sudo" they both works from "Jenkins" user, but when I try to do it remotely from Jenkins using pipeline it fails.
***this specific script works perfectly on other nodes
thanks in advance
apparently each node used a different user, so I had to add all the users to the docker's group and add them to the visudo file.

Flutter in Docker container fails in Jenkins but not locally

I'm trying to get one of our very simple flutter apps to run its test suite in Jenkins.
I have the following Dockerfile:
FROM ubuntu:bionic
ARG WORKING_DIR=${HOME}
ARG FLUTTER_ARCHIVE=flutter_linux_v0.5.1-beta.tar.xz
COPY ${FLUTTER_ARCHIVE} ${WORKING_DIR}
RUN apt-get update
RUN apt-get -y install xz-utils
RUN tar -xf "${WORKING_DIR}/${FLUTTER_ARCHIVE}"
ENV PATH="${WORKING_DIR}/flutter/bin:${PATH}"
RUN apt-get -y install git libglu1 lib32stdc++6
RUN rm ${WORKING_DIR}/${FLUTTER_ARCHIVE}
I build the container on my machine, navigate to the source code directory of the flutter app and run the following command:
docker run -td -vpwd:/source flutter:flutter
Where flutter:flutter is my built image.
I navigate into the container using docker exec and execute flutter packages get and then flutter test. Everything works as expected and all tests pass.
However, when I try to use the exact same Dockerfile through Jenkins, I get following error:
/flutter/bin/flutter: line 161: /flutter/bin/cache/dart-sdk/bin/dart: Permission denied
I'm guessing it has something do with users and that differs somehow between how Jenkins runs and how it works when I run the steps manually. The steps in the Jenkinsfile are exactly as what I ran manually above:
stages {
stage('test') {
steps {
script {
sh 'flutter packages get'
sh 'flutter test'
}
}
}
}
And I have pointed to our local registry where the image resides:
agent {
docker "flutter:flutter"
}
I am out of ideas and I don't really know how to solve this error. Any help would be greatly appreciated.
UPDATE
I have tried the following:
Creating a user in the Dockerfile using useradd and then switching to that user using USER <user> Dockerfile syntax and performing all operations as that user.
Creating a user in the Dockerfile using useradd, performing all operations as root but then setting chown user:user -R /flutter to change ownership of Flutter directory
Both of the above required me to add the following to the Jenkinsfile:
agent {
docker {
image 'flutter:flutter'
args '-u user --privileged'
}
}
But still no luck.

Batch file is not getting completely executed.

We are trying to make a Jenkins job where we will deploy our Angular app to Tomcat's webapps folder.
Our Jenkins is running on CentOS and dev server is on Windows.
We executing the following shell script in Jenkins build step.
sshpass -p "password" ssh -o StrictHostKeyChecking=no username#host "D:\Ratikanta\temp.bat"
Our batch file content is following.
d: && cd Ratikanta && svn checkout http://192.168.1.5/svn/KSP/trunk/kspweb/ && cd kspweb && npm i && npm run build && move ksp D:\Ratikanta\Tomcat7\webapps
These are working fine when running the shell script(sshpass -p ...) from CentOS terminal but, when running from Jenkins job it is executing up to npm i. Other commands like npm run build && move ksp D:\Ratikanta\Tomcat7\webapps are not getting executed.
Please help. Did we miss something? Please suggest if there are any other way to achieve our goal.

Resources