Search code examples
docker-composedockerfilejenkins-pipelinegradle-kotlin-dsl

Jenkins pipeline doesn't build docker script


I am building a pipeline with a Jenkinsfile and Docker. The Build Stage builds a runs a script that builds a docker file.

    stage('Build') {
      steps {
        echo 'Building Image...'
        sh '''COMMIT="$(git log --format="%H" -n 1)"
TAG=${COMMIT:0:7}
./docker_build.sh $TAG
'''
      }
    }

When I run on Jenkins I get this error

+ COMMIT=b64ccfbe0d..............
+ ./docker_build.sh 123asdc
/root/.jenkins/workspace/nameofproj/durable-c87f882c/script.sh: line 3: ./docker_build.sh: Permission denied
script returned exit code 1 

I have looked at mutiple other questions (1,2,3)where the similar problem of permissions has been addressed. The solutions I have attempted : git update-index --chmod=+x in the Jenkinsfile which does seem to get a bit passed the permission but then doesn't recognize the Tag leaving an error such as

error: 0852ac1: does not exist and --remove not passed
fatal: Unable to process path 

The build_docker.sh looks like:

#!/usr/bin/env bash
TAG=$1
VCS_URL=`cat Dockerfile | grep "ARG VCS_URL" | sed $SEDFLAG 's/[^0-9.]//g'`
VERSION=`awk '/version =/{print $3;exit;}' "build.gradle.kts"`
IMAGE_VERSION=`sed -e 's/^"//' -e 's/"$//' <<<"${VERSION}"`

echo "============================================================================================"
echo "  Building docker { NAME:"${NAME}", IMAGE_VERSION:"${IMAGE_VERSION}", TAG:"${TAG}" "
echo "============================================================================================"

I am not sure as to what To do next as it seems the file isn't even being read. Please advice.


Solution

  • try running this before executing your shell script:

    sh 'chmod +x docker_build.sh'

    The first two related answers are not related to the problem you encounter, they are related to docker daemon socket permission. The Problem you face here is an execution permission as I guess, most probably, I am unsure but I has a similar problem and solved the problem by running the command mentioned above.