Gitlab + Jenkins + Docker Automated deployment front End project
1, the background
Develop push code to Gitlab, trigger Jenkins to pull code automatically, compile and package it through NPM, and then execute shell script to make Docker build image and push it to private server (or Ali Cloud) warehouse. After this operation is completed, execute SSH command on Jenkins server to log in to the deployment server, docker pulls image from warehouse (private server) and starts container.
2. Environment preparation
2.1. Deploy GitLab
Docker – compose. Yml as follows:
Web: image: 'gitlab/gitlab-ee:latest' restart: always hostname: '192.168.25.128' Environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://192.168.25.128:8090 # Add any other gitlab. Rb configuration here, each on its own line ports: Volumes: - '8090:8090' - '/srv/gitlab/config:/etc/gitlab' - '/srv/gitlab/logs:/var/log/gitlab' - '/srv/gitlab/data:/var/opt/gitlab'Copy the code
** GitLab deployment requires a minimum of 4G memory
2.2. Deploy dockerHub
Docker Hub as Docker default official public image; If you want to build your own private image repository, the official registry image is also provided, making it very simple to build a private repository. Docker-comemage.yml: docker-comemage.yml: docker-comemage.yml: docker-comemage.yml
image: 'registry:2'
containers: dockerhub
restart: always
ports:
- '5000:5000'
volumes:
- '/home/dockerhub/data:/var/lib/registry'
Copy the code
The Docker CLI client is accessed using HTTPS by default, and the deployed Registry does not provide HTTPS. Therefore, we need to add HTTP trust to the Docker host of the pull image: {"insecure-registries":[" 192.168.1.95:5000 "]} # then restart docker systemctl Daemon -reload && systemctl restart docker # test whether the repository is ok docker pull Ubuntu docker tag Ubuntu localhost:5000/ Ubuntu docker Push localhost: 5000 / ubuntu # to check the uploaded image of curl http://localhost:5000/v2/_catalogCopy the code
2.3. Deploy Jenkins
2.3.1 Create Jenkins’ working directory and grant permissions
Mkdir -p /home/jenkins_home CD /home/chown -r 1000 jenkins_home # assign the owner of the current directory to uid 1000Copy the code
2.3.2 Writing docker-compose file
Version: '3.7' Services: Jenkins: image: Jenkins/Jenkins: LTS container_name: Jenkins Environment: - TZ=Asia/Shanghai volumes: - /home/jenkins_home/home:/var/jenkins_home ports: - "8050:8080" expose: - "8050" restart: alwaysCopy the code
2.3.3 Starting the Container
cd /home/jenkins_home/
docker-compose up -d
Copy the code
2.3.4 Enabling Firewall port 8050
firewall-cmd --zone=public --add-port=8050/tcp --permanent
firewall-cmd -reload
Copy the code
2.3.5 Modifying the download source of the Plug-in Center
vi /home/jenkins_home/hudson.model.UpdateCenter.xml # hudson.model.UpdateCenter.xml <? The XML version = '1.1' encoding = "utf-8"? > <sites> <site> <id>default</id> <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url> </site> </sites>Copy the code
3. Configure Jenkins
3.1 the unlock Jenkins
Copy the password after startup into the administrator password box as shown below
3.2 Modify Jenkins domestic mirror source
System Administration – Plug-in Center – Advanced – Upgrade site
https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
Copy the code
3.3 Downloading plug-ins
Select plugins to install: Docker, Docker-build-step, SSH, Gitlab, Gitlab Hook, NodeJS
Docker is used to pack images, start containers, etc. Docker-build-step can add build steps to Jenkins; SSH is used to execute Shell commands on remote Docker hosts; Gitlab, Gitlab Hook is used for Gitlab to automatically trigger Jenkins construction; NodeJS package front-end project
3.3.1 Configuring SSH: System Management – System Configuration – New
3.3.2 Configure docker-build-step
Docker host needs to enable remote API and execute on docker host:
Vim/lib/systemd/system/docker. The value of the service # modified ExecStart ExecStart = / usr/bin/dockerd -h fd: / / -- containerd = / run/containerd containerd. The sock - H TCP: / / 0.0.0.0:2375Copy the code
Reload the configuration file
systemctl daemon-reload & systemctl restart docker
Copy the code
Port for enabling the firewall
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --query-port=2375/tcp
Copy the code
3.3.3 docker configuration
System Management – System Configuration -Cloud Added cluster configuration
3.3.4 Configuring the Node Version
System Administration – Global Tool Configuration -NodeJS
4 Create a build task
4.1 Creating a Task
4.2 Source Code Management
4.3 Build Trigger
Setting -Webhooks Set Jenkins trigger address and token
The url for
secret token
Gitlab reported an error: THE Url is blocked: Requests to the local network are not allowed
Admin area => Settings => Network=>Outbound requests
4.4 Building an Environment
Select Node-> Select the Node version
4.5 Build -> Execute shell
4.5.1 Use NPM to package front-end projects
4.5.2 Creating default.conf and Dockerfile Files
4.5.3 Pack front-end packages into images and put them into DockerHub
echo $PATH pwd node -v npm -v npm install npm run build pwd cd /var/jenkins_home/workspace/test/ #! /bin/bash if [ -f "./default.conf" ]; then rm -f ./default.conf fi if [ -f "./Dockerfile" ]; then rm -f ./Dockerfile fi echo "server {" >> ./default.conf echo " listen 80;" >> ./default.conf echo " server_name localhost;" >> ./default.conf echo " location / {" >> ./default.conf echo " root /usr/share/nginx/html;" >> ./default.conf echo " index index.html index.htm;" >> ./default.conf echo ' try_files $uri $uri/ /index.html; ' >> ./default.conf echo " }" >> ./default.conf echo "}" >> ./default.conf cat ./default.conf echo "FROM nginx:latest" >> ./Dockerfile echo "COPY ./dist /usr/share/nginx/html/" >> ./Dockerfile echo "COPY ./default.conf /etc/nginx/conf.d/" >> ./Dockerfile echo "EXPOSE 80" >> ./Dockerfile cat ./DockerfileCopy the code
4.6 Building an Image and uploading it to DockerHub
4.7 Deployment Items
-
Use SSH to connect to the server
-
Pause deleting the test container and image
-
Run the new version image file
Docker rm -f test docker rmI 192.168.25.128:5000/test Docker run -d -p 8080:80 --name=test 192.168.25.128:5000/testCopy the code
The first time you deploy the container, you can just start the container