gitlab CI/CD: How to enter into a container for testing i.e getting an interactive shell

Viewed 706

Like in docker we can enter a container by and have an interactive shell

docker-compose exec containername /bin/bash

Similary in the script in gitlab CI/CD can we enter into it. Like it provides an interactive shell

Eg:

build:
  stage: build
  script:
    - pwd; ls -al
    HERE I WANT TO HAVE AN INTERACTIVE SHELL SO THAT I CAN CHECK FEW THINGS
1 Answers

I think we need to do an small detour here and explain how jobs are working in GitLab CI.

Each job is an encapsulated docker container. The container only executes things you like to be executed within the script directive. By default the jobs on shared runners are using a ruby container image.

If you want to check, what you have available within your image, or you want try things out locally. You can do so running a container with this image locally and mounting your project folder into it.

docker run --rm -v "$(pwd):/build/project" -w "/build/project" -it <the job image> /bin/bash # or /bin/sh or whatever shell is available in the image.
# -v mounts the current directory int /build/project in your container
# -w changes the working directory to the mounting point
# /bin/bash starts the shell, it might be that there are others within the image

If you want to use a different docker image, lets say because you are running some other build tool, you can specify this with the image directive like:

build:
  image: maven:latest
  script:
    - echo "some output"

You do have the functionality available within your job, which is provided by the image. As the job will run within a container of that image.

You can even use some tools like https://github.com/firecow/gitlab-ci-local to verify this locally. But in the end those are just docker images, and you can easily recreate the flow on your own.

Related