自动化部署(python版)gitea + jenkins + docker

线路: gitea + jenkins + docker

部署:
1-panel - docker_compose.yaml

version: '3'
services:
  1panel:
    image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/moelin/1panel:latest
    container_name: 1panel
    ports:
      - "10086:10086"
      # - "443:443"
      # - "9443:9443"
    volumes:
      - 1panel-data:/opt/1panel/data
      - /var/run/docker.sock:/var/run/docker.sock
    restart: always

volumes:
  1panel-data:    
# docker版本
# docker exec -it 326d5304c338 /bin/bash
# root@326d5304c338:/# 1pctl user-info
# 面板地址: http://$LOCAL_IP:10086/entrance 
# 面板用户:  1panel
# 面板密码:  1panel_password
# 提示:修改密码可执行命令:1pctl update password
# root@326d5304c338:/# 

# 本地版本
# https://1panel.cn/docs/
# curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh

# sudo 1pctl user-info

# 操作
# Usage:
#   1pctl [COMMAND] [ARGS...]
#   1pctl --help

# Commands: 
#   status              查看 1Panel 服务运行状态
#   start               启动 1Panel 服务
#   stop                停止 1Panel 服务
#   restart             重启 1Panel 服务
#   uninstall           卸载 1Panel 服务
#   user-info           获取 1Panel 用户信息
#   listen-ip           切换 1Panel 监听 IP
#   version             查看 1Panel 版本信息
#   update              修改 1Panel 系统信息
#   reset               重置 1Panel 系统信息
#   restore             恢复 1Panel 服务及数据



#   Usage:
#   1pctl reset [COMMAND] [ARGS...]
#   1pctl reset --help

# Commands: 
#   domain              取消 1Panel 访问域名绑定
#   entrance            取消 1Panel 安全入口
#   https               取消 1Panel https 方式登录
#   ips                 取消 1Panel 授权 IP 限制
#   mfa                 取消 1Panel 两步验证


#   Usage:
#   1pctl listen-ip [COMMAND] [ARGS...]
#   1pctl listen-ip --help

# Commands: 
#   ipv4                监听 IPv4
#   ipv6                监听 IPv6


# Usage:
#   1pctl update [COMMAND] [ARGS...]
#   1pctl update --help

# Commands: 
#   username            修改面板用户
#   password            修改面板密码
#   port                修改面板端口


#   Usage:
#   1panel app [COMMAND] [ARGS...]
#   1panel app --help

# Commands: 
#   init                初始化应用



#   1panel app init -k app_name -v v1.0.0

目录:

Dockerfile

Jenkinsfile

build.sh

project/*

requirement.txt

Dockerfile

# 使用Ubuntu作为基础镜像
FROM ubuntu:20.04

# 设置上海时区
ENV TZ=Asia/Shanghai
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime 

# 安装基础软件和Python环境
RUN apt-get update && \
    apt-get install -y python3 python3-pip gcc vim curl wget net-tools && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 设置pip镜像源
RUN pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 设置工作目录
WORKDIR /app

# 复制requirements文件
COPY requirements.txt .

# 安装依赖
RUN pip3 install --no-cache-dir -r requirements.txt

# 复制应用代码
COPY . .

# 暴露端口
EXPOSE 5000

# 运行应用
CMD ["python3", "app.py"]

Jenkinsfile

pipeline {
    agent any
    // jenkins传递的环境变量 通过 params.XX
    parameters {
        string(name: 'APP_VERSION', defaultValue: '1.0.0', description: '应用程序版本号')
        booleanParam(name: 'RUN_TESTS', defaultValue: true, description: '是否运行测试')
        choice(name: 'DEPLOY_ENV', choices: ['dev', 'staging', 'prod'], description: '部署环境')
        booleanParam(name: 'NEED_LOGIN', defaultValue: false, description: '是否需要登录 Docker Hub')
    }

    environment {
        IMAGE_NAME = "my-flask-app"
        DOCKER_REPO = "127.0.0.1:5000"  // 去掉 http://
        BUILD_TIME = sh(script: 'date +"%Y%m%d%H%M"', returnStdout: true).trim()
    }

    stages {
        stage('Clean Up') {
            steps {
                script {
                    // 清理旧的 Docker 镜像和容器
                    sh '''
                        docker system prune -f || true
                        docker rmi $(docker images -q ${IMAGE_NAME}) || true
                    '''
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    echo "Building Docker image with tag ${BUILD_TIME}"
                    sh """
                        docker build --no-cache -t ${IMAGE_NAME}:${BUILD_TIME} .
                    """
                }
            }
        }

        stage('Push') {
            steps {
                script {
                    if (params.NEED_LOGIN) {
                        withCredentials([usernamePassword(credentialsId: 'docker-hub', 
                            usernameVariable: 'DOCKER_USER', 
                            passwordVariable: 'DOCKER_PASS')]) {
                            sh """
                                docker login -u $DOCKER_USER -p $DOCKER_PASS
                            """
                        }
                    }
                    echo "Pushing image to repository..."
                    sh """
                        docker tag ${IMAGE_NAME}:${BUILD_TIME} ${DOCKER_REPO}/${IMAGE_NAME}:${BUILD_TIME}
                        docker tag ${IMAGE_NAME}:${BUILD_TIME} ${DOCKER_REPO}/${IMAGE_NAME}:latest
                        docker push ${DOCKER_REPO}/${IMAGE_NAME}:${BUILD_TIME}
                        docker push ${DOCKER_REPO}/${IMAGE_NAME}:latest
                    """
                    if (params.NEED_LOGIN) {
                        sh "docker logout"
                    }
                }
            }
        }
    }

    post {
        always {
            // 无论成功失败都执行清理
            sh 'docker system prune -f || true'
        }
        success {
            echo 'Pipeline completed successfully!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
}

build.sh

#!/bin/bash

# 获取当前时间,格式为YYYYMMDDHHMM
CURRENT_TIME=$(date +"%Y%m%d%H%M")

# 设置变量
IMAGE_NAME="my-flask-app"
TAG="$CURRENT_TIME"  # 使用时间作为标签
DOCKER_REPO="127.0.0.1:5000"

# 构建Docker镜像
echo "Building Docker image..."
docker build -t $IMAGE_NAME:$TAG . || {
    echo "Failed to build Docker image"
    exit 1
}

# 标记镜像
echo "Tagging Docker image..."
docker tag $IMAGE_NAME:$TAG $DOCKER_REPO/$IMAGE_NAME:$TAG || {
    echo "Failed to tag Docker image"
    exit 1
}

# 推送镜像到仓库
echo "Pushing Docker image..."
docker push $DOCKER_REPO/$IMAGE_NAME:$TAG || {
    echo "Failed to push Docker image"
    exit 1
}

# 返回完整的镜像路径
FULL_IMAGE_PATH="${DOCKER_REPO}/${IMAGE_NAME}:${TAG}"
echo "Build and push completed successfully!"
echo "Image path: ${FULL_IMAGE_PATH}"

# 返回镜像路径
echo "${FULL_IMAGE_PATH}"

requirement.txt

Flask==2.0.1
posted @ 2025-04-27 11:17  派森的猫  阅读(97)  评论(0)    收藏  举报