Home > database >  GitLab pipeline pass along variable to build arg
GitLab pipeline pass along variable to build arg

Time:12-23

Somehow I cannot figure out how to pass along this ssh key to the build process of a container. This is the step in the .gitlab-ci.yml:

php:
  stage: build
  services:
    - docker:20.10.12-dind
  image: docker:20.10.12
  script:
    # Leverage --cache-from and try to use an existing image if it exists
    - echo "$DOCKER_ACCESS_KEY" | docker login --username $DOCKER_REGISTRY_USER --password-stdin
    - docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
    - docker pull $PHP_CACHE_TAG || true
    - cat "$LARAVEL_ENV_FILE" > "$(pwd)/.env"
    - DOCKER_BUILDKIT=1 docker build --cache-from $PHP_CACHE_TAG
      --build-arg BUILDKIT_INLINE_CACHE=1
      --build-arg SSH_PRIV_KEY="${SSH_PRIV_KEY}"
      --tag $PHP_CACHE_TAG
      --tag $PHP_TAG
      --target=api_prod .
    - docker push $PHP_TAG
    - docker push $PHP_CACHE_TAG

It's about this line: --build-arg SSH_PRIV_KEY="${SSH_PRIV_KEY}"

In the Dockerfile I do this: echo "$SSH_PRIV_KEY" > /root/.ssh/id_rsa; \ (After defining ARG SSH_PRIV_KEY at the top of the Dockerfile)

The /root/.ssh/id_rsa remains empty afterwards.

For more context of what I do in the Dockerfile:

# Needed to be able to install custom dependencies
RUN mkdir -p /root/.ssh; \
    ssh-keyscan -H gitlab.com >> /root/.ssh/known_hosts; \
    echo "$SSH_PRIV_KEY" > /root/.ssh/id_rsa; \
    cat /root/.ssh/id_rsa; \
    chmod 600 /root/.ssh/*;

I suspect I'm doing something wrong in the gitlab-ci build step. But not sure.

CodePudding user response:

In your Docker file, use ARG SSH_PRIV_KEY

then you use

ARG SSH_PRIV_KEY
RUN mkdir -p /root/.ssh; \
    ssh-keyscan -H gitlab.com >> /root/.ssh/known_hosts; \
    echo "$SSH_PRIV_KEY" > /root/.ssh/id_rsa; \
    cat /root/.ssh/id_rsa; \
    chmod 600 /root/.ssh/*;

This should work.

  • Related