kind: pipeline
type: docker
name: fateverse
platform:
  os: linux
  arch: arm64


steps:
  - name: build-jar # 流水线名称
    image: maven:3.8.5-openjdk-11 # 定义创建容器的Docker镜像,maven:3.8.5-openjdk-8用于对java进行打包使用
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: maven-cache
        path: /root/.m2 # 将maven下载依赖的目录挂载出来，防止重复下载
      - name: maven-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
      - name: skywalking-agent
        path: /app/build/skywalking-agent
    commands: # 定义在Docker容器中执行的shell命令
      - sed -i 's/$DRONE_COMMIT_BRANCH/'"$DRONE_COMMIT_BRANCH"'/' start.sh
      - bash maven.sh

  - name: build-docker # 制作docker镜像
    image: docker # 使用官方docker镜像
    volumes: # 将容器内目录挂载到宿主机
      - name: maven-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
      - name: docker
        path: /var/run/docker.sock # 挂载宿主机的docker
    environment: # 获取到密文的docker用户名和密码
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
      REGISTRY:
        from_secret: registry
      REGISTRY_NAMESPACE:
        from_secret: registry_namespace
    commands: # 定义在Docker容器中执行的shell命令
      - cd /app/build/$DRONE_COMMIT_BRANCH
      - sed -i 's/$REGISTRY/'"$REGISTRY"'/' deployment.yml
      - sed -i 's/$REGISTRY_NAMESPACE/'"$REGISTRY_NAMESPACE"'/' deployment.yml
      - sed -i 's/$DRONE_COMMIT_BRANCH/'"$DRONE_COMMIT_BRANCH"'/' start.sh
      - sed -i 's/$DRONE_COMMIT_BRANCH/'"$DRONE_COMMIT_BRANCH"'/' Dockerfile
      - sed -i 's/$DRONE_COMMIT_BRANCH/'"$DRONE_COMMIT_BRANCH"'/' .dockerignore
      - sed -i 's/$DRONE_COMMIT_BRANCH/'"$DRONE_COMMIT_BRANCH"'/' deployment.yml
      - sed -i 's/$DRONE_COMMIT/'"$DRONE_COMMIT"'/' deployment.yml
      # docker登录,不能在脚本中登录,并且不能使用docker login -u -p
      - echo $DOCKER_PASSWORD | docker login $REGISTRY --username $DOCKER_USERNAME --password-stdin
      - chmod +x docker.sh
#      - cp -r /app/skywalking ./
      - sh docker.sh
      # 执行完脚本删除本次制作的docker镜像,避免多次后当前runner空间不足
      - docker rmi -f $(docker images | grep $DRONE_COMMIT_BRANCH | awk '{print $3}')


  - name: deploy # rancher运行
    image: bitnami/kubectl:1.26.13-debian-11-r1 #阿里云的kubectl镜像,里面包含kubectl命令行工具
    volumes: # 将容器内目录挂载到宿主机
      - name: maven-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
      - name: config
        path: /app/config # 将kubectl 配置文件挂载出来
    commands: # 定义在Docker容器中执行的shell命令
      - cd /app/build/$DRONE_COMMIT_BRANCH
      # 通过kubectl指令运行deployment.yml,并指定授权文件kubectl_conf.yml
      - kubectl apply -f deployment.yml -n fateverse --kubeconfig=/app/config/base-taishan-kubectl.yml



volumes:
  - name: maven-build
    host:
      path: /home/build/fateverse
  - name: skywalking-agent
    host:
      path: /home/build/skywalking-agent
  - name: config # k8s对接的配置文件
    host:
      path: /home/kubect
  - name: maven-cache
    host:
      path: /home/data/maven/cache
  - name: docker
    host:
      path: /var/run/docker.sock
# 定义触发条件
trigger:
  branch:
    - gateway
    - auth
    - admin-biz
    - notice-biz
    - log-biz
    - monitor
    - code-gen
    - custom-query-biz
    - sentinel-dashboard
    - workflow

  event:
    - push