D
D
Dmitry Afonchenko2018-08-16 13:00:22
go
Dmitry Afonchenko, 2018-08-16 13:00:22

How to set up CI/CD in gitlab for a go project?

Good afternoon! There was a question on the setup of CI/CD on a remote server. I'm new to this, so the question is probably trivial, but still it was not possible to find adequate information on it.
There is a server on VDS with CentOS 7_x64
A web application written in Go is running on it. How to make gitlab deliver a new version to the server after the build and run it? Here is my .gitlab-ci.yml:

image: golang:latest

variables:
  REPO_NAME: $REPO_NAME

before_script:
  - go get github.com/gorilla/mux
  - go get github.com/gorilla/websocket
  - mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
  - echo $CI_PROJECT_DIR
  - echo $GOPATH
  - ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME
  - cd $CI_PROJECT_DIR/src
  - ls

stages:
    - build
    - deploy

compile:
    stage: build
    script:
      - go build -race -ldflags "-extldflags '-static'" -o $CI_PROJECT_DIR/mybinary
    artifacts:
      paths:
        - mybinary

deploy_staging:
  stage: deploy
  environment:
    name: Staging
    url: $SEVER_ADDRESS
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - mkdir -p ~/.ssh
  - eval $(ssh-agent -s)
  - ' && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
  - ssh-add <(echo "$USER_PASS")
  - ssh -o StrictHostKeyChecking=no $USERNAME@"$SEVER_ADDRESS" 'rm -rf /var/www/html/*'

It breaks down at this point:
$ && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh-add <(echo "$USER_PASS")
Enter passphrase for / dev/fd/63: ERROR: Job failed: exit code 1

UPD: Answer in the comments to the answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jhn Doe from, 2018-08-16
@Indermove

gitlab should download your project, then throw it via ssh to your server, below I will throw off my config, which gives gitlab access to the organization's repositories. In addition to everything, you need to add an ssh key, go here https://gitlab.com/orgname/reponame/settings/ci_cd, make it xclip -sel clip < ~/.ssh/id_rsa.puband slip it on the gitlab on that very page into the variables of the copied key.
specify the variable SSH_PRIVATE_KEY
Everything.

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question