Hossein Safari
Hossein Safari

Reputation: 369

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?

Upvotes: 3

Views: 11945

Answers (3)

Rishabh Khanna
Rishabh Khanna

Reputation: 1

Set this variable to true in your .gitlab-ci.yaml file.

For example:

include:
  - project: 'project-path'
    ref: <version>
    file: 'template.gitlab-ci.yml'

variables:
  FF_KUBERNETES_HONOR_ENTRYPOINT: true

This is mentioned in this document. https://docs.gitlab.com/runner/executors/kubernetes/#container-entrypoint-known-issues

Upvotes: 0

sytech
sytech

Reputation: 40891

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.

Upvotes: 6

dmoonfire
dmoonfire

Reputation: 231

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

Upvotes: 2

Related Questions