前回のポストで自前の端末を Azure DevOps のパイプラインへ登録する方法を記載しましたが、実はアレだけだと git がインストールされていないのでソースコードが端末から取得できず、当たり前ですが Java も Cento OS にインストールされていないのでビルド時にエラーが発生します。ハッキリ言って Azure DevOps pipeline にただ登録しただけの状態になっています。
normalian.hatenablog.com
今回は端末を Java のビルドで利用できるようにセットアップしていきます。Java のビルドには Ant や Gradle が有名ですが、今回は Maven を試したいと思います。実は Azure DevOps には以下の様に built-in の Maven タスクが存在します。
learn.microsoft.com
こちらを利用すれば Azure DevOps Pipeline でそのまま Maven ビルドを実行できるように思いますが、Azure DevOps エージェントをセットアップした自端末の CentOS マシンには Java がインストールされていないので、pipeline にて Maven タスクを実行すると以下というエラーメッセージが pipeline 内の結果に表示されます。こちらは読みやすいように改行しているのでご注意ください。
##[error]No agent found in pool "your agent pool name" which satisfies the following demand: agent.name. All demands: agent.name -equals "your specified machine name", maven, Agent.Version -gtVersion 2.182.1
エラーメッセージを御覧になれば分かるように Maven がインストールされていないことに加え、Azure DevOps エージェントのバージョンも一定以上である必要が分かると思います。今回は前回のセットアップに加え、Maven のセットアップを追加しています。今回の記事でのスクリプトは、前回の記事で実施済のエージェントセットアップを含んでいるので、適宜読みかえて頂ければ幸いです。
Maven タスクを実行するためのスクリプトと Azure Pipeline 定義ファイル
まずは自分が Cent OS 内で実施したスクリプトを以下に記載します。
# CentOS で git v2 をインストールするためのパッケージをインストール& git と maven のインストール sudo yum install -y https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm sudo yum install -y java maven git # Azure DevOps エージェントのセットアップ wget https://vstsagentpackage.azureedge.net/agent/2.210.1/vsts-agent-linux-x64-2.210.1.tar.gz mkdir myagent mv vsts-agent-linux-x64-2.210.1.tar.gz myagent/ cd myagent/ tar zxvf vsts-agent-linux-x64-2.210.1.tar.gz sudo ./bin/installdependencies.sh ./config.sh # JDK の配置&Azure Pipeline がアクセスできるように権限制御 sudo mkdir /builds/ wget https://builds.openlogic.com/downloadJDK/openlogic-openjdk/8u342-b07/openlogic-openjdk-8u342-b07-linux-x64.tar.gz mv openlogic-openjdk-8u342-b07-linux-x64.tar.gz /builds/ sudo mkdir -p /builds/binaries/externals wget https://builds.openlogic.com/downloadJDK/openlogic-openjdk/8u342-b07/openlogic-openjdk-8u342-b07-linux-x64.tar.gz sudo mv openlogic-openjdk-8u342-b07-linux-x64.tar.gz /builds/ sudo chown -R azureuser /builds/
次に Azure DevOps のパイプライン定義もそのまま記載します。こちらは CentOS 側の配置した JDK パッケージのパスを参照していることに注意ください。
# Maven # Build your Java project and run tests with Apache Maven. # Add steps that analyze code, save build artifacts, deploy, and more: # https://docs.microsoft.com/azure/devops/pipelines/languages/java trigger: - master pool: name: JavaBuildVM-Agents steps: - script: | java -version env: JAVA_HOME: $(JAVA_HOME_8_X64) PATH: $(JAVA_HOME_8_X64)/bin:$(PATH) - task: JavaToolInstaller@0 inputs: versionSpec: "8" jdkArchitectureOption: x64 jdkSourceOption: LocalDirectory jdkFile: "/builds/openlogic-openjdk-8u342-b07-linux-x64.tar.gz" jdkDestinationDirectory: "/builds/binaries/externals" cleanDestinationDirectory: true - task: Maven@3 inputs: mavenPomFile: 'pom.xml' mavenOptions: '-Xmx3072m' javaHomeOption: 'JDKVersion' jdkVersionOption: '1.8' jdkArchitectureOption: 'x64' publishJUnitResults: true testResultsFiles: '**/surefire-reports/TEST-*.xml' goals: 'package'
上記を踏襲すればすんなり動くと思いますが、如何にそれぞれのハマりどころの注意点を追記しています。
設定時の注意点① - git のバージョン
Cent OS で単に yum install git を実施した場合、v1 系の git がインストールされました。その場合、Azure DevOps pipeline 実行時に以下のエラーが発生します。
Starting: Checkout SampleJavaProject@master to s ============================================================================== Task : Get sources Description : Get sources from a repository. Supports Git, TfsVC, and SVN repositories. Version : 1.0.0 Author : Microsoft Help : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199) ============================================================================== Syncing repository: SampleJavaProject (Git) git version git version 1.8.3.1 ##[error]Min required git version is '2.0', your git ('/usr/bin/git') version is '1.8.3' Finishing: Checkout SampleJavaProject@master to s
上記を見れば分かる通り、git v2 系をインストールする必要があります。
設定時の注意点② - JDK のセットアップ
自端末に対して JDK のセットアップから開始する必要がありますが、これには 以下の Java Tool Installer タスクを利用します。自分で展開されたファイルや tar.gz/zip 等で JDK パッケージをどこかに配置する必要があります。
Java Tool Installer task - Azure Pipelines | Microsoft Learn
実は手抜きをしようとタスクの jdkSourceOption の pre-installed を利用しようとしたのですが、こちらは Microsoft 側が標準で提供されているエージェントプールでのみ利用可能なようです。そのため、スクリプトで記載したような tar.gz ファイルを設置しています。
加えて、JDK パッケージを配置したフォルダの権限管理にも注意が必要です。Azure DevOps pipeline の実行時に権限が不足すると以下の様なエラーが発生します。こちらについても対応方法はスクリプトを参照ください。
Starting: JavaToolInstaller ============================================================================== Task : Java tool installer Description : Acquire a specific version of Java from a user-supplied Azure blob or the tool cache and sets JAVA_HOME Version : 0.209.0 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/tool/java-tool-installer ============================================================================== ##[error]EACCES: permission denied, stat '/builds/binaries/externals' ##[error]EACCES: permission denied, stat '/builds/binaries/externals' Finishing: JavaToolInstaller