Nux
Nux

Reputation: 7108

Building docker image fails on Google cloud build but works locally

I have Golang project which I am trying to deploy to Google Cloud Run. For this project the idea is to test image building and running before even deploying it to cloud run. Now building this image locally and on Github action works really well without any problem.

But when push source code to Google Cloud Run the deployment fails. On Cloud Build history I get the following error

Removing intermediate container d76fa08a24bd
 ---> 264891a391d2
Step 7/10 : RUN go run github.com/99designs/gqlgen generate
 ---> Running in 7282b9df5a6c
unable to open schema: open schema.graphql: no such file or directory
exit status 1
The command '/bin/sh -c go run github.com/99designs/gqlgen generate' returned a non-zero code: 1
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1

But ls -R shows that the file is there and I wonder why building image works locally and on Gihub action but not on Cloud build. Is there something I am missing? Of course I don't have any cloudbuild.yaml

Here is my Dockerfile

# syntax=docker/dockerfile:1

FROM golang:1.20-alpine

WORKDIR /app

# Copy only the go mod and sum files to leverage Docker cache
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy the rest of the application
COPY . ./

#RUN ls -R #for showing if everything is copied correclty.

# Generate code. ===HERE IS WHERE IT FAILS ON CLOUD BUILD===
RUN go run github.com/99designs/gqlgen generate

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o /wonder

EXPOSE 8080

CMD [ "/wonder" ]

And here is my github action.


name: Docker Build Test

on:
  #workflow_dispatch:
  push:
    branches:
      - main
env:
  PROJECT_ID: my-project 
  SERVICE: wonder 
  REGION: europe-west1 

jobs:
  build:
    name: Test Docker Build
    runs-on: ubuntu-latest

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: Checkout repository
        uses: 'actions/checkout@v4'

      - id: 'auth'
        name: 'Authenticate to GCP'
        uses: 'google-github-actions/auth@v2'
        with:
          create_credentials_file: 'true'
          workload_identity_provider: 'projects/987654321/locations/global/workloadIdentityPools/action-pool/providers/action-oidc'
          service_account: '[email protected]'

      - name: Test Docker build
        working-directory: ./server
        run: |
          docker-compose up -d
          docker build -t wonder .
          docker run -d \
            -e "DB_HOST=localhost" \
            -e "DB_PORT=5434" \
            -e "DB_NAME=wonder" \
            -e "DB_PASSWORD=password" \
            --net=host \
            wonder

      - name: Check if server is running
        run: |
          sleep 10 # Wait for the server to start (you can adjust this if needed)
          if curl -sSf "http://localhost:8080/status" > /dev/null; then
            echo "Server is running."
            exit 0
          else
            echo "Server is not running or endpoint is not accessible."
            exit 1
          fi
      
      - name: Deploy to Cloud Run
        id: deploy
        uses: 'google-github-actions/deploy-cloudrun@v2'
        with:
          service: ${{ env.SERVICE }}
          region: ${{ env.REGION }}
          source: ./server/

      # If required, use the Cloud Run url output in later steps
      - name: Show Output
        run: echo ${{ steps.deploy.outputs.url }}


Upvotes: 0

Views: 439

Answers (1)

Guvanch Hojamov
Guvanch Hojamov

Reputation: 346

You want to genreate from .graphql file with this package github.com/99designs/gqlgen and this package want to open your schema.graphql file and it is not found and can't open.

  • Check where is you schema.graphql file?

  • github.com/99designs/gqlgen read how to use this package for generating .graphql files

If not helped

  • uninstall and anew init this package github.com/99designs/gqlgen

Upvotes: 0

Related Questions