如何使用Cake.net與Gitlab CI?

[英]How to use Cake.net with Gitlab CI?


I have an ASP.NET MVC application. I am trying to implement CI and CD using Gitlab and Cake.net.

我有一個ASP。凈MVC應用程序。我正在嘗試使用Gitlab和Cake.net實現CI和CD。

In order to test more easily, I installed Gitlab CI multi runner on my machine. I registered it with 'shell' as executor.

為了更容易測試,我在我的機器上安裝了Gitlab CI multi runner。我注冊了殼牌公司作為遺囑執行人。

I am trying to execute the Cake.net build.ps1 file from .gitlab-ci.yml, but it doesn't execute the script. When it reaches the build.ps1 line it only opens the file with notepad and then it says Build succeeded.

我正在嘗試執行Cake.net構建。從.gitlab-ci ps1文件。yml,但是它不執行腳本。當它到達構建時。行,它只使用記事本打開文件,然后它說構建成功了。

What am I missing? Why isn't the script executed?

我缺少什么?為什么沒有執行腳本?

Here is the code:

這是代碼:

.gitlab-ci.yml

.gitlab-ci.yml

stages:
  - build
build:
 stage: build
 script:
  - build.ps1
 only:
   - develop

Gitlab CI multi runner config.toml

Gitlab CI multi runner config.toml。

concurrent = 1
check_interval = 0

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

build.cake

build.cake

#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);

build.ps1 (Cake.net default file)

構建。ps1(Cake.net默認文件)

<#
.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

2 个解决方案

#1


3  

You could try changing

你可以試着改變

  - build.ps1

To 

  - PowerShell .\build.ps1

#2


0  

devlead answer is great, but in my case the script is located in a subfolder and I need passing an argument to the Cake script. On top of it, I have to change directory before running the script so that it goes well. Here is the full instruction :

devlead的回答很好,但是在我的例子中,腳本位於一個子文件夾中,我需要將一個參數傳遞給蛋糕腳本。最重要的是,我必須在運行腳本之前更改目錄,以便它正常運行。以下是完整的說明:

- PowerShell -command "& cd ./mysubfolder; ./build.ps1 -stringparam='myvalue'"

The semi colon ";" separates several powershell commands which are executed one after the other. The first command changes the current directory, the second command runs the script in the current directory (mysubfolder).

semi冒號“;”分隔多個執行一個接一個的powershell命令。第一個命令更改當前目錄,第二個命令在當前目錄(mysubfolder)中運行腳本。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/30/720b9084c2f2d41233eb157d44920a53.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com