Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab-runner doesn't run ENTRYPOINT scripts in Dockerfile

I use gitlab-ci in my project. I have created an image and push it to gitlab container registry.

To create an image and register it to gitlab container registry, I have created a Dockerfile.

Dockerfile:

...

ENTRYPOINT [ "scripts/entry-gitlab-ci.sh" ]
CMD "app"
...

entry-gitlab-ci.sh:

#!/bin/bash
set -e

if [[ $@ == 'app' ]]; then
  echo "Initialize image"
  rake db:drop
  rake db:create
  rake db:migrate
fi

exec "$@"

the image will be created successfully, but when the gitlab-runner pulls and execs the created image, doesn't run the **entry-gitlab-ci** script.
What is the problem?
like image 615
Hossein Safari Avatar asked Sep 28 '26 21:09

Hossein Safari


2 Answers

Image entrypoints definitely run in GitLab CI with the docker executor, both for services and for jobs, so long as this has not been overwritten by the job configuration.

There's two key problems if you're trying to use this image in your job image:.

  1. GitLab overrides the command for the image. So your if condition won't ever catch here.
  2. Your entrypoint should be prepared to run a shell script. So, you should use something like exec /bin/bash not exec "$@" for a job image.
    Per the documentation:

The runner expects that the image has no entrypoint or that the entrypoint is prepared to start a shell command.

So your entrypoint might look something like this:

#!/usr/bin/env bash

# gitlab-entrypoint-script
echo "doing something before running commands"

if [[ -n "$CI" ]]; then
    echo "this block will only execute in a CI environment"
    
    echo "now running script commands"
    # this is how GitLab expects your entrypoint to end, if provided
    # will execute scripts from stdin
    exec /bin/bash
else
    echo "Not in CI. Running the image normally"
    exec "$@"
fi

This assumes you are using a docker executor and the runner is using a version of docker >= 17.06

You can also explicitly set the entrypoint for job images and service images in the job config image:. This may be useful, for example, if your image normally has an entrypoint and you don't want to build your image with consideration for GitLab-CI or if you wanted to use a public image that has a non-compatible entrypoint.

like image 103
sytech Avatar answered Oct 02 '26 13:10

sytech


From my experience and struggles, I couldn't get Gitlab to use the EXEC automatically. Same with trying to get a login shell working easily to pick up environment variables. Instead, you have to run it manually from the CI.

# .gitlab-ci.yml
build:
    image: your-image-name
    stage: build
    script:
        - /bin/bash ./scripts/entry-gitlab-ci.sh
like image 41
dmoonfire Avatar answered Oct 02 '26 13:10

dmoonfire