user3755946
user3755946

Reputation: 804

How do I have multiple docker images available in one stage Git lab CI

I have the following .gitlab-ci.yml

stages:
  - test
  - build
  - art

image: golang:1.9.2

variables:   
  BIN_NAME: example
  ARTIFACTS_DIR: artifacts   
  GO_PROJECT: example
  GOPATH: /go

before_script:
  - mkdir -p ${GOPATH}/src/${GO_PROJECT}
  - mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
  - go get -u github.com/golang/dep/cmd/dep
  - cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
  - cd ${GOPATH}/src/${GO_PROJECT}

test:   
  stage: test
  script:
    # Run all tests
    go test -run ''

build:   
 stage: build
 script:
    # Compile and name the binary as `hello`
    - go build -o hello
    - pwd
    - ls -l hello
    # Execute the binary
    - ./hello
    # Move to gitlab build directory
    - mv ./hello ${CI_PROJECT_DIR}   artifacts:
    paths:
    - ./hello

The issue is my program is dependant on both Go and Mysql...

I am aware i can have a different docker image for each stage but my test stage needs both

go test & MySql

What I have looked into:

I have learn ho to create my own docker image based using docker commit and also how to use a docker file to build and image up.

However I have hear there are way to link docker containers together using docker compose, and this seems like a better method...

I have no idea how to go about this in GitLab, I know I need a compose.yml file but not sure where to put it, whats need to go in it, does it create an image that I then link to from my .gitlab-ci.yml file?

Perhaps this is over kill and there is a simpler way?

Upvotes: 2

Views: 3844

Answers (1)

Pierre B.
Pierre B.

Reputation: 12943

I understand your tests need a MySQL server in order to work and that you are using some kind of MySQL client or driver in your Go tests.

You can use a Gitlab CI service which will be made available during your test job. GitlabCI will run a MySQL container beside your Go container which will be reachable via it's name from the Go container. For example:

test:   
  stage: test
  services: 
  - mysql:5.7
  variables:
    # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: mydb
    MYSQL_ROOT_PASSWORD: password
  script:
    # Run all tests
    go test -run ''

Will start a MySQL container and it will be reachable from the Go container via hostname mysql. Note you'll need to define variables for MySQL startup as per the Environment variables section of image documentation (such as Root password or database to create)

You can also define the service globally (will be made available for each job in your build) and use an alias so the MySQL server will be reachable from another hostname.

Upvotes: 4

Related Questions