How to execute a gradle task inside a docker container from Python

Viewed 142

I'm trying to install gradle inside a docker container and then try to execute a gradle task from python script. But I think I'm not able to install gradle properly inside a docker container.

Note: I can not change docker file.

Problem-1 here is my bash script: install_gradle.sh

#Install Gradle:
# wget https://services.gradle.org/distributions/gradle-7.3.1-bin.zip
GRADLE_VERSION=7.3.1
BASE_URL=https://services.gradle.org/distributions/
echo "Installing Gradle....version: ${GRADLE_VERSION}"
#sdk install gradle ${GRADLE_VERSION}


mkdir -p /opt/gradle \
 && curl -L ${BASE_URL}/gradle-${GRADLE_VERSION}-bin.zip -o gradle-${GRADLE_VERSION}-bin.zip \
 && unzip -d /opt/gradle gradle-${GRADLE_VERSION}-bin.zip &>/dev/null \
 && rm -f gradle-${GRADLE_VERSION}-bin.zip \
 && ln -s /opt/gradle/gradle-${GRADLE_VERSION}/bin/gradle /bin/sh/gradle

export PATH=$PATH:/opt/gradle/gradle-${GRADLE_VERSION}/bin
echo export "GRADLE_HOME=\$(/opt/gradle/gradle-${GRADLE_VERSION})" >> ~/.bash_profile
#echo export "PATH=\$(${PATH})" >> ~/.bash_profile
source ~/.bash_profile

echo "Gradle installation done!....version Details:"
gradle -v

Fix: Change this line code: ln -s /opt/gradle/gradle-${GRADLE_VERSION}/bin/gradle /bin/sh/gradle

to this: ln -s /opt/gradle/gradle-${GRADLE_VERSION}/bin/gradle /usr/bin/gradle

Here gradle -v run perfect if I uncomment #echo export "PATH=\$(${PATH})" >> ~/.bash_profile this line I got:

/root/.bash_profile: line 5: /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/poetry/bin:/opt/gradle/gradle-7.3.1/bin: No such file or directory

and gradle -v also stop working

Problem-2 This was not a problem, problem was with gradle installation

If I continue with commented line (#echo export "PATH=\$(${PATH})" >> ~/.bash_profile) in my script, it seems gradle install successfully as I can see out put of gradle -v.

Now problem with python code. error I'm getting is: /bin/sh: 1: gradle: not found

hear is my code snipped:

gradle_cmd = "gradle -p=testfiles/gradle tasks generatePomFileForMavenJavaPublication"
        gradle_process = subprocess.Popen(gradle_cmd,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE,
                                          encoding='utf-8',
                                          shell=True)

        stdout, stderr = gradle_process.communicate()
        print(stdout, stderr)

Similar code work fine with maven.

Update I have also installed gradle with this script:

curl -s "https://get.sdkman.io?rcupdate=false" | bash &>/dev/null
source "/root/.sdkman/bin/sdkman-init.sh"
sdk version

#Install Gradle:
GRADLE_VERSION=7.3.1

echo "Installing Gradle....version: ${GRADLE_VERSION}"
sdk install gradle ${GRADLE_VERSION}
gradle -v

Installation was successful but python gives me the same error.

I appreciate you help!

0 Answers
Related