skip to Main Content

I have two jobs build_binary and build deb and I want to combine both of them. But the issue is they both use different images. Former one uses golang:latest and later one uses ubuntu:20.04 as shown:

gitlab-ci.yml

build_binary:
  stage: build
  image: golang:latest
  rules:
    - if: '$CI_COMMIT_TAG'
  # tags:
  #   - docker-executor
  artifacts:
    untracked: true
  script:
    - echo "Some script"

build deb:
  stage: deb-build
  rules:
    - if: '$CI_COMMIT_TAG'
  # tags:
  #   - docker-executor
  image: ubuntu:20.04
  dependencies:
    - build_binary
  script:
    - echo "Some script 2"
  artifacts:
    untracked: true

I have tried in these two ways but it didn’t work.

build_binary:
  stage: build
  image: golang:latest ubuntu:20.04

and

build_binary:
  stage: build
  image: [golang:latest,ubuntu:20.04]

Any pointers would be very helpful.

2

Answers


  1. Using the image and services keywords in your.gitlab-ci.yml file, you may define a GitLab CI/CD job that uses multiple Docker images.

    For example, you could use the following configuration to specify a job that uses both a Golang image and an Ubuntu image:

    build:
        image: golang:latest
        services:
          - ubuntu:20.04
        before_script:
          - run some command using Ubuntu
      script:
          - go build
          - run some other command using Ubuntu
    
    Login or Signup to reply.
  2. It’s not about gitlab-ci – for first hand you should understand what is images, what containers (docker) are in it’s nature.

    You cannot magically get mixed image that will be magically ubuntu:20.04+golang:latest. It’s just impossible to make it from gitlab-ci file.

    But you can create your own IMAGE.

    You can take Dockerfile for ubuntu:20.04, at dockerhub https://hub.docker.com/_/ubuntu

    Then you can add commands to it to install golang inside this operation system `
    After this you open golang:latest Dockerfile and copy it’s installation process to ubuntu Dockerfile with required modifications.

    Then you do docker build -t my-super-ubuntu-and-golang – see manual

    Then you check it – docker run and check that it’s normal ubuntu with golang

    If all success you can push it to your own account to dockerhub and in gitlab-ci:

    image: your-name/golang-ubuntu-20.04
    ...
    

    Suggestion to use services is very incorrect – service starts another image and connect it by network, so you can run postgres, rabbit and another services and use it in your tests for example:

    image: alpine
    services : [ rabbitmq ]
    

    not mean that rabbitmq will be started on alpine – both will started – image of alpine and image of rabbitmq with local HOST name rabbitmq, and your alpine could connect to tcp://rabbitmq:5672 and use it. It’s another approach.

    P.S.
    For example you can look at https://hub.docker.com/r/partlab/ubuntu-golang
    I think it’s not image you really want but you can see how to make mixed ubuntu-golang image

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search