[Cake] 2. dotnet 全局工具 cake


在上篇博客[Cake] 1. CI中的Cake中介紹了如何在CI中利用Cake來保持與CI/CD環境的解耦。

1. 簡化cake的安裝

當時dotnet 2.1還未正式發布,dotnet 還沒有工具的支持,使得安裝cake非常麻煩。不過隨着 dotnet tool 的加入,這一問題得到了很好的解決。目前安裝cake(0.30.0版本之后)只需要一行命令即可:

1 dotnet tool install -g cake.tool

然后就可以使用cake了。用 dotnet cake 或者 dotnet-cake 都可以,推薦使用前者。

 1 $ dotnet cake --help
 2 
 3 Usage: Cake.exe [script] [--verbosity=value]
 4                 [--showdescription] [--dryrun] [..]
 5 
 6 Example: Cake.exe
 7 Example: Cake.exe build.cake --verbosity=quiet
 8 Example: Cake.exe build.cake --showdescription
 9 
10 Options:
11     --verbosity=value    Specifies the amount of information to be displayed.
12                          (Quiet, Minimal, Normal, Verbose, Diagnostic)
13     --debug              Performs a debug.
14     --showdescription    Shows description about tasks.
15     --showtree           Shows the task dependency tree.
16     --dryrun             Performs a dry run.
17     --exclusive          Execute a single task without any dependencies.
18     --bootstrap          Download/install modules defined by #module directives
19     --version            Displays version information.
20     --info               Displays additional information about Cake execution.
21     --help               Displays usage information.

2. 簡化cake的引導腳本

上一篇博客[Cake] 1. CI中的Cake中出現的cake的引導腳本 build.ps1 和 build.sh ,絕大部分代碼都是在下載安裝cake用的,既然有了上面的 dotnet tool 命令可以安裝cake,那么當然也就可以簡化一下了。

引導腳本中包含安裝和執行命令的代碼。nuget相關的環境變量是項目需要的,cake腳本可以讀取這些信息來使用。

2.1 cake.ps1

 1 [string]$SCRIPT       = '0-build/build.cake'
 2 [string]$CAKE_VERSION = '0.33.0'
 3 
 4 # nuget server config
 5 $ENV:NUGET_REPOSITORY_API_URL = "http://nuget-server.test/nuget"
 6 $ENV:NUGET_REPOSITORY_API_KEY = "123456"
 7 
 8 # Install cake.tool
 9 dotnet tool install --global cake.tool --version $CAKE_VERSION
10 
11 # Start Cake
12 [string]$CAKE_ARGS = "-verbosity=diagnostic"
13 
14 Write-Host "dotnet cake $SCRIPT $CAKE_ARGS $ARGS" -ForegroundColor GREEN
15 
16 dotnet cake $SCRIPT $CAKE_ARGS $ARGS

查看一下cake腳本都有哪些task:

 1 $ .\cake.ps1 --showtree
 2 Tool 'cake.tool' is already installed.
 3 dotnet cake 0-build/build.cake -verbosity=diagnostic --showtree
 4 
 5 ....
 6 
 7 default
 8 └─test
 9    └─build
10       ├─clean
11       └─restore
12 
13 push
14 └─pack
15    └─test
16       └─build
17          ├─clean
18          └─restore

2.2 cake.sh

 1 #!/bin/sh
 2 
 3 SCRIPT='0-build/build.cake'
 4 CAKE_VERSION='0.33.0'
 5 
 6 # nuget server config
 7 export NUGET_REPOSITORY_API_URL='http://nuget-server.test/nuget'
 8 export NUGET_REPOSITORY_API_KEY='123456'
 9 
10 # Install  cake.tool
11 dotnet tool install --global cake.tool --version $CAKE_VERSION
12 export PATH="$PATH:$HOME/.dotnet/tools"
13 
14 # Start Cake
15 CAKE_ARGS="$SCRIPT -verbosity=diagnostic"
16 
17 echo "\033[32mdotnet cake $CAKE_ARGS $@"
18 
19 dotnet cake $CAKE_ARGS "$@"

3. CI/CD

CI/CD的yaml配置文件不用做調整,只需執行對 cake.sh 或者 cake.ps1 的調用即可。這也是cake帶來的避免在CI/CD中編程的好處,所有的邏輯都位於cake腳本中。

參考

dotnet tool https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-tool-install

cake 示例項目 https://github.com/linianhui/cake.example

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM