This is the third day of my participation in Gwen Challenge

1. Create the folder to be mapped on the physical machine

mkdir gitlab
cd gitlab/
mkdir config logs data
Copy the code

Two, pull mirror image

docker pull gitlab/gitlab-ce
Copy the code

3. Run the image

docker run -d  -p 443:443 -p 8090:80 -p 222:22 --name gitlab --restart always -v /usr/local/gitlab/config:/etc/gitlab -v /usr/local/gitlab/logs:/var/log/gitlab -v /usr/local/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
Copy the code

Four, configuration,

As a Gitlab server, we need a fixed URL access address, so we need to configure gitlab.rb

vim /usr/local/gitlab/config/gitlab.rb

# configure the HTTP protocol to use the access address, no port number default is80
external_url 'http://IP address'Gitlab_rails ['gitlab_ssh_host'] = 'IP address'
gitlab_rails['gitlab_shell_ssh_port'] = 222# This port is run22portmapped222portCopy the code

Five, restart

docker restart gitlab
Copy the code

Six, solve the problem successfully

At 502 error

1. Port 8090 is found to be occupied. Run a gitlub port 8091 again

2. Gitlab has not been successfully started. Wait a moment

docker run -d  -p 443:443 -p 8091:80 -p 222:22 --name gitlab --restart always -v /usr/local/gitlab/config:/etc/gitlab -v /usr/local/gitlab/logs:/var/log/gitlab -v /usr/local/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
gitlub1
Copy the code

7. Add an account

First enter the password of user root

Secondly, register a new account, automatic login after registration

Create a project

Cloning address

Create local projects

After configuring Git, access was denied.

The reason is that the port is inconsistent. The default port is 80, but we changed it to 8091, so we need to add 8091 to the Clone address

To do this once and for all, modify the configuration file.

1. Enter the container

docker exec -it gitlab bash
Copy the code

2. Modify the configuration file

vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
Copy the code

3. Restart the service

vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
Copy the code

10. Close the registration function

If you do not close the registration function, there will be many strange accounts.

1. Log in as the root user

2. Cancel registration

11. Configure gitlab — Runner

1. Pull the mirror

docker pull gitlab/gitlab-runner:latest
Copy the code

2. Create a container

docker run -d --name gitlab-runner --restart always -p 8093:8093 -v /var/run/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest
Copy the code

3, registration,

The input data is in gitlab

  • Into the container
docker exec -it gitlab-runner gitlab-runner register
Copy the code
  • Enter the gitlab domain name
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
Copy the code
  • Enter the token of Gitlab
Please enter the gitlab-ci token for this runner
xxx
Copy the code
  • Description of Runner, can be modified in gitlab
Please enter the gitlab-ci description for this runner
[hostname] my-runner
Copy the code
  • Runner tag, modifiable
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tagEnter the [Runner executor](https://docs.gitlab.com/runner/executors/README.html):
Copy the code
  • Enter the Runner executor:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker
Copy the code

Xii. Test

Compile. Gitlab-ci.yml file

Stages: -deploy docker-deploy: stage: deploy # Executes the Job contentscriptPython3 -wget HTTPS:/ / www.python.org/ftp/python/3.5.7/Python-3.5.7.tgz
     - tar -xzvf Python-3.57..tgz
     - add-apt-repository ppa:jonathonf/python-3.57./configure --enable-optimizations -- make-make install -- pip3 install -r requirements. TXT  - echo"build python....."# Start the Ziot project,8094Port - the echo"done"
     - pwd
     - ls
     - python3 server.py
     
   sidebar: 'auto'
tagsServer that executes Job -94new
   onlyOnly the master branch executes -masterCopy the code

Automate python project deployment

Using CICD principle: when we push the project to gitlab, it will trigger.gitlab-ci.yml file, where only: -maste represents, when the branch of push is master, the script will be executed. Sidebar: ‘auto’ tags:- shell selects existing runner to execute. By this time he will pull up items to gitlab – runner, such as the folder/home/gitlab – runner/builds / 76 vguzgu / 0 / wangscaler /, the same runner folder 76 vguzgu address the same. At this time, we copy the project to our test environment through SSH, so we need to configure SSH secret-free login. When the project is pulled to the test environment, we can start the Supervisor to manage our project. We can branch to choose the deployment environment.

1. Install Supervior

2. Configure the runner to log in to the production machine without password

In gitlab-runner, switch to the gitlab-runner user to configure a secret free login, because the runner is running.gitlab-ci.yml and the user is gitlab-runner.

docker exec -it gitlab-runner bash su gitlab-runner ssh-keygen -t rsa ssh-copy-id -i /home/gitlab-runner/.ssh/id_rsa.pub root@ Server IP addressCopy the code

The configuration succeeds after entering the password

SSH INDICATES the IP address of the production machineCopy the code

No password is required to enter successfully

3. Compile. Gitlab-ci. yml file

Stages: - clean_env # Clean up the environment and kill processes. - install_dependency # update dependencies - compile_python # compile_python. - restart_server # restart service.variables:
  BASE_DIR: "/wangscaler/"

job clean_env_job:
  stage: clean_env
  script: - whoami - SSH -o stricthostkeychecking = no root @ server IP pkill supervisord | |true- SSH -o stricthostkeychecking = no root @ server IP killall python3 | |true# - SSH -o stricthostkeychecking = no root @ server IP killall/data/miniconda3 / envs/go2cloud_platform/bin/python | |true-ssh -o stricthostKeychecking =no root@server IP rm -rf /project${BASE_DIR}* sidebar:'auto'
tags:
    - shell
  only:
    - master
  when: always


job deploy_src_job:
  stage: deploy_src
  script:
    - whoami
    - scp -r /home/gitlab-runner/builds/B8F5kKw7/0/WangScaler/${BASE_DIR}* root@server IP:/project${BASE_DIR}sidebar: 'auto'
tags:
    - shell
  only:
    - master
  when: always


job install_dependency_job:
  stage: install_dependency
  script: -whoami-ssh -o stricthostKeychecking =no root@server IP pip3 install -r /project${BASE_DIR} requiremss.txtsidebar: 'auto'
tags:
    - shell
  only:
    - master
  when: always
  
  
job compile_python_job:
  stage: compile_python
  script: -whoami-ssh -o stricthostKeychecking =no root@server IP address sleep7- SSH -o stricthostKeychecking =no root@ Server IP address"cd /project/wangscaler && python3 CreateLicense.py 00:16:3e:10:34:74"- SSH -o stricthostKeychecking =no root@ Server IP address"cd /project/wangscaler && python3 setup.py"- SSH -o stricthostkeychecking=no root@server IP python3 -m compileall -b /project${BASE_DIR}build/ -ssh -o Stricthostkeychecking =no root@server IP find /product/ wangscaler-name"server.py" -type f -print -exec rm -rf {} \;
  sidebar: 'auto'
tags:
    - shell
  only:
    - master
  when: always

job restart_server_job:
  stage: restart_server
  script: -whoami-ssh -o stricthostKeychecking =no root@server IP address sleep7-ssh -o stricthostKeychecking =no root@server IP supervisord -c /etc/supervisord.conf -ssh -o stricthostKeychecking =no Root @ server IP ps - ef | grep supervisord | grep -v grepsidebar: 'auto'
tags:
    - shell
  only:
    - master
  when: always

job check_server_job:
  stage: check_server
  script: -whoami-ssh -o stricthostKeychecking =no root@server IP address sleep7- SSH -o stricthostkeychecking = no root @ server IP ps - ef | grep"8193"
  sidebar: 'auto'
tags:
    - shell
  only:
    - master
  when: always
Copy the code