Linux(CentOS7)で、ASP.NET Coreアプリのビルド自動化(2)
前回の続きです。必要なツール類を、概ねインストールできました。
今回は、ツールの初期設定から連携動作確認まで持っていきます。
Gitを入れてなかった
GitBucketでGitを受け付けるサーバは入ったのですが、コマンドラインツールのgit
を入れるのを忘れてました。
Jenkinsが使うので、入れときます。
[root@centos7 ~]# yum -y install git 読み込んだプラグイン:fastestmirror Loading mirror speeds from cached hostfile --- 中略 --- rsync.x86_64 0:3.0.9-17.el7 完了しました!
CentOS7側の hosts を編集
今のところCentOS7は検証環境なので、DNSによる名前解決ができません。
前回の開発用Windows機と同様に、/etc/hosts
にドメイン名を書き込んでおきます。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 # --- 下記行を追記 --- 127.0.0.1 dotnet.centos7.local git.centos7.local jenkins.centos7.local
名前解決、出来てますかね?
[root@centos7 ~]# ping dotnet.centos7.local PING dotnet.centos7.local (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.019 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.055 ms ^C --- dotnet.centos7.local ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.019/0.037/0.055/0.018 ms [root@centos7 ~]# ping git.centos7.local PING dotnet.centos7.local (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.019 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.055 ms ^C --- dotnet.centos7.local ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.019/0.037/0.055/0.018 ms [root@centos7 ~]# ping jenkins.centos7.local PING dotnet.centos7.local (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.021 ms ^C --- dotnet.centos7.local ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.021/0.021/0.021/0.000 ms [root@centos7 ~]# ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.021 ms ^C --- localhost ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.021/0.021/0.021/0.000 ms
大丈夫ですね。
GitBucketのセットアップ
GitBucketに、ASP.NET Coreアプリ用のリポジトリを作っておきます。
git.centos7.local
にアクセスして、ログインします。
まずは初期ユーザー root でログイン。
初期状態のパスワードは root です。
このあと、
などなどをやりました。
開発用Windows機上の ASP.NET Coreプロジェクトファイル一式を、Gitリポジトリに追加します。
GitBucket上でも、追加されたファイルが確認できますね。
Jenkinsのセットアップ
次はJenkins。
インストール手順は、ほぼこちらにあるとおりをなぞります。
jenkins.centos7.local
にアクセスします。
初回アクセス時には、アンロック用文字列を入力が必要です。
画面の赤字のパスにあるファイルを開いて、文字列をコピペします。
[root@centos7 ~]# cat /home/webroot/jenkins/data/secrets/initialAdminPassword --- アンロック用文字列が、記号なし小文字のみの英数で表示されます ---
次はプラグインのインストール内容を選びます。
お勧め一式を入れてくれるそうなので、左側の青いボタンを選びます。
プラグインのインストール進捗が全て終わると、管理者ユーザー設定になります。
初期設定が完了すると、ダッシュボードが表示されます。
GitBucket連携用プラグインをインストールするため、「Jenkinsの管理」へ。
リバースプロキシの警告が出ちゃってるんですが、どうにも直りませんでした。
ぐぐっても、JenkinsURLのポート指定解除くらいしか出てこない...?
放っといて、「プラグインの管理」に行きます。
利用可能タブを開いて、フィルターに"Gitb"と入力すると。
出てきましたね、「GitBucket Plugin」
チェックを入れて「再起動せずにインストール」を選びます。
成功したようですね。
参考:
JenkinsとGitBucketを連携し、pushされたら自動的にソースを取得する(GitBucket Plugin)
Jenkinsにジョブを追加する
やることが無くてひまぷーなJenkinsさんに、早速お仕事を教えてあげましょう。
ふふっ、世の中は厳しいんだぜ。
改めてダッシュボードに戻り、「新規ジョブ作成」を選びます。
ジョブ名称を入力して、「フリースタイル・プロジェクトのビルド」を選びます。
ジョブの詳細設定を入力する画面になりました。
「GitBucket」セクションで、上で作ったリポジトリのトップURL http://git.centos7.local/05rw/dotnet_sample
を入力します。
「ソースコード管理」セクションで、同リポジトリのクローン用URL http://git.centos7.local/git/05rw/dotnet_sample.git
を入力します。
エラー表示が出ます。「Authentication failed」とのこと。
認証情報も追加しましょう。
Credentials項目の「追加」メニューを押し、Jenkins を選びます。
設定項目が沢山並んでますが、ユーザー名とパスワードだけ入力します。
あれー?エラー消えてない?
追加した Credentials は、自動で選択してくれないようですね。
「ビルド・トリガ」セクションで、「Build when a change is pushed ot GitBucket」にチェックを入れます。
「ビルド」セクションで、「ビルド手順の追加」メニューから「シェルの実行」を選びます。
シェルスクリプトを入力します。
dotnet
コマンドrestore
, build
, publish
のリファレンスを参考に、スクリプトを書きます。
build
, publish
には記述がありませんが、restore
と同様に第一引数にプロジェクトパスをセットできるようです。
Jenkinsはジョブを実行すると、[Jenkinsデータフォルダ]/[ジョブ名]/
の下に、Gitリポジトリのファイル一式を展開します。
スクリプトで指定するパスは、そのあたりを念頭に置いて書きます。
dotnet restore /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ dotnet build /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ systemctl stop dotnet_sample dotnet publish /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ --configuration Release --output /home/webroot/dotnet_sample/bin systemctl start dotnet_sample
やれやれ、やっとジョブ情報を入力しおわりました。
保存して戻ります。
ジョブが出来ましたね!
「ビルド実行」を試してみます。
左下のビルド履歴に進捗が表示され、しばらく待つと履歴行が増えていますね。
記念すべき初回ビルド#1
の詳細内容は、ビルド履歴行の左端の〇
をクリックすると出てきます。
ユーザー05rwが実行 ビルドします。 ワークスペース: /home/webroot/jenkins/data/workspace/dotnet_sample_build > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://git.centos7.local/git/05rw/dotnet_sample.git # timeout=10 Fetching upstream changes from http://git.centos7.local/git/05rw/dotnet_sample.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress http://git.centos7.local/git/05rw/dotnet_sample.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision c303273cfc10f328edfb4ab4d97cc965b949592c (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f c303273cfc10f328edfb4ab4d97cc965b949592c > git rev-list c303273cfc10f328edfb4ab4d97cc965b949592c # timeout=10 [dotnet_sample_build] $ /bin/sh -xe /tmp/hudson9163061906313740256.sh + dotnet restore /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ log : Restoring packages for /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.json... log : Restoring packages for tool 'BundlerMinifier.Core' in /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.json... log : Restoring packages for tool 'Microsoft.AspNetCore.Razor.Tools' in /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.json... log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.json... log : Writing lock file to disk. Path: /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.lock.json log : /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/project.json log : Restore completed in 9050ms. + dotnet build /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ Project WebApplication1 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing Compiling WebApplication1 for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:02.1568893 + systemctl stop dotnet_sample + dotnet publish /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/ --configuration Release --output /home/webroot/dotnet_sample/bin Publishing WebApplication1 for .NETCoreApp,Version=v1.0 bower ESUDO Cannot be run with sudo Additional error details: Since bower is a user command, there is no need to execute it with superuser permissions. If you're having permission errors when using bower without sudo, please spend a few minutes learning more about how your system should work and make any necessary repairs. http://www.joyent.com/blog/installing-node-and-npm https://gist.github.com/isaacs/579814 You can however run a command with sudo using --allow-root option Bundling with configuration from /home/webroot/jenkins/data/workspace/dotnet_sample_build/src/WebApplication1/bundleconfig.json Processing wwwroot/css/site.min.css Bundled Minified Processing wwwroot/js/site.min.js Bundled Project WebApplication1 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing Compiling WebApplication1 for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:02.0746785 Configuring the following project for use with IIS: '/home/webroot/dotnet_sample/bin' Updating web.config at '/home/webroot/dotnet_sample/bin/web.config' Configuring project completed successfully publish: Published to /home/webroot/dotnet_sample/bin Published 1/1 projects successfully + systemctl start dotnet_sample Finished: SUCCESS
参考:
dotnet-restore
dotnet-build
dotnet-publish
GitBucketがJenkinsをキックするように
Jenkinsの手動ビルドはできたんですが、まだです。
WebHookを利用して、GitBucketからJenkinsに通知を送るようにしないと、やはりJenkinsさんはひまぷーなままです。
Jenkinsが受け取るWebHookURLは、ジョブ設定
→ビルド・トリガ
のヘルプメッセージに書いてあります。
http://your.jenkins.host/jenkins/gitbucket-webhook/
とな。
URLの中の/jenkins/
はデフォルトのプレフィクスなので、ウチでは使いません。
設定するURLは、http://jenkins.centos7.local/gitbucket-webhook/
となりますね。
GitBucketに戻ります。
左ペインのdotnet_sample
リポジトリを開きます。
リポジトリ詳細画面の左ペインから、Settinsg
を開きます。
右ペインがリポジトリ設定に切り替わるので、Service Hooks
タブを開いて「Add Webhook」を押します。
WebHook編集画面が出てきます。
PayloadURL
へ、先ほどのJenkins側WebHookURLを入力します。
その他はデフォルトですね。
「Add Webhook」ボタンを押すと、WebHookが作られました。
自動化されたか、試してみる
さて、Jenkinsさんがキチンと労働しているか、確かめてみましょう!
現状のdotnet.centos7.local
の表示は、こんな感じ。
Visual Studioが作ったテンプレートそのままです。
開発用Windows機の方で、このプロジェクトを開いて編集してみましょう。
Visual Studio 2015 Update3 で、プロジェクトのViews
/Shared
/_Layout.cshtml
を開きます。
<body>
直下に、<div>
を差し込んでみました。
保存してコミット&プッシュします。
おや、Jenkinsさんに反応が。
ブラウザから、ASP.NET Coreアプリを見てみます。
なんかテキストが、割り込んでますね!!!
長かったですが、これでやっと!
WindowsのVisual Studioを操作するだけで、Linux上のASP.NET Coreアプリが更新されるようになりました。
お手軽に、なったぞー!!
今回分のソースコードはこちら。
Jenkinsによる自動ビルド確認のため、ViewのHTMLを書き換えています。
github.com