Skip to content

FF_NETWORK_PER_BUILD: provide the build network name as an environment variable

Description

The "network per build" feature enables creating a dynamic build network for each build with a non-trivial name composed of several components, e.g.: runner-xtwsd1d9-project-1070-concurrent-0-job-1939486-network.

Would it be possible to provide this network name to the CI job as an environment variable (e.g., CI_BUILD_NETWORK_ID)?

Below is an example use case, where this would be really useful: docker build needing access to some service containers. It runs on a Docker runner with bind mount on /var/run/docker.sock:/var/run/docker.sock.

build-artifact:
  stage: build
  only:
    - master
  tags:
    - docker-builder
  services:
    - name: redis:alpine
      alias: redis
    - name: mysql:5.6
      alias: mysql
    - name: 'docker.elastic.co/elasticsearch/elasticsearch:6.3.2'
      alias: es
      entrypoint: ["/bin/bash"]
      command: ["-c", "env discovery.type=single-node docker-entrypoint.sh eswrapper"]
  image: docker
  script:
    - docker build
        --pull
        --network ${CI_BUILD_NETWORK_ID}
        -t docker.example.com/${CI_PROJECT_PATH}:${CI_COMMIT_REF_SLUG}
        -f build/ci/Dockerfile .
    - docker login -u user -p password docker.example.com
    - docker push docker.example.com/${CI_PROJECT_PATH}:${CI_COMMIT_REF_SLUG}
  after_script:
    - docker logout docker.rsvx.it

After some digging I found out that the runner-xtwsd1d9-project-1070-concurrent-0 part of the network name is actually the Docker container $HOSTNAME. Thus, as a workaround, one can simply add

export CI_BUILD_NETWORK_ID="$HOSTNAME-job-$CI_JOB_ID-network"

to the script:, before_script: of the CI job or to the pre_build_script: of the Gitlab runner.

Proposal

Provide the dynamic build network name to the CI job as an environment variable (e.g., CI_BUILD_NETWORK_ID)?

Edited by Ghost User