Making the yml service name a variable

I need to put a variable name on the service in my yml file used to deploy a feature branch from gitlab into rancher. The variable will be from gitlab -> CI_COMMIT_REF_SLUG. I have tried to get the name feature to be the variable. The yml setup keeps deploying the word “feature” as the name for the subdomain that appears at feature.mywebsite.com.

What do I need to change in my yml to get the ${CI_COMMIT_REF_SLUG} to be deployed as the subdomain?

Here is my feature.yml file.

version: '2’
services:
feature:
container_name: ${CI_COMMIT_REF_SLUG}
image: my.image.com/site/feature:${CI_BUILD_REF}
expose:
- "4500"
labels:
sxg.service.type: web
sxg.service.port: 4500
health_check:
port: 4500
request_line: GET / HTTP/1.0
interval: 10000
healthy_threshold: 2
unhealthy_threshold: 3
response_timeout: 10000

The name of the service is feature because that’s what’s on the third line (the key under services). container_name is for setting the name that shows up in docker ps and is ignored in Rancher because they have to be unique and would not work with scale > 1.

How would I get the name feature to be a variable that pulls the branch name from gitlab? Is there a way to include a variable on the name feature in line 3?

I’d expect something like this:

version: '2'
services:
  ${YOUR_VARIABLE}:
    container_name: ${CI_COMMIT_REF_SLUG}
    image: my.image.com/site/feature:${CI_BUILD_REF}
    expose:
      - "4500"
    labels:
    sxg.service.type: web
    sxg.service.port: 4500
    health_check:
      port: 4500
      request_line: GET / HTTP/1.0
      interval: 10000
      healthy_threshold: 2
      unhealthy_threshold: 3
      response_timeout: 10000

That is not valid syntax for yml. But I did get it to work by adding a sxg.service.subdomain variable.

1 Like