2

I have built a Golang project, I want to deploy the app when successfully tested with GitLab ci, but when do some tests, it fails because cannot connect to MySQL. I want to use the Golang image and MySQL image in one stage. This is my current pipeline. On stage test, on before script fails (/bin/bash: line 130: mysql: command not found)

# To contribute improvements to CI/CD templates, please follow the Development guide 

at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Go.gitlab-ci.yml

image: golang:latest

services:
  - mysql:latest

stages:
  - test
  - build
  - deploy

variables:
  MYSQL_DATABASE: "db"
  MYSQL_USER: "user"
  MYSQL_PASSWORD: "password"
  MYSQL_ROOT_PASSWORD: "password"

format:
  stage: test
  variables:
    # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: $MYSQL_DATABASE
    MYSQL_PASSWORD: $MYSQL_PASSWORD
    MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
  services:
    - mysql:latest
  before_script:
    - mysql --version

  script:
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)

compile:
  stage: build
  script:
    - mkdir -p typing
    - go build -o typing ./...
  artifacts:
    paths:
      - typing

deploy:
  image: google/cloud-sdk:alpine
  stage: deploy
  allow_failure: true
  script:
    - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 -d)
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - (echo "$SSH_PRIVATE_KEY" | base64 -d) > file
    - echo "$SSH_PUBLIC_KEY" > file.pub
    - chmod 600 file
    - echo $SERVICE_ACCOUNT > file.json
    - gcloud auth activate-service-account --key-file file.json
    - gcloud compute scp typing/* --project="project-id" --zone="zone"  vm-name:/home/ubuntu
    - ssh -i file ubuntu@public-ip 'sudo ./kill.sh; sudo ./start.sh'
  artifacts:
    paths:
      - typing

How can I achieve that?

Thanks in advance.

lamboktulus1379
  • 350
  • 1
  • 4
  • 10

1 Answers1

1

In the stage test, the job is based on the golang image as a result it does not come packaged with the MySQL client.

In order to reach the MySQL service you defined you need to install the client

If I am not mistaken the Golang image is based on debian, so something like this

before_script:
  - apt get-update
  - apt get-install -y default-mysql-client
  - mysql --version
Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14
  • I run apt-get update instead, but It prompts a message that causes the script to stop. $ apt-get install default-mysql-client Reading package lists... Building dependency tree... Reading state information... Do you want to continue? [Y/n] Abort. Cleaning up project directory and file based variables 00:01 ERROR: Job failed: exit code 1 – lamboktulus1379 May 20 '22 at 04:21
  • 1
    @lamboktulus1379 Excuse me I forgot to add the auto approve flag – Tolis Gerodimos May 20 '22 at 04:26
  • I've added the flag, and used - apt-get -y update - apt-get -y install default-mysql-client $ mysql --version mysql Ver 15.1 Distrib 10.5.15-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper It works now, but after run $ go test -race $(go list ./... | grep -v /vendor/) got message: [error] failed to initialize database, got error dial tcp: lookup mysql:tcp://172.17.0.3:3306: no such host. Still cannot connect to the database. – lamboktulus1379 May 20 '22 at 04:33
  • @lamboktulus1379 What host are you providing for the connection? You should provide mysql as host – Tolis Gerodimos May 20 '22 at 05:25
  • The host is mysql. – lamboktulus1379 May 20 '22 at 05:27