如何将Cake.net与Gitlab CI一起使用?

如何将Cake.net与Gitlab CI一起使用?,.net,powershell,gitlab-ci,gitlab-ci-runner,cakebuild,.net,Powershell,Gitlab Ci,Gitlab Ci Runner,Cakebuild,我有一个ASP.NETMVC应用程序。我正在尝试使用Gitlab和Cake.net实现CI和CD 为了更容易地进行测试,我在机器上安装了Gitlab CI multi-runner。我在“壳牌”登记为执行人 我试图从.gitlab-ci.yml执行Cake.net build.ps1文件,但它不执行脚本。当它到达build.ps1行时,它只会用记事本打开文件,然后说build successed 我错过了什么?为什么不执行脚本? 代码如下: .gitlab ci.yml stages: -

我有一个ASP.NETMVC应用程序。我正在尝试使用Gitlab和Cake.net实现CI和CD

为了更容易地进行测试,我在机器上安装了Gitlab CI multi-runner。我在“壳牌”登记为执行人

我试图从.gitlab-ci.yml执行Cake.net build.ps1文件,但它不执行脚本。当它到达build.ps1行时,它只会用记事本打开文件,然后说build successed

我错过了什么?为什么不执行脚本?

代码如下:

.gitlab ci.yml

stages:
  - build
build:
 stage: build
 script:
  - build.ps1
 only:
   - develop
concurrent = 1
check_interval = 0

[[runners]]
  name = "Development runner"
  url = "https://gitlab.com/ci"
  token = "***"
  executor = "shell"
  shell = "powershell"
Gitlab CI multi-runner config.toml

stages:
  - build
build:
 stage: build
 script:
  - build.ps1
 only:
   - develop
concurrent = 1
check_interval = 0

[[runners]]
  name = "Development runner"
  url = "https://gitlab.com/ci"
  token = "***"
  executor = "shell"
  shell = "powershell"
制作蛋糕

#tool "nuget:?package=xunit.runner.console"

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var version = Argument("releaseNumber", "");

var solution = "src/Pentrugatit.sln";
var binFolder = "src/Presentation/Nop.Web/bin/";
var pluginsFolder = "src/Presentation/Nop.Web/Plugins/";

Task("Clean")
  .Does(() => {
    CleanDirectories(binFolder);
    CleanDirectories(pluginsFolder);
  });

Task("NuGetRestore")
  .Does(() => NuGetRestore(solution));

Task("Build")
  .IsDependentOn("Clean")
  .IsDependentOn("NuGetRestore")
  .Does(() => MSBuild(solution, new MSBuildSettings { Configuration = configuration }));

Task("Default")
  .IsDependentOn("Build");

RunTarget(target);
<#
.SYNOPSIS
This is a Powershell script to bootstrap a Cake build.
.DESCRIPTION
This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
and execute your Cake build script with the parameters you provide.
.PARAMETER Target
The build script target to run.
.PARAMETER Configuration
The build configuration to use.
.PARAMETER Verbosity
Specifies the amount of information to be displayed.
.PARAMETER WhatIf
Performs a dry run of the build script.
No tasks will be executed.
.PARAMETER ScriptArgs
Remaining arguments are added here.
.LINK
http://cakebuild.net
#>

