Drone構建失敗,一次drone依賴下載超時導致構建失敗的爬坑記錄

蒲公英的狂想發表於2021-02-07

Once upon a time, birds were singing in the forest, and people were dancing under the trees, It's so peaceful

1 發生

1.1 Drone簡介

Drone by Harness™ is a modern Continuous Integration platform that empowers busy teams to automate their build, test and release workflows using a powerful, cloud native pipeline engine.

1.2 Context

因為xxx所以Drone構建的所依賴的maven私服停服了,構建介面哀鴻遍野,於是更換構建映象,換上新的倉庫地址,森林彷彿回到了之前的平靜

隨著新的新的映象的更換,在構建記錄中會偶爾出現構建失敗的情況,build的時間也加長了一點,起初並沒有很關注這個問題,直到有一天這個問題一直repeat,版本一直推不上去,開發的功能遲遲不能提測,各種壓力撲面而來

1.3 現象描述

drone在執行build節點突然中斷,沒有任何報錯日誌,第一反應是Drone工具有問題,於是立馬在公司的IM上密相關負責人,他們定位日誌說應當是私服拉取超時,可能是私服那邊做了熔斷,這個私服全公司都在用,構建失敗需要聯絡私服那邊人一起定位去看,跨部門協同讓人頭大

drone構建是加了快取的,且之前一直沒有問題,為什麼突然就不行了,而且build日誌中為什麼一直在列印下載依賴的日誌,於是果斷的放棄跨多部門協同這條路,轉身來定位一下問題出現的原因

2 定位

2.1 Drone配置如下

# 工作空間
workspace:
   base: /app
   path: code

pipeline:
   #  恢復cache
   restore_cache:
      image: appleboy/drone-sftp-cache
      server: 172.31.xx.xx
      port: xx
      username: xxx
      password: xxx
      path: /home/mavencache/cache
      restore: true
      mount:
         - /app/.m2/
   build:
      # 這裡是更換的新的映象
      image: hub.xxx.com/xxx/cicd-xx:1.1
      when:
         event:
            - push
         branch: [dev*, release*, emergency, hotfix*, master]
      commands:
         # 初始化目錄,防止沒有
         - mkdir -p /app/.m2/
         - export NAME=web-center-v4
         - export APP_ID=5ee0528eb090259fe0056ac0
         - echo $NAME > NAME
         - echo $(jx-release-version)-$DRONE_COMMIT_BRANCH.$DRONE_BUILD_NUMBER > VERSION
         - cat VERSION
         - export COMMIT_ID=$DRONE_COMMIT
         - export COMMIT_BRANCH=$DRONE_COMMIT_BRANCH
         - export BUILD_NUMBER=$DRONE_BUILD_NUMBER
         - cd charts  && sh upload.sh && cd ..
         - mvn versions:set -DnewVersion=$(cat VERSION)
         - mvn package -DskipTests
         - echo $(cat VERSION) > .tags

2.2 除錯

drone構建的節點是可以執行linux命令的,於是在commonds下加了一句 mvn -X,輸出擷取如下

……
[DEBUG] Reading global settings from /opt/apache-maven-3.5.3/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml
[DEBUG] Reading global toolchains from /opt/apache-maven-3.5.3/conf/toolchains.xml
[DEBUG] Reading user toolchains from /root/.m2/toolchains.xml
[DEBUG] Using local repository at /root/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /root/.m2/repository
……

可以看到 settings.xml 的位置以及 localRepository 的位置,這個和我們配置的workspace不一致
我們配置的是 /app/.m2,怎麼會走預設的路徑?於是 cat /root/.m2/settings.xml 走起

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository  -->
</settings>

這裡問題已經比較明顯的,這個settings.xml在本地Repository這一塊是預設配置,所以所有的快取都沒有生效

2.3 嘗試解決

嘗試1:手動編寫了一個setting檔案,增加命令 cp settings.xml /root/.m2/,提交構建速度恢復正常

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <!-- 設定本地倉庫的地址 -->
  <localRepository>/app/.m2/</localRepository> 
</settings>

嘗試2:修改快取掛載路徑為/root/.m2/,提交構建速度恢復正常

2.4 問題回頭看

一個疑問,這樣的修改雖然能夠解決,但是為什麼之前都沒有問題?於是我映象改為之前的,再次執行cat /root/.m2/settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <!-- localRepository
     | The path to the local repository maven will use to store artifacts.
     |
     | Default: ${user.home}/.m2/repository  -->
    <localRepository>/app/.m2</localRepository>
</settings>

這下問題實錘了,是新映象問(的)題(鍋),生成的settings.xml沒有讀取workspace的配置資訊

3 解決方案

不細說了,找領導修改構建映象,祝我好運

相關文章