简体   繁体   中英

Share env variables between Docker-Compose and GitLab-CI

Note: I've omitted some details, stages and settings from the following config files to make the post shorter and the question more "readable". Please comment if you believe essential details are missing, and I'll (re-) add them.

Now, consider a docker-compose project, described by the following config,

# docker-compose.yml
version: '3'

services:
    service_1:
        build: ./service_1
        image: localhost:8081/images/name_of_service_1
        container_name: name_of_service_1
    service_2:
        build: ./service_2
        image: localhost:8081/images/name_of_service_2
        container_name: name_of_service_2

Then, in the project's git repository, we have another file, for the GitLab continuous integration config,

# .gitlab-ci.yml
image: docker:latest

stages:
    - build
    - release

Build:
    stage: build
    script:
        - docker-compose build
        - docker-compose push

# Release by re-tagging some specific (not all) images with version num
Release:
    stage: release
    script:
        - docker pull localhost:8081/images/name_of_service_1
        - docker tag localhost:8081/images/name_of_service_1 localhost:8081/images/name_of_service_1:rel-18.04

Now, this works fine and all, but I find it frustrating how I must duplicate image names in both files. The challenge here (in my own opinion) is that the release stage does not release all images that are part of the compose, because some are pure mock/dummy-images purely meant for testing. Hence, I need to tag/push the images and containers individually.

I would like to be able to define the image names only once : I tried introducing the .env file, which is automatically imported by docker-compose.yml ,

# .env
GITLAB=localhost:8081/images
SERVICE_1=name_of_service_1
SERVICE_2=name_of_service_2

This let's me update the docker-compose.yml to use these variables and write image: "${GITLAB}/${PARSER_NAME}" as opposed to the original file above. However, I'm unable to import these variables into gitlab-ci.yml , and hence need to duplicate them.

Is there a simple way to make docker-compose.yml and .gitlab-ci.yml share some (environment) variables?

I'm not sure what your problem is. So you have an .env file in your project's root directory that contains all the variavles you need to be set in your release job? Why don't you just load them like this:

script:
    - source ./env
    - docker pull "${GITLAB}/${SERVICE_1}"
    - docker tag "${GITLAB}/${SERVICE_1}" "${GITLAB}/${SERVICE_1}:rel-18.04"

There is no builtin way for .gitlab-ci.yml to load environment variables via an external file (eg, your .env file).

You could try adding the environment variables to your .gitlab-ci.yml file itself (in addition to your .env file used locally).

This may seem like you're copy-pasting code, but CI systems run the same environment you run locally by the means of a file (eg, .gitlab-ci.yml for GitLab CI) with the same commands you've used locally, so it's okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM