首页 > 系统相关 >Azure DevOps 发布.Net项目到Windows IIS站点之Azure项目发布内网VM

Azure DevOps 发布.Net项目到Windows IIS站点之Azure项目发布内网VM

时间:2023-11-13 22:12:03浏览次数:35  
标签:name IIS value Write Build Azure Output ArtifactStagingDirectory 内网

当你有一个需求,需要通过Azure DevOps发布到一个没有公网的VM的时候,你将需要使用以下脚本

trigger:
- master

pool:
  vmImage: 'windows-2022'

variables:
  - name: Build.ArtifactStagingDirectory
    value: '$(Build.Repository.LocalPath)\artifacts'
  - name: buildConfiguration
    value: 'Release'
  - name: publishFolder
    value: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId)'
  - name: solution
    value: '项目.sln'
  - name: zipFileName
    value: '压缩包xxx.zip'
  - name: storageAccountName
    value: 'storagea名称'
  - name: resourceGroupName
    value: '资源组'
  - name: vmName
    value: 'VM虚拟机名称'
  - name: shareName
    value: 'storage目录'
  - name: destPath
    value: "目录地址"



stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: 'Build job'
    steps:
    - script: dotnet restore $(solution)
      displayName: 'Build solution'

    - script: dotnet publish $(solution) --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)
      displayName: 'Publish solution'

    - task: ArchiveFiles@2
      inputs:
        rootFolderOrFile: '$(Build.ArtifactStagingDirectory)'
        includeRootFolder: false
        archiveType: 'zip'
        archiveFile: '$(Build.ArtifactStagingDirectory)\$(zipFileName)'
        replaceExistingArchive: true

    - script: |
        echo "===== Listing files in ArtifactStagingDirectory ====="
        ls "$(Build.ArtifactStagingDirectory)"
        echo "===== Finished listing files ====="
      displayName: 'List files in ArtifactStagingDirectory'
      
    - task: PublishBuildArtifacts@1
      inputs:
        pathtoPublish: '$(Build.ArtifactStagingDirectory)'
        artifactName: 'drop'
        publishLocation: 'Container'

- stage: Deploy
  displayName: 'Deploy to IIS on Azure VM using ARM'
  jobs:
  - deployment: DeployJob
    displayName: 'Deploy'
    environment: 'YourEnvironmentName'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: 'drop'
              downloadPath: '$(System.ArtifactsDirectory)'
              
          - task: AzurePowerShell@5
            inputs:
              azureSubscription: 'PublishAreaDts'
              azurePowerShellVersion: LatestVersion
              ErrorActionPreference: 'Continue'
              ScriptType: 'InlineScript'
              Inline: |
                # Verify the value of $srcPath
                $srcPath = Join-Path -Path $env:AGENT_BUILDDIRECTORY -ChildPath "drop\$(zipFileName)"
                Write-Output "===== Verifying the value of srcPath ====="
                Write-Output "srcPath: $srcPath"
                Write-Output "===== Finished verifying ====="

                # Check if the file exists
                if (Test-Path -Path $srcPath) {
                    Write-Output "File exists"
                } else {
                    Write-Output "File does not exist"
                    throw "File not found: $srcPath"
                }

                # Add more debug output
                Write-Output "===== Getting storage account ====="
                $storageAccount = Get-AzStorageAccount -ResourceGroupName $(resourceGroupName) -Name "$(storageAccountName)" -ErrorAction SilentlyContinue
                Write-Output "===== Finished getting storage account ====="

                Write-Output "===== Getting file share ====="
                $fileShare = Get-AzStorageShare -Name "$(shareName)" -Context $storageAccount.Context -ErrorAction SilentlyContinue
                Write-Output "===== Finished getting file share ====="
                if (-not $fileShare) {
                    $fileShare = New-AzStorageShare -Name "$(shareName)" -Context $storageAccount.Context
                }
                
                # Write-Output "===== Uploading file to storage account ====="
                Set-AzStorageFileContent -ShareName $fileShare.Name -Source $srcPath -Path "$(zipFileName)" -Context $storageAccount.Context
                # Write-Output "===== Finished uploading file ====="

                Write-Output "===== Generating SAS token ====="
                $url = $fileShare.CloudFileShare.Uri.AbsoluteUri + "/$(zipFileName)"
                $sasToken = New-AzStorageShareSASToken -ShareName $fileShare.Name -Context $storageAccount.Context -Permission "r" -ExpiryTime (Get-Date).AddHours(1)
                $urlWithSas = $url + $sasToken
                Write-Output "===== Finished generating SAS sasToken $urlWithSas ====="
                # Write-Output "===== Finished generating SAS file $file ====="
                Write-Output "===== Finished generating SAS url $url ====="
                Write-Output "===== Executing script on target VM ====="

                # Update the script to execute
                $scriptToExecute = @"
                    Add-Type -AssemblyName System.IO.Compression.FileSystem
                    Invoke-WebRequest -Uri `"$urlWithSas`" -OutFile c:\temp\$(zipFileName)
                    Remove-Item -Path $(destPath) -Recurse -Force
                    [System.IO.Compression.ZipFile]::ExtractToDirectory('c:\temp\$(zipFileName)', '$(destPath)')
                "@

                # Execute the script on the target VM and capture the result
                $runCommandResult = Invoke-AzVMRunCommand -ResourceGroupName $(resourceGroupName) -Name $(vmName) -CommandId 'RunPowerShellScript' -ScriptString $scriptToExecute -Verbose

                # Output the result of Invoke-AzVMRunCommand
                Write-Output "===== Invoke-AzVMRunCommand Result ====="
                Write-Output $runCommandResult.Value
                Write-Output "===== End of Invoke-AzVMRunCommand Result ====="

            env:
              ArtifactStagingDirectory: $(Build.ArtifactStagingDirectory)

