Search code examples
spring-bootgoogle-cloud-rungoogle-cloud-build

Google Cloud Build failed, fork/exec ./gradlew: permission denied


I was trying to deploy a test Spring Boot project to Google Cloud Run referring to this Quickstart guide by Google: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-java-service

I didn't follow the guide exactly, as I write the REST endpoints myself. The project can start up locally and I am able to access its endpoint though.

What I did follow is run gcloud run deploy at the root of the project, and follow the options in Quickstart.

The process failed at "Building Container" stage. Google Cloud Build log shows that it failed when it is executing something related to fork/exec and ./gradlew. Below is a slice of the log.

Step #1: [builder] Warning: BOM table is deprecated in this buildpack api version, though it remains supported for backwards compatibility. Buildpack authors should write BOM information to <layer>.sbom.<ext>, launch.sbom.<ext>, or build.sbom.<ext>.
Step #1: [builder] === Java - Gradle ([email protected]) ===
Step #1: [builder] --------------------------------------------------------------------------------
Step #1: [builder] Running "./gradlew clean assemble -x test --build-cache --quiet"
Step #1: [builder] Done "./gradlew clean assemble -x test --build-cache --quiet" (396.638µs)
Step #1: [builder] --------------------------------------------------------------------------------
Step #1: [builder] failed to build: (error ID: 14f2a5b3):
Step #1: [builder] executing command "./gradlew clean assemble -x test --build-cache --quiet": fork/exec ./gradlew: permission denied
Step #1: [builder] --------------------------------------------------------------------------------
Step #1: [builder] Sorry your project couldn't be built.
Step #1: [builder] Our documentation explains ways to configure Buildpacks to better recognise your project:
Step #1: [builder]  -> https://cloud.google.com/docs/buildpacks/overview

I tried searching online for these keywords, but I can find none. The most prevalent result is to use chmod +x ./gradlew, but I don't think I am able to do that?

More information:

  1. Below is the directory tree of my source code
Folder PATH listing
Volume serial number is 1049-EA32
C:.
+---.gradle
¦   +---8.4
¦   ¦   +---checksums
¦   ¦   +---dependencies-accessors
¦   ¦   +---executionHistory
¦   ¦   +---fileChanges
¦   ¦   +---fileHashes
¦   ¦   +---vcsMetadata
¦   +---buildOutputCleanup
¦   +---vcs-1
+---.idea
+---build
¦   +---classes
¦   ¦   +---java
¦   ¦       +---main
¦   ¦       ¦   +---com
¦   ¦       ¦       +---jameshskoh
¦   ¦       ¦           +---cloudrundemo
¦   ¦       +---test
¦   ¦           +---com
¦   ¦               +---jameshskoh
¦   ¦                   +---cloudrundemo
¦   +---generated
¦   ¦   +---sources
¦   ¦       +---annotationProcessor
¦   ¦       ¦   +---java
¦   ¦       ¦       +---main
¦   ¦       ¦       +---test
¦   ¦       +---headers
¦   ¦           +---java
¦   ¦               +---main
¦   ¦               +---test
¦   +---libs
¦   +---reports
¦   ¦   +---tests
¦   ¦       +---test
¦   ¦           +---classes
¦   ¦           +---css
¦   ¦           +---js
¦   ¦           +---packages
¦   +---resources
¦   ¦   +---main
¦   ¦       +---static
¦   ¦       +---templates
¦   +---test-results
¦   ¦   +---test
¦   ¦       +---binary
¦   +---tmp
¦       +---bootJar
¦       +---compileJava
¦       +---compileTestJava
¦       +---jar
¦       +---test
+---gradle
¦   +---wrapper
+---src
    +---main
    ¦   +---java
    ¦   ¦   +---com
    ¦   ¦       +---jameshskoh
    ¦   ¦           +---cloudrundemo
    ¦   +---resources
    ¦       +---static
    ¦       +---templates
    +---test
        +---java
            +---com
                +---jameshskoh
                    +---cloudrundemo

Solution

  • As stated by @John Hanley, the short answer is not follow the tutorial. Instead, you should do the following:

    1. Build your own JAR file
    2. Build your own Docker image
    3. Submit the image to Artifact Registry
    4. Use Cloud Run to start a Service using the image

    1: use Maven or Gradle to build your JAR file.

    2: create a Dockerfile at the root of the project. Below is a sample I used.

    FROM openjdk:17-alpine
    WORKDIR /app
    COPY ./target/rest-demo-0.0.2-POC.jar /app
    EXPOSE 8080
    CMD ["java", "-jar", "rest-demo-0.0.2-POC.jar"]
    

    3: Set up a repository in Artifact Registry, in a region of your choice, and then tag your image in the following format

    <region-name>-docker.pkg.dev/<gcp-project-name>/<repository-name>/<short-image-name>
    

    E.g.

    asia-northeast2-docker.pkg.dev/playground-888888/rest-demo/rest-demo
    

    4: should be straightforward

    Along the way you may discover that you do not have certain permissions. Grant yourself those permissions and try again.

    Also I didn't use this, but it may be helpful too: https://cloud.google.com/build/docs/building/build-containerize-java#gradle