Jenkins is installed in the virtual machine
1.1 Background introduction
Jenkins is a popular open source continuous integration (Continuous Integration) tool, widely used in project development, with functions such as automated build, test and deployment.
Jenkins official website: Jenkins
Jenkins Features
- An open source Java language development continuous integration tool that supports continuous integration and continuous deployment.
- Easy to install, deploy and configure: it can be installed through yum, or download the war package, and quickly realize the installation and deployment through the docker container, which is convenient for web interface configuration management.
- Message notification and test report: integrate RSS/E-mail to publish the build result through RSS or notify through e-mail when the build is completed, and generate a JUnit/TestNG test report.
- Distributed build: Support Jenkins to allow multiple computers to build/test together.
- File identification: Jenkins can track which jars are generated by which build, which version of jar is used by which build, etc.
- Rich plug-in support: Support extended plug-ins, you can develop tools suitable for your team, such as git, svn, maven, docker, etc.
1.2 Installation
Reference: Jenkins installation and entry configuration - short book
1.3 use
First of all, what we want to achieve is the automatic deployment of a back-end service of flask. My back-end service is deployed on docker, so we must first know some simple commands of docker. I will not go into details here. Use it directly
The flask service app.py code is as follows
from flask import Flask, jsonify app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' @app.route('/health') def health_checking(): ret = {'status': 'UP'} return jsonify(ret) @app.route('/hello') def hello_chen(): return 'Hello, chen!' @app.route('/index') def index(): return 'Index!' @app.route('/ckk') def ckk(): return 'Chenkeke'
The DockerFile file is as follows, which means to use gunicorn to start the flask app and then use port 5001 to access it
FROM python:3.8 RUN pip install --no-cache-dir -i http://mirrors.aliyun.com/pypi/simple/ \ --trusted-host mirrors.aliyun.com Flask gunicorn ADD . /app ENV GUNICORN_CMD_ARGS="--bind=0.0.0.0:5001 --chdir=./app/ --workers=4" CMD ["gunicorn", "app:app"]
After these two files are configured, they need to be uploaded to gitlab, as shown in the figure
The ssh of the configured server is on gitlab, so that it can be pulled directly through ssh (but it doesn’t matter if it doesn’t match, I pulled the project through http)
After configuration, you can try to pull the project from the server, and then start a task through DockDile to see if you can run the project successfully, and go directly to the pulled file to compile (note that it is the same layer)
docker build -t your_image_name .
After success, a mirror image will be generated, and then the mirror image will be executed
docker run -d --name test -p 5001:5001 your_image_name
Open the webpage to see, successfully executed
Close the container and delete the image, otherwise an error will be reported later
If it is ok, go to the next step to configure Jenkins. After the Jenkins port is installed, the default is 8080. You can observe the interface and get familiar with it. After no problem, we click to create a new workflow
- Configure the account password of gitlab to pull the warehouse of the code
- Select the post-build operation and execute the shell
The steps to execute the shell can be Baidu meaning by yourself
#!/bin/bash echo "hello chen" # Close the docker project IMAGE_NAME="company_flask" CONTAINER_NAME="my_container" PORT_MAPPING="5001:5001" WORKSPACE_PATH="/var/lib/jenkins/workspace/demo" # Check whether the container is Already running if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then echo "Stopping existing $CONTAINER_NAME container..." docker stop $CONTAINER_NAME fi # Check if there is a Docker named $IMAGE_NAME mirror if docker images | awk '{print $1}' | grep -q "^$IMAGE_NAME$"; then # delete the mirror if it exists echo "Deleting existing $IMAGE_NAME image..." docker rmi $IMAGE_NAME fi # check Is there a Docker container named $CONTAINER_NAME if docker ps -a | awk '{print $NF}' | grep -q "^$CONTAINER_NAME$"; then # If it exists, delete the container echo "Deleting existing $CONTAINER_NAME container ..."docker rm $CONTAINER_NAME fi # Enter the working directory and build a new Docker image cd $WORKSPACE_PATH docker build -t $IMAGE_NAME . # Start a new Docker container echo "Starting new $CONTAINER_NAME container..." docker run -d --name $ CONTAINER_NAME -p $PORT_MAPPING $IMAGE_NAME
Then click Save, and the build should start at this time, view the history of the build
View console output
success. Then add an interface to the edited file in this article and push it to the remote git, and then rebuild it to have a look.
upload code
Check if git uploaded successfully
Then log in to jenkins to rebuild the image
view build history
select the most recent build
Looking at the console output, you can see that the latest code has been pulled down
See if you can access the interface
It was also successful. Now you can use automatic deployment, but it is semi-automatic deployment. Because you still need to log in to jenkins and then manually build it, it is still not perfect. Because
jenkins uses local git and online gitlab, it cannot form a closed loop. So the next blog Both gitlab and jenkins will be deployed with docker. Then a fully automatic deployment will be completed.
Finally: The following complete software testing video learning tutorial has been sorted out and uploaded. Friends can get it for free if they need it [Guaranteed 100% free]
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.