Search code examples
msbuildazure-devopsazure-web-app-servicewebdeployterraform-provider-azure

When deploying my build to azure (i use devops pipeline but i also tried to deploy via visual studio directly) i keep getting resource not found 404


When I try to publish my .net core 2.2 webapi to my azure app service (via azure devops using Azure App Service deploy or via visual studio publish method) I keep getting: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

But when I publish it directly with visual studio ( without package or zip configuration) it works. (but we can't keep doing this because we need it working on the azure devops pipeline).

I have tried to build the project differently using dotnet build/publish and vsbuild. I Also tried using different methods of publish (zip,package, web deploy).

I went thru the generated xml files (deploy.cmd,deploy-readme,parameters,setparametersa and sourcemanifest) to check if there is some wrong naming issue or if the folder structure is incorrect but everything matches correctly. The zip file where my project is in, is also in the same location as all the generated xml files(root).

The strange thing is that it was working before and we are using terraform to generate the azure resources. So we threw away the resources which we made manually and remade them using terraform and then we get the error. We did not change anything on the pipeline.

yaml build code from the pipeline:

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:CreatePackageOnPublish=true /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\{redactedprojectname}.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'


- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Terraform script for the app service part :

# Create App Service Plan
resource "azurerm_app_service_plan" "asp" {
  name                = "${local.aspName}"
  location            = "${azurerm_resource_group.rg.location}"
  resource_group_name = "${azurerm_resource_group.rg.name}"
  tags                = "${var.tags}"

  sku {
      tier = "${var.aspSku.tier}"
      size = "${var.aspSku.size}"
    }
}

# Create App Service
resource "azurerm_app_service" "as" {
  name                = "${local.apiName}"
  location            = "${azurerm_resource_group.rg.location}"
  resource_group_name = "${azurerm_resource_group.rg.name}"
  app_service_plan_id = "${azurerm_app_service_plan.asp.id}"
  tags                = "${var.tags}"

  https_only          = true

  site_config {
    dotnet_framework_version = "v4.0"
    always_on = true
  }

  app_settings = {
    "APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.ai.instrumentation_key}"
    "ASPNETCORE_ENVIRONMENT" = "${var.environment}"
  }
}

# --- Output section --
output "appServiceName" {
  value = azurerm_app_service.as.name
}

Terraform steps in azure devops:

use terraform 0.12.11
Terraform init
Terraform plan
Terraform apply
Terraform output get appservice name

after this we do Azure App Service deploy

yaml code app service deploy:

variables:
  environment: 'prd'

steps:
- task: AzureRmWebAppDeployment@4
  displayName: 'Azure App Service Deploy: $(appServiceName)'
  inputs:
    azureSubscription: '{redactedprojectname}'
    WebAppName: '$(appServiceName)'
    packageForLinux: '$(System.DefaultWorkingDirectory)/{redactedprojectname}/drop'
    AppSettings: ASPNETCORE_ENVIRONMENT "$(environment)"'
    enableCustomDeployment: true
    DeploymentType: zipDeploy
    TakeAppOfflineFlag: false

I would expect that after having a working solution before, that deleting the resources and redeploying to let terraform recreate the resources from scratch without modifying the pipeline, would just provide a working solution


Solution

  • We ended up solving the build issue by using msbuild with filesystem instead of package Here is the yaml code for anyone who encounters the same issue:

    
    trigger:
    - master
    
    pool:
      vmImage: 'windows-latest'
    
    variables:
      solution: '**/*.sln'
      buildPlatform: 'Any CPU'
      buildConfiguration: 'Release'
    
    steps:
    - task: NuGetToolInstaller@1
    
    - task: NuGetCommand@2
      inputs:
        restoreSolution: '$(solution)'
    
    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        msbuildArgs: '/p:DeployOnBuild=true /p:Configuration=Release /p:WebPublishMethod=FileSystem  /p:SkipInvalidConfigurations=true /p:DeployDefaultTarget=WebPublish /p:DeleteExistingFiles=True  /p:publishUrl="$(build.artifactStagingDirectory)\YourProjectNameFolder" /p:DeployIisAppPath="Default Web Site"'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
    
    
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'
    

    All you need to change is the msbuildArgs property.

    The msbuild arguments for the filesystem are based on https://www.joe-stevens.com/2018/12/14/publish-to-file-system-with-msbuild/