kind: pipeline # 定义对象类型，还有secret和signature两种类型
type: docker # 定义流水线类型，还有kubernetes、exec、ssh等类型
name: cicd # 定义流水线名称

clone:
  disable: true
  
steps: # 定义流水线执行步骤，这些步骤将顺序执行
  - name: clone
    image: alpine/git
    pull: if-not-exists
    environment:
      http_proxy: 
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
    commands:
    - git config --global core.compression 0
    - git clone https://github.com/dataelement/bisheng.git .
    - git checkout $DRONE_COMMIT

  - name: set poetry
    pull: if-not-exists
    image: golang
    environment:
      RELEASE_VERSION: 99.99.99
      NEXUS_PUBLIC:
        from_secret: NEXUS_PUBLIC
      NEXUS_PUBLIC_PASSWORD:
        from_secret: NEXUS_PUBLIC_PASSWORD
      REPO:
        from_secret: PY_NEXUS
      PROXY:
        from_secret: APT-GET
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: bisheng-cache
        path: /app/build/
    commands:
    - cd ./src/backend
    - echo $REPO
    - REPO2=$(echo $REPO | sed 's/http:\\/\\///g')
    - sed  '/apt-get/ s|$| '"$PROXY"'|' Dockerfile
    - sed -i.bak 's/uv cache clean.*$/ /' Dockerfile
    - sed -i '6i\RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - sed -i '7i\ENV UV_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - sed -i '8i\ENV UV_DEFAULT_INDEX=https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - sed -i '9i\ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - cat Dockerfile

  - name: build_docker
    pull: if-not-exists
    image: docker:24.0.6
    privileged: true
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: apt-cache
        path: /var/cache/apt/archives # 将应用打包好的Jar和执行脚本挂载出来
      - name: socket
        path: /var/run/docker.sock
      - name: pro-cache
        path: /root/.local/share/pypoetry
      - name: uv-cache
        path: /root/.cache/uv
    environment:
      http_proxy:
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
      no_proxy: 192.168.106.8
      version: release
      docker_registry: http://192.168.106.8:6082
      docker_repo: 192.168.106.8:6082/dataelement/bisheng-backend
      docker_user:
        from_secret: NEXUS_USER
      docker_password:
        from_secret: NEXUS_PASSWORD
    commands:
      - cd ./src/backend/
      - docker login -u $docker_user -p $docker_password $docker_registry
      - docker build -t $docker_repo:$version .
      - docker push $docker_repo:$version

  - name: build_docker_frontend
    pull: if-not-exists
    image: docker:24.0.6
    privileged: true
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: apt-cache
        path: /var/cache/apt/archives # 将应用打包好的Jar和执行脚本挂载出来
      - name: socket
        path: /var/run/docker.sock
    environment:
      http_proxy:
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
      no_proxy: 192.168.106.8
      version: release
      docker_registry: http://192.168.106.8:6082
      docker_repo: 192.168.106.8:6082/dataelement/bisheng-frontend
      docker_user:
        from_secret: NEXUS_USER
      docker_password:
        from_secret: NEXUS_PASSWORD
    commands:
      - cd ./src/frontend/
      - docker login -u $docker_user -p $docker_password $docker_registry
      - docker build -t $docker_repo:$version .
      - docker push $docker_repo:$version

  - name: ssh deploy
    image: appleboy/drone-ssh
    pull: if-not-exists
    settings:
      host: 192.168.106.116
      username: root
      password:
        from_secret: sshpwd
      script:
       - echo =======找到目录=======
       - cd /opt/server/bisheng-test
       - echo =======直接启动=======
       - docker compose pull
       - docker compose up -d

  - name: notify-start # notify
    pull: if-not-exists
    image: plugins/webhook
    settings:
      debug: true
      urls: 
        from_secret: FEISHU_URL
      content_type: application/json
      template: |
        {
          "msg_type": "interactive",
          "card": {
            "type": "template",
            "data": {
              "template_id": "AAqkI9bnY5FUs",
              "template_variable": {
                "repo_name": "{{ repo.name }}",
                "build_branch": "{{build.branch}}",
                "build_author": "{{ DRONE_COMMIT_AUTHOR }}",
                "link": "{{build.link}}",
                "commit_msg": "{{ trim build.message }}",
                "build_tag":"{{build.tag}}",
                "build_start":"{{build.started}}",
                "status": "{{ build.status }}"
              }
            }
          }
        }
    when: # 成功
      status:
        - success
trigger:
  branch:
  - release
  event:
  - push

volumes:
- name: bisheng-cache
  host:
    path: /opt/drone/data/bisheng/
- name: pro-cache
  host:
    path: /opt/drone/data/pro/
- name: apt-cache
  host:
    path: /opt/drone/data/bisheng/apt/