[CmdletBinding()]
Param(
    [string]$Target = "Default",
    [ValidateSet("Release", "Debug")]
    [string]$Configuration = "Release",
    [ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
    [string]$Verbosity = "Verbose",
    [switch]$WhatIf,
    [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
    [string[]]$ScriptArgs
)

$CakeVersion = "0.17.0"
$DotNetChannel = "preview";
$DotNetVersion = "1.0.0-preview2-003121";
$DotNetInstallerUri = "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1";
$NugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

# Make sure tools folder exists
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$ToolPath = Join-Path $PSScriptRoot "tools"
if (!(Test-Path $ToolPath)) {
    Write-Verbose "Creating tools directory..."
    New-Item -Path $ToolPath -Type directory | out-null
}

###########################################################################
# INSTALL .NET CORE CLI
###########################################################################

Function Remove-PathVariable([string]$VariableToRemove)
{
    $path = [Environment]::GetEnvironmentVariable("PATH", "User")
    if ($path -ne $null)
    {
        $newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
        [Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "User")
    }

    $path = [Environment]::GetEnvironmentVariable("PATH", "Process")
    if ($path -ne $null)
    {
        $newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
        [Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "Process")
    }
}

# Get .NET Core CLI path if installed.
$FoundDotNetCliVersion = $null;
if (Get-Command dotnet -ErrorAction SilentlyContinue) {
    $FoundDotNetCliVersion = dotnet --version;
}

if($FoundDotNetCliVersion -ne $DotNetVersion) {
    $InstallPath = Join-Path $PSScriptRoot ".dotnet"
    if (!(Test-Path $InstallPath)) {
        mkdir -Force $InstallPath | Out-Null;
    }
    (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, "$InstallPath\dotnet-install.ps1");
    & $InstallPath\dotnet-install.ps1 -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;

    Remove-PathVariable "$InstallPath"
    $env:PATH = "$InstallPath;$env:PATH"
    $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
    $env:DOTNET_CLI_TELEMETRY_OPTOUT=1
}

###########################################################################
# INSTALL NUGET
###########################################################################

# Make sure nuget.exe exists.
$NugetPath = Join-Path $ToolPath "nuget.exe"
if (!(Test-Path $NugetPath)) {
    Write-Host "Downloading NuGet.exe..."
    (New-Object System.Net.WebClient).DownloadFile($NugetUrl, $NugetPath);
}

###########################################################################
# INSTALL CAKE
###########################################################################

# Make sure Cake has been installed.
$CakePath = Join-Path $ToolPath "Cake.$CakeVersion/Cake.exe"
if (!(Test-Path $CakePath)) {
    Write-Host "Installing Cake..."
    Invoke-Expression "&`"$NugetPath`" install Cake -Version $CakeVersion -OutputDirectory `"$ToolPath`"" | Out-Null;
    if ($LASTEXITCODE -ne 0) {
        Throw "An error occured while restoring Cake from NuGet."
    }
}

###########################################################################
# RUN BUILD SCRIPT
###########################################################################

# Build the argument list.
$Arguments = @{
    target=$Target;
    configuration=$Configuration;
    verbosity=$Verbosity;
    dryrun=$WhatIf;
}.GetEnumerator() | %{"--{0}=`"{1}`"" -f $_.key, $_.value };

# Start Cake
Write-Host "Running build script..."
Invoke-Expression "& `"$CakePath`" `"build.cake`" $Arguments $ScriptArgs"
exit $LASTEXITCODE
build.ps1(Cake.net默认文件)


[CmdletBinding()]
Param(
[字符串]$Target=“默认值”,
[验证集(“发布”、“调试”)]
[string]$Configuration=“Release”,
[验证集(“安静”、“最小”、“正常”、“详细”、“诊断”)]
[string]$Verbosity=“Verbose”,
[开关]$WhatIf,
[参数(位置=0,必需=$false,ValueFromRemainingArguments=$true)]
[string[]]$ScriptArgs
)
$CakeVersion=“0.17.0”
$DotNetChannel=“预览”;
$DotNetVersion=“1.0.0-preview2-003121”;
$DotNetInstallerUri=”https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1";
$NugetUrl=”https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
#确保“工具”文件夹存在
$PSScriptRoot=拆分路径$MyInvocation.MyCommand.Path-父级
$ToolPath=连接路径$PSScriptRoot“工具”
如果(!(测试路径$ToolPath)){
写详细的“创建工具目录…”
新项-路径$ToolPath-类型目录| out null
}
###########################################################################
#安装.NET CORE CLI
###########################################################################
函数Remove PathVariable([string]$VariableToRemove)
{
$path=[Environment]::GetEnvironmentVariable(“路径”、“用户”)
如果($path-ne$null)
{
$newItems=$path.Split(“;”,[StringSplitOptions]::removeMptyEntries)|其中对象{“$($)”-inotlike$VariableToRemove}
[Environment]::SetEnvironmentVariable(“PATH”,[System.String]::Join(“;”,$newItems),“User”)
}
$path=[Environment]::GetEnvironmentVariable(“路径”、“进程”)
如果($path-ne$null)
{
$newItems=$path.Split(“;”,[StringSplitOptions]::removeMptyEntries)|其中对象{“$($)”-inotlike$VariableToRemove}
[Environment]::SetEnvironmentVariable(“PATH”,[System.String]::Join(“;”,$newItems),“Process”)
}
}
#获取.NET Core CLI路径(如果已安装)。
$FoundDotNetCliVersion=$null;
if(获取命令dotnet-ErrorAction SilentlyContinue){
$FoundDotNetCliVersion=dotnet--version;
}
if($FoundDotNetCliVersion-ne$DotNetVersion){
$InstallPath=加入路径$PSScriptRoot“.dotnet”
如果(!(测试路径$InstallPath)){
mkdir-强制$InstallPath | Out Null;
}
下载文件($DotNetInstallerUri,“$InstallPath\dotnetinstall.ps1”);
&$InstallPath\dotnet-install.ps1-通道$DotNetChannel-版本$DotNetVersion-安装目录$InstallPath;
删除路径变量“$InstallPath”
$env:PATH=“$InstallPath;$env:PATH”
$env:DOTNET\u SKIP\u FIRST\u TIME\u EXPERIENCE=1
$env:DOTNET\u CLI\u遥测\u OPTOUT=1
}
###########################################################################
#安装NUGET
###########################################################################
#确保nuget.exe存在。
$NugetPath=连接路径$ToolPath“nuget.exe”
如果(!(测试路径$NugetPath)){
写主机“下载NuGet.exe…”
(新对象系统.Net.WebClient).DownloadFile($numgeturl,$numgetpath);
}
###########################################################################
#安装蛋糕
###########################################################################
#确保蛋糕已经安装好。
$CakePath=连接路径$ToolPath“Cake.$CakeVersion/Cake.exe”
如果(!(测试路径$CakePath)){
编写主机“安装蛋糕…”
调用表达式“&`“$NugetPath`”install Cake-Version$CakeVersion-OutputDirectory`“$ToolPath``”| Out Null;
if($LASTEXITCODE-ne 0){
Throw“从NuGet还原蛋糕时出错。”
}
}
###########################################################################
#运行构建脚本
###########################################################################
#构建参数列表。
$Arguments=@{
target=$target;
配置=$configuration;
详细程度=$verbosity;
干运行=WhatIf美元;
}.GetEnumerator()|%{--{0}=`{1}`'-f$\u0.key,$\u0.value};
#开始吃蛋糕
编写主机“正在运行构建脚本…”
调用表达式“&`$CakePath``` build.cake``“$Arguments$ScriptArgs”
退出$LASTEXITCODE
您可以尝试更改

  - build.ps1


devlead的答案很好,但在我的例子中,脚本位于一个子文件夹中,我需要向Cake脚本传递一个参数。最重要的是,我必须在运行脚本之前更改目录,以使其运行良好。以下是完整的说明:

- PowerShell -command "& cd ./mysubfolder; ./build.ps1 -stringparam='myvalue'"
分号“;”分隔了几个相继执行的powershell命令。
第一个命令更改当前目录,第二个命令运行当前目录(mysubfolder)中的脚本。

我收到以下错误:术语“build.ps1”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。“请重试。\build.ps1而不是build.ps1,更新答案以反映。实际上,正如我最近被咬到的那样,执行PowerShell脚本的正确方法是使用
PowerShell.exe-NoProfile-NoInteractive-File”。\build.ps1“
这确保不会有任何提示,也不会因为某人向profile.ps1中添加了一些代码而感到意外