4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Microsoft] Azure Pipelinesビルド環境を自前で用意(セルフホスト)する

Last updated at Posted at 2019-10-17

はじめに

Azure Pipelines は、自前で環境を用意しなくても、Microsoftが用意している環境を使ってビルド&デプロイできます。
ただ、プライベートなプロジェクトの場合は利用時間に上限(1800分/月)があり、長いテストを走らせたりしていると月半ばで上限に達してしまうことがあります。

自前で環境を用意(セルフホスト)すれば、この上限がなくなります。

手順

公式ドキュメント にはわかりづらいところがあるので、あらためてまとめてみます。

事前準備

作業を始める前に、準備しておくものがあります。

  • PAT(Personal Access Token) →→→ DevOpsの画面から作成して取得します。
  • エージェント →→→ githubからダウンロードします。
  • エージェントインストール先の仮想マシンか物理マシン

OSは、ビルドしたいものに合わせます。
Linuxであれば、Ubuntu 18.04 がオススメです。19.04 は未サポートです。

エージェントのインストール

事前準備ができたら、対象マシンにエージェントをインストールします。

全体の概略は以下の通りです。

  1. エージェントを動かすユーザを作成
  2. エージェントをインストール
  3. エージェントを設定
  4. エージェントをサービスとして動かす

ひとつずつ見ていきます。

エージェントを動かすユーザを作成

以下の要件でユーザを作成しました。

設定項目
GID 660
グループ名 vstsagent
UID 660
ユーザー名 vstsagent
シェル /usr/sbin/nologin
ホームディレクトリ /opt/vstsagent
sudo useradd --uid 660 --system --shell /usr/sbin/nologin --home-dir /opt/vstsagent --create-home vstsagent

エージェントをインストール

ファイルをホームディレクトリに展開します。
ホームディレクトリ必須です。

sudo -u vstsagent tar zxf vsts-agent-linux-x64-2.159.0.tar.gz -C /opt/vstsagent

エージェントを設定

展開したファイル中にあるconfig.shを実行します。

cd /opt/vstsagent
sudo -u vstsagent ./config.sh

いくつか質問が表示されるので答えていきます。

質問項目
Server URL https://dev.azure.com/****** <== 実際はDevOps組織のURL
Authentication Type 既定 (PAT)
Personal Access Token 事前準備で取得したもの
Agent Pool DevOps組織の中にあるAgent Pool名(defaultでOK)
Agent name 表示されるエージェント名(既定でOK)
Work folder /mnt/vstswork1

エージェントをサービスとして動かす

ユーザー vstsagent で動かすようにします。

sudo ./svc.sh install vstsagent

開発ツールのインストール

使用する開発ツールもインストールします。

  • build-essential (コンパイラ等)
  • Node
  • DotNet SDK
  • Azure CLI
  • ...等

落ち穂拾い

Azure VMにおける消えるリソースディスクへの対応

シャットダウン時に消えてしまうリソースディスク内に作業ディレクトリを置くことにしたので、起動時に作成するようにしました。

systemd用のファイルを作成します。

/lib/systemd/system/create-vsts-work-dir.service
[Unit]
Description=Create a work folder for the VSTS agent
After=cloud-config.service
ConditionVirtualization=microsoft
ConditionPathIsMountPoint=/mnt
ConditionPathExists=/dev/disk/azure/resource-part1

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/create-vsts-work-dir
RemainAfterExit=yes
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

起動時に動くようにします。

systemctl enable create-vsts-work-dir

実行するシェルスクリプトも作成します。

ファイル全体は https://github.com/sengokyu/create-vsts-work-dir へ置きました。

トラブルシューティング

node.jsがインストールされていないと言われる

問題: ビルドの途中で、node.jsがインストールされていないというエラーが出ます。
解決策?:thinking:: エージェントをホームディレクトリに展開したら大丈夫でした。

エラー "No agent found in pool Default which satisfies the specified demands: npm Agent.Version " が出力される

問題: Pipelineの中でnpmを使えません。
解決策?:thinking:: エージェントのバージョンを2.158から2.159にしたら動きました。

エラー "null: zip" と言われる

問題: パッケージやZIPタスクでエラーが起きます。
解決策: zipをインストールします。apt-get install zip

リンク


  1. 自分はAzure VMを用意したので、アクセスが速いリソースディスク(/mntにマウントされている)を使うようにしました。リソースディスクは再起動で消えるので、ディレクトリは別途起動時に作成する必要があります。 

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?