- name: socket
  host:
    path: /var/run/docker.sock


---

kind: pipeline # 定义对象类型，还有secret和signature两种类型
type: docker # 定义流水线类型，还有kubernetes、exec、ssh等类型
name: feat_cicd # 定义流水线名称

clone:
  disable: true

steps: # 定义流水线执行步骤，这些步骤将顺序执行
  - name: clone
    image: alpine/git
    pull: if-not-exists
    environment:
      http_proxy:
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
    commands:
    - git config --global core.compression 0
    - git clone https://github.com/dataelement/bisheng.git .
    - git checkout $DRONE_COMMIT

  - name: set poetry
    pull: if-not-exists
    image: golang
    environment:
      NEXUS_PUBLIC:
        from_secret: NEXUS_PUBLIC
      NEXUS_PUBLIC_PASSWORD:
        from_secret: NEXUS_PUBLIC_PASSWORD
      REPO:
        from_secret: PY_NEXUS
      PROXY:
        from_secret: APT-GET
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: bisheng-cache
        path: /app/build/
    commands:
    - cd ./src/backend
    - echo $REPO
    - REPO2=$(echo $REPO | sed 's/http:\\/\\///g')
    - sed  '/apt-get/ s|$| '"$PROXY"'|' Dockerfile
    - sed -i '6i\RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - sed -i '7i\RUN export UV_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple' Dockerfile
    - cat Dockerfile

  - name: build_docker
    pull: if-not-exists
    image: docker:24.0.6
    privileged: true
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: apt-cache
        path: /var/cache/apt/archives # 将应用打包好的Jar和执行脚本挂载出来
      - name: socket
        path: /var/run/docker.sock
      - name: pro-cache
        path: /root/.local/share/pypoetry
    environment:
      http_proxy:
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
      no_proxy: 192.168.106.8
      version: ${DRONE_BRANCH}
      docker_registry: http://192.168.106.8:6082
      docker_repo: 192.168.106.8:6082/dataelement/bisheng-backend
      docker_user:
        from_secret: NEXUS_USER
      docker_password:
        from_secret: NEXUS_PASSWORD
    commands:
      - echo "old tag is $version"
      - version=$(echo $version | sed 's/\\//_/g')
      - echo "build image tag is $version"
      - cd ./src/backend/
      - docker login -u $docker_user -p $docker_password $docker_registry
      - docker build -t $docker_repo:$version .
      - docker push $docker_repo:$version

  - name: build_docker_frontend
    pull: if-not-exists
    image: docker:24.0.6
    privileged: true
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: apt-cache
        path: /var/cache/apt/archives # 将应用打包好的Jar和执行脚本挂载出来
      - name: socket
        path: /var/run/docker.sock
    environment:
      http_proxy:
        from_secret: PROXY
      https_proxy:
        from_secret: PROXY
      no_proxy: 192.168.106.8
      version: ${DRONE_BRANCH}
      docker_registry: http://192.168.106.8:6082
      docker_repo: 192.168.106.8:6082/dataelement/bisheng-frontend
      docker_user:
        from_secret: NEXUS_USER
      docker_password:
        from_secret: NEXUS_PASSWORD
    commands:
      - echo "old tag is $version"
      - version=$(echo $version | sed 's/\\//_/g')
      - echo "build image tag is $version"
      - cd ./src/frontend/
      - docker login -u $docker_user -p $docker_password $docker_registry
      - docker build -t $docker_repo:$version .
      - docker push $docker_repo:$version

  - name: notify-start # notify
    pull: if-not-exists
    image: plugins/webhook
    settings:
      debug: true
      urls:
        from_secret: FEISHU_URL
      content_type: application/json
      template: |
        {
          "msg_type": "interactive",
          "card": {
            "type": "template",
            "data": {
              "template_id": "AAqkI9bnY5FUs",
              "template_variable": {
                "repo_name": "{{ repo.name }}",
                "build_branch": "{{build.branch}}",
                "build_author": "{{ DRONE_COMMIT_AUTHOR }}",
                "link": "{{build.link}}",
                "commit_msg": "{{ trim build.message }}",
                "build_tag":"{{build.tag}}",
                "build_start":"{{build.started}}",
                "status": "{{ build.status }}"
              }
            }
          }
        }
    when: # 成功
      status:
        - success
trigger:
  branch:
  - add_some_branch_you_need
  event:
  - push
   
volumes:
- name: bisheng-cache
  host:
    path: /opt/drone/data/bisheng/
- name: pro-cache
  host:
    path: /opt/drone/data/pro/
- name: apt-cache
  host:
    path: /opt/drone/data/bisheng/apt/
- name: socket
  host:
    path: /var/run/docker.sock
