📜 ⬆️ ⬇️

How to configure the web application warm on Go for Gitlab on VDS


Foreword


This article is the result of a weekly search for very scattered information about how to set up a web-service deployment on Go. Not on Heroku, not on Docker, not on Digital Ocean, but simply on old-fashioned VDS with CentOS 7x64. For some reason, the network does not have this information, and most tutorials begin with how to set up a build, and end with the launch of tests.

Immediately I warn you that for the first time I set up the CI / CD process, so this is an article from a newbie to a newbie.

Surely much can be corrected here, so I will gladly accept any comments in the comments and try to update the changes as soon as possible. It is also possible that such an article is already there, and I am simply terribly using a search engine. Then, please give a link to it, and in this case, I will delete the article and go to sprinkle ashes on my head.

Initial data



src/ public/ index.html main.go 

Server Configuration: Creating a Service


First, create a service for our application. In CentOS 7, this is done quite simply. You need to write this script in a file called serviceName.service:
')
 [Unit] #   Description=Service Description After=network.target [Service] Type=simple #  ,       User=username #    ExecStart=/username/service/binaryFile Restart=on-abort [Install] WantedBy=multi-user.target 

The script itself must be put in the folder etc / systemd / system /

SSH setup


On the server, run the command:

 ssh-keygen -f /etc/ssh/hmp.key 

On request
Enter passphrase (empty for no passphrase)
do not enter the password, just click on Enter.

Two files will be generated in the / etc / ssh / folder:

  1. hmp.key - private key
  2. hmp.key.pub - public key

We need a private key. View its contents with the command:

 cat /etc/ssh/hmp.key 

It will look something like this:

 -----BEGIN RSA PRIVATE KEY----- {    } -----END RSA PRIVATE KEY----- 

Everything is completely copied to the clipboard.

ATTENTION! - not only the key itself, but also
----- BEGIN RSA PRIVATE KEY ----- and ----- END RSA PRIVATE KEY -----

Gitlab setup


First, fill in the data important for the depot (user name, password, etc.).
Even if your repository is public, they will remain closed.

In Gitlab in the repository go to Settings -> CI / CD -> Variables. We create the following variables there:


Add the .gitlab-ci.yml file with the following contents to the repository:

 image: golang:latest before_script: #     sshpass - apt-get update -qq && apt-get install -y -qq sshpass #  ,    .   govendor,      - go get github.com/gorilla/mux - go get github.com/gorilla/websocket #  SSH - '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 #     - mkdir -p /go/src/gitlab.com/$GROUP_NAME #     - git clone git@gitlab.com:$GROUP_NAME/$REPOSITORY_NAME.git /go/src/gitlab.com/$GROUP_NAME/$REPOSITORY_NAME #      - mkdir -p $CI_PROJECT_DIR/build/ #        (, HTML- ..)  . #       src/public - cp -r $CI_PROJECT_DIR/src/public $CI_PROJECT_DIR/build stages: - build - deploy compile: stage: build script: #     Go  - cd /go/src/gitlab.com/$GROUP_NAME/$REPOSITORY_NAME/src #    . ,        build       main - go build -race -ldflags "-extldflags '-static'" -o $CI_PROJECT_DIR/build/main artifacts: paths: - $CI_PROJECT_DIR/build/main deploy: stage: deploy script: #      ()      public - cd $CI_PROJECT_DIR/build #   sshpass      VDS - sshpass -V - export SSHPASS=$USER_PASS #   - sshpass -e ssh -o stricthostkeychecking=no $USER@$HOST systemctl stop $SERVICE_NAME #    - sshpass -e scp -o stricthostkeychecking=no -r . $USER@$HOST:$TARGET_DIR_ON_HOST #   - sshpass -e ssh -o stricthostkeychecking=no $USER@$HOST systemctl restart $SERVICE_NAME 

After the configuration, this script is pushed into the repository and we have a ready build and a warmup. That's all!

I hope the article was helpful. For any questions and comments gladly answer in the comments!

Source: https://habr.com/ru/post/421087/


All Articles