Search code examples
azure-devopsblazor-webassemblyblazor-client-sideazure-pipelines-yaml

Pass arguments to Blazor WebAssembly from pipeline build-publish-deployment


I know I can use Environment variables to create (and use) different appSettings.json files in my client-side Blazor WebAssembly project.

I would like to implement a more general and flexible solution, where I can start a pipeline deployment passing for example a suffix that's used throughout the deployment, to customize it totally.

So, for example, if I have the simple case of a back-end and blazor webassembly client, passing '31' as an argument would generate a web api 'api-31' and a client 'blazor-client-31' that knows that he has to connect to 'api-31'.

I would need a way to modify the appSetting.json file before deployment, or override it like in 'UseEnvironmentVariables()'...

I could solve my problem when dockerizing the client, with something like

FROM base AS final
ARG apiprefix
WORKDIR /usr/share/nginx/html
COPY --from=build /src/publish/wwwroot/ .
COPY /WebApps/WebBlazor/nginx.conf .
RUN echo "{  \"apiprefix\": \"$apiprefixval\" }" > ExecutionContext.json

And then reading that json file in the client. Maybe it's not beautiful, but it's simple, and it works.

Is there anything similar to this (if not better) when just publishing and deploying directly from a pipeline? How can I write a json file (maybe with some PowerShell code) in the webapp that I'm using to host the Blazor WebAssembly?

Actually I use these steps, to publish and deploy:

- task: DotNetCoreCLI@2
  displayName: 'Publish BlazorAssemblyClient'
  inputs:
    command: publish
    projects: '**/BlazorAssemblyClient.csproj'
    arguments: '--configuration $(buildConfiguration) --runtime win-x64 --output $(System.ArtifactsDirectory)/tempBlazorAssemblyClient'
    modifyOutputPath: true
    zipAfterPublish: true

- task: AzureRmWebAppDeployment@4
  displayName: 'Deploy BlazorAssemblyClient'
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: '...'
    appType: 'webApp'
    WebAppName: $(blazorAppName)
    packageForLinux: '$(System.ArtifactsDirectory)/tempBlazorAssemblyClient/**/*.zip'

And i cannot see how I can insert anything custom, in this process.

Thanks to anyone wanting to help.


Solution

  • I would need a way to modify the appSetting.json file before deployment, or override it like in 'UseEnvironmentVariables()'...

    We could install the extension Replace Tokens, add variable and set the variable to secret then add the task Replace Tokens to replace the appSetting.json variable and use it in the Azure DevOps pipeline.

    Update1

    Thanks for Andrea sharing.

    There is a standard way, using just the deploy step, that includes a transformation capability, we could refer to this doc for more details.