标签:name,IIS,value,Write,Build,Azure,Output,ArtifactStagingDirectory,内网
From: https://www.cnblogs.com/MrChuJiu/p/17830393.html

相关文章

  • 【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
    问题描述通过.NETAzureStorageBlobsSDK,获取Blob的MD5值,查看了Azure操作手册中,介绍可以使用 blob.Properties.ContentMD5属性。//blob文件测试CloudBlobClientblobClient=storageAccount.CreateCloudBlobClient();CloudBlobContainercontainer=blobClient......
  • 内网穿透小工具
    1、概述介绍Ngrok是一个内网穿透工具,该工具采用go语言进行开发,支持TCP、HTTP、TLS(企业用户,需要付费)。该工具主要用于解决外网的客户端无法访问内网资源的问题,因为其具有使用简单、连接迅速,具备良好的跨平台特性等特点,所以可以被项目开发人员用来快速在互联网部署项目,同时,该工具......
  • 如何修改Azure SQL server数据库的字符集(UTF-8)排序规则
    ......
  • 解决Azure function部署问题
    最近在AzureChina遇到了一个问题,大概现象是这样,我们用的runtime是python的,创建好function之后,在尝试往function上部署代码时遇到一些奇怪的现象,试了各种方法将代码部署到function上,都会遇到一些问题1.vscodeextension-报错Encounteredanerror7:15:31AMaz-mim-xxxxxx:Synci......
  • Quartz.Net 在IIS下不执行或多次执行解决
    不执行解决方法在IIS中打开程序对应的应用程序池【高级设置】1.设置应用程序池【回收】->【固定时间间隔(分钟)】为0。2.设置应用程序池【进程模型】->【闲置超时时间(分钟)】为0。执行多次解决方法在IIS中打开程序对应的应用程序池【高级设置】1.设置应用程序池【进程模型】->【......
  • IIS怎么设置跨域访问
    转自:https://jingyan.baidu.com/article/6dad5075fd697ce023e36ed9.html1、设置Access-Control-Allow-Origin 2、打开IIS,找到“HTTP响应标头”点进去 3、在右侧可以看到添加,然后添加如下标头即可Access-Control-Allow-Headers:Content-Type,api_key,AuthorizationAccess......
  • 内网穿透解决方案之frp
    该方法需要有一台外网可以访问的固定ip机器,一般可以是云服务器windows、linux、macOS都支持从GitHub上下载frp其中包含客户端与服务端执行文件和配置文件服务端frpsfrps.toml客户端frpcfrpc.toml在云服务器上配置与运行服务端(以linux为例)以最简单的方式,服务端只......
  • 内网渗透-防火墙出入规则上线-正反向连接+隧道技术-SMB+防火墙控制
    环境:如下图 不出网-控制上线-CS-反向连接前提:已经使用攻击机通过漏洞拿下了windows7主机,又通过windows7正向连接拿下了windows10。 目的:让windows2008在cs上线。      想要通过windows10正向连接拿下windows2008时,发现windows2008开启了防火墙对入站规则做了限制,无法正向......
  • IIS HTTP 500 - 内部服务器错误完美解决 IIS 服务器无法加载应用程序 '/LM/W3SVC/1/RO
    现象:       浏览ASP页面提示  500内部错误事件查看器中:        [1]:               服务器无法加载应用程序'/LM/W3SVC/1/ROOT'。错误是'没有注册类别'。                有关此消息的详细信息,请访问Microsoft联机支......
  • 【Microsoft Azure 的1024种玩法】七十四.五分钟在Azure Virtual Machines中快速部署
    【简介】ApacheMaven由Apache软件基金会所提供的一个软件项目管理及自动构建工具,Maven为开发者提供了一套完整的构建生命周期框架。开发团队几乎不用花多少时间就能够自动完成工程的基础构建配置,因为Maven使用了一个标准的目录结构和一个默认的构建生命周期,Maven能够在很短......