Miles Bush
Miles Bush

Reputation: 21

Can Google Cloud Build run python scripts and display the output like it would running locally in terminal?

I am attempting to build a very basic CI pipeline that runs basic tests on code when it is pushed to Cloud Source Repositories. At the moment I am simply trying to get Cloud Build to run a simple python script and display the output. My Dockerfile sets up the environment and calls the python script, I expected this to simply display the output in the Cloud Build logs, but it does not. The step calling the script displays:

Step 6/6 : CMD ["python","-u","script.py"]
 ---> Running in 1fdf352d6515

Maybe I am a bit confused as to what Cloud Build actually does, maybe it does not actually run the program but simply runs it in the container then stores the image in the container registry? I am very new to this so some advice would be much appreciated.

Is there a way to get Cloud Build to display the output of this program like it would when running locally in terminal? If not, then I would guess I need to use another GCP service like Cloud Functions or Cloud Run to actually test my programs.

Thanks!

Upvotes: 1

Views: 3430

Answers (1)

DazWilkin
DazWilkin

Reputation: 40051

Your intent (a CI pipeline) is a fine application for Cloud Build.

Cloud Build is conceptually straightforward but it does take some time to get your head around it. Conceptually, Cloud Build is a pipeline tool. You give it some inputs and a series of container image (steps) and it applies each image (in turn) to the output of the preceding step and produces output artifacts (often images).

NOTE Although it is a requirement that the steps in Cloud Build are defined by (easily distributable) container images, there is no requirement that what's produced also be container images.

It has 2 modes and, it appears you're using the mode in which you submit a Dockerfile and Cloud Build effectively does a docker build ... on your Dockerfile for you (resulting in an image). Per @skolsuper, your Dockerfile builds an image that, when docker run ... will run python -u script.py. This is probably not what you want.

Here's an example that's similar:

FROM busybox

RUN echo "[RUN] Hello Freddie!"

ENTRYPOINT ["/bin/ash","-c","echo \"[ENTRYPOINT] Hello Freddie!\""]

Then:

PROJECT=[[YOUR-PROJECT]]
QUESTION="62293281"
gcloud builds submit \
--tag=gcr.io/${PROJECT}/${QUESTION} \
--project=${PROJECT}

This outputs:

...
starting build "4414e398-54b9-463e-8281-dc2a35196ca8"

FETCHSOURCE
...
Operation completed over 1 objects/250.0 B.                                      
BUILD
Step 1/3 : FROM busybox
...
 ---> 1c35c4412082
Step 2/3 : RUN echo "[RUN] Hello Freddie!"
 ---> Running in 97eff39fa957
[RUN] Hello Freddie!
Removing intermediate container 97eff39fa957
 ---> c2fd37a6430a
Step 3/3 : ENTRYPOINT ["/bin/ash","-c","echo \"[ENTRYPOINT] Hello Freddie!\""]
 ---> Running in d55f7deb85e6
Removing intermediate container d55f7deb85e6
 ---> 5f0ba5789757
Successfully built 5f0ba5789757
...
PUSH
latest: digest: sha256:2261cd00817477bd1d2017033f9fab38f92de6f0dc6b8658836bad20c4439b42 size: 527
DONE

NOTE See [RUN] Hello Freddie! appears in the build output

And, we can run the image:

docker run --interactive --tty gcr.io/${PROJECT}/${QUESTION}@sha256:2261cd00817477bd1d2017033f9fab38f92de6f0dc6b8658836bad20c4439b42
...
[ENTRYPOINT] Hello Freddie!

NOTE See [ENTRYPOINT] Hello Freddie! appears when the image is run.

So, you may use commands in the Dockerfile to output state using e.g. RUN echo ... or RUN ls -l and the (standard) output (|error) is captured by Cloud Build.

You may review (all) build jobs for a project, describe them and review build logs using:

for BUILD in $(gcloud builds list --project=${PROJECT} --format="value(id)")
do
  echo ${BUILD}
  gcloud builds describe ${BUILD} --project=${PROJECT}
  gcloud builds log ${BUILD} --project=${PROJECT}
done

NOTE you may access the same functionality through Cloud Console too: Dashboard; Builds; and clicking on a build will take you to the build's logs.

NOTE gcloud builds log ... shows the same information as was shown above and would, in my case, include a line with [RUN] Hello Freddie!.

The other form of gcloud build submit ... using a cloudbuild.yaml configuration file. This mechanism provides you with the ability to run more than one step. More than one docker build ... equivalent command. It's possible that this approach is more suitable for your CI needs.

Upvotes: 3

Related Questions