diff --git a/docs/cicd-with-bicep.md b/docs/cicd-with-bicep.md index 3433abf82db..97eb54319f3 100644 --- a/docs/cicd-with-bicep.md +++ b/docs/cicd-with-bicep.md @@ -1,19 +1,19 @@ # Adding Bicep to a CI/CD pipeline +## Build and Deploy with CI/CD As your Bicep practice matures, you will want to check-in your Bicep code into source control and kick off a pipeline or workflow, which would do the following: - 1. Build your Bicep file into an ARM Template 1. Deploy the generated ARM template With the current Azure CLI 2.20 now installed in GitHub and also on Azure DevOps, Bicep CLI can be automatically triggerd by using `az bicep build` command and an explicit task to download Bicep CLI is no more needed. -The two examples below illustrates this. It assumes the following prerequisite: +The two examples below illustrate this. They assume the following prerequisites: * The Bicep file you want to transpile and deploy is called `main.bicep` and exists in the root of the repo * The parameters file you want to use is called `parameters.json` and exists in the root of the repo * You are deploying the transpiled ARM Template to a resource group. Deploying to another scope like a subscription requires a different CLI command. -## GitHub Workflow +### GitHub Workflow ```yaml @@ -68,7 +68,7 @@ jobs: az account show az deployment group create -f ./main.json -g ${{ env.AZURE_RESOURCE_GROUP }} ``` -## Azure DevOps Pipeline +### Azure DevOps Pipeline ```yaml trigger: @@ -152,4 +152,160 @@ stages: csmFile: '$(Pipeline.Workspace)/drop/main.json' csmParametersFile: '$(Pipeline.Workspace)/drop/parameters.json' deploymentMode: 'Incremental' +``` + +## Build with MSBuild +If your existing CI pipeline heavily relies on MSBuild, you can use our MSBuild task and CLI packages to compile your .bicep files into ARM templates. + +This functionality relies on the following NuGet packages: +| Package Name | Description | +|:-|-| +| `Azure.Bicep.MSBuild` | Cross-platform MSBuild task to invoke the Bicep CLI to compile .bicep files into ARM templates | +| `Azure.Bicep.CommandLine.win-x64` | Bicep CLI for Windows | +| `Azure.Bicep.CommandLine.linux-x64` | Bicep CLI for Linux | +| `Azure.Bicep.CommandLine.osx-x64` | Bicep CLI for OSX | + +### `Azure.Bicep.CommandLine.*` packages +When referenced in a project file via a `PackageReference`, the `Azure.Bicep.CommandLine.*` packages set the `BicepPath` property to the full path of the Bicep executable for the platform. The reference to this package may be omitted if Bicep CLI is installed through other means and the `BicepPath` environment variable or MSBuild property are set accordingly. + +### `Azure.Bicep.MSBuild` package +When referenced in a project file via a `PackageReference`, the `Azure.Bicep.MSBuild` package imports the `Bicep` task used to invoke the Bicep CLI and convert its output into MSBuild errors and the `BicepCompile` target used to simplify the usage of the `Bicep` task. By default the `BicepCompile` runs after the `Build` target and will compile all `@(Bicep)` items and place the output in `$(OutputPath)` with the same file name and the `.json` extension. + +The following will compile `one.bicep` and `two.bicep` files in the same directory as the project file and placed the compiled `one.json` and `two.json` in the `$(OutputPath)`. +```msbuild + + + + +``` + +You can override the output path per file using the `OutputFile` metadata on `Bicep` items. The following will recursively find all `main.bicep` files and place the compiled `.json` files in `$(OutputPath)` under a sub-directory with the same name in `$(OutputPath)`: +```msbuild + + + +``` + +Additional customizations can be performed by setting one of the following properties in your project: +| Property Name | Default Value | Description | +|:-|:-|:-| +| `BicepCompileAfterTargets` | `Build` | Used as `AfterTargets` value for the `BicepCompile` target. Change the value to override the scheduling of the `BicepCompile` target in your project. | +| `BicepCompileDependsOn` | None | Used as `DependsOnTargets` value for the `BicepCompile` target. This can be set to targets that you want `BicepCompile` target to depend on. | +| `BicepCompileBeforeTargets` | None | Used as `BeforeTargets` value for the `BicepCompile` target. | +| `BicepOutputPath` | `$(OutputPath)` | Set this to override the default output path for the compiled ARM template. `OutputFile` metadata on `Bicep` items takes precedence over this value. | + +The `Azure.Bicep.MSBuild` requires the `BicepPath` property to be set either in order to function. You may set it by referencing the appropriate `Azure.Bicep.CommandLine.*` package for your OS or manually by installing the Bicep CLI and setting the `BicepPath` environment variable or MSBuild property. The examples below assume Windows. + +### SDK-based .csproj example +The following contains a default Console App SDK-based C# project file that was modified to additionally compile Bicep files into ARM templates. This type of project will work with the classic .net framework, .net core, and .net 5. + +> Replace `__LATEST_VERSION__` with the latest version of the Bicep NuGet packages. + +```xml + + + Exe + netcoreapp3.1 + + + + + + + + + + + + +``` + +### NoTargets SDK example +The following contains a project that compiles Bicep files into ARM templates using the NoTargets SDK. This allows creation of standalone projects that compile only Bicep files. +> Replace `__LATEST_VERSION__` with the latest version of the Bicep NuGet packages. + +```xml + + + net472 + + + + + + + + + + + +``` + +### Classic .csproj example +The following contains an example of how to build Bicep files inside a classic .csproj file (not SDK-based). Only use this if the previous examples do not work for you. +> Replace `__LATEST_VERSION__` with the latest version of the Bicep NuGet packages. + +```xml + + + + + Debug + AnyCPU + {7E4E9C45-3EBE-419D-9E45-BCF7CA61961E} + Exe + ClassicFramework + ClassicFramework + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + __LATEST_VERSION__ + + + __LATEST_VERSION__ + + + + ``` \ No newline at end of file diff --git a/scripts/UploadSignedReleaseArtifacts.ps1 b/scripts/UploadSignedReleaseArtifacts.ps1 index 94fa60e1b24..79efdf9eb6e 100644 --- a/scripts/UploadSignedReleaseArtifacts.ps1 +++ b/scripts/UploadSignedReleaseArtifacts.ps1 @@ -82,6 +82,10 @@ $artifacts = @( @{ assetName = "Azure.Bicep.MSBuild.$buildVersion.nupkg"; relativePath = "Azure.Bicep.MSBuild.$buildVersion.nupkg"; + }, + @{ + assetName = "Azure.Bicep.MSBuild.$buildVersion.snupkg"; + relativePath = "Azure.Bicep.MSBuild.$buildVersion.snupkg"; } ); zipAssets = @(