在上篇博客[Cake] 1. CI中的Cake中介绍了如何在CI中利用Cake来保持与CI/CD环境的解耦。
当时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.
上一篇博客[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 "$@"
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
原文地址:https://www.cnblogs.com/linianhui/p/cake-with-dotnet-tool.html
.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com