Automated testing based on Jenkins+Docker


Computer configuration: Windows 10, Asus Tianxuan, R9-4900H, RTX2060


1. Pre-tasks


1. The use of Docker .
2. Use of Jenkins .

2. Preparation


1. Start the Docker-desktop client.


2. Start the Jenkins service.


3. Write scripts


1.Dockerfile script file.

Write a Dockerfile file in the automation project directory to create a custom image file.

# 获取镜像
FROM markadams/chromium-xvfb-py3

# 设置时区
ENV TZ 'Asia/Shanghai'

# 设置编码方式
ENV LANG C.UTF-8

# 设置环境变量
ENV PATH=$PATH:/code

# 安装字体
COPY ./resources/simsun.ttf /usr/share/fonts/ttf-dejavu/simsun.ttf

# 拷贝镜像源
COPY ./resources/sources.list /etc/apt/sources.list

# 添加代码到镜像
COPY . /code

# 更新apt-get
RUN apt-get update

# 安装pip
RUN apt-get install -y python3-pip --allow-unauthenticated

# 安装依赖
RUN pip3 install -r /code/requirements.txt

# 运行程序
CMD ["python3", "/code/main/main.py"]

2. Jenkins pipeline script.

Write a Jenkins pipeline script to build the Jenkins project.

pipeline {
    
    
    agent any
    environment {
    
    
        /* Git用户名称 */
        gitUsername = ''
        /* 自动化仓库名称 */
        repositoryName = ''
        /* 自定义镜像名称 */
        imageName = ''
        /* 自定义镜像版本 */
        imageTag = ''
        /* 自定义容器名称 */
        containerName = ''
    }
    stages {
    
    
        stage('前置工作') {
    
    
            steps {
    
    
                echo '工作目录'
                sh 'pwd'
                echo '清理历史文件'
                sh 'rm -rf ${WORKSPACE}/${repositoryName}'
                echo '拉取项目'
                sh 'git clone [email protected]:${gitUsername}/${repositoryName}.git'
                echo '查看镜像'
                sh 'docker images'
                echo '查看容器'
                sh 'docker ps -a'
                echo '构建镜像'
                sh '''cd ${repositoryName}/
                    docker build -t ${imageName}:${imageTag} .
                '''
            }
        }
        stage('执行测试') {
    
    
            steps {
    
    
                echo '运行容器'
                sh 'docker run -i --name=${containerName} ${imageName}:${imageTag}'
            }
        }
        stage('后置工作') {
    
    
            steps {
    
    
                echo '保存报告'
                sh 'docker cp ${containerName}:/code/reports ./${repositoryName}/reports'
                echo '删除容器'
                sh 'docker rm ${containerName}'
                echo '删除镜像'
                sh 'docker rmi "${imageName}:${imageTag}"'
            }
        }
    }
    post {
    
    
        success {
    
    
            echo '查看镜像'
            sh 'docker images'
            echo '查看镜像'
            sh 'docker ps -a'
            echo '执行成功'
        }
        failure {
    
    
            echo '查看镜像'
            sh 'docker images'
            echo '查看镜像'
            sh 'docker ps -a'
            echo '执行失败'
        }
    }
}

4. Operation steps


1. Log in to Jenkins.



2. Create a pipeline project.



3. After clicking OK, fill in the pipeline script at Configuration->Pipeline.



4. After clicking Save, manually build the Jenkins project.



5. View the results.

Execution Status shows that the example project has run to completion.



The console output shows that the six test cases of the sample project passed.




If there are mistakes or improvements, please actively point them out!

Guess you like

Origin blog.csdn.net/embracestar/article/details/127222888