gitea actions 下载artifact和部署到IIS

方式一: 通过WebAdministration模块控制程序池

需要在仓库设置里配置变量IIS_APP_NAME
image

由于WebAdministration模块需要有admin权限,所以要求runner是通过有管理员权限去运行的act_runner.exe deamon

deploy-test-win:
    #if: github.ref.name == 'develop'
    needs: dotnet-publish #表示需要等dotnet-publish任务执行完毕后才会执行该任务
    runs-on: iis #运行在有iis标签的runner
    steps:
      - uses: christopherhx/gitea-download-artifact@v4
        with:
          name: myApp #记得修改为上传时配置的name
      - name: Deploy to IIS
        shell: pwsh
        run: |
          Write-Host Deploy by WebAdministration
          Import-Module WebAdministration
          $inputSiteId = "${{ vars.IIS_APP_NAME }}"
          if(([string]::IsNullOrEmpty($inputSiteId)))
          {
              Write-Host siteId empty - $inputSiteId
              exit 1
          }

          $siteId = "IIS:\Sites\$inputSiteId"
          $currentIISPath = Get-WebFilePath -PSPath $siteId
          if(([string]::IsNullOrEmpty($currentIISPath)) -Or (-Not (Test-Path $currentIISPath))) #判断iis站点目录是否存在
          {
              Write-Host Cannot deploy - IIS path is wrong
              exit 1
          }

          # 获取应用程序池状态
          $poolState = Get-WebItemState -PSPath "IIS:\AppPools\$inputSiteId"
          if ($poolState -eq "Started") 
          {
              Write-Host "Stopping IIS app pool $inputSiteId"
              Stop-WebAppPool -Name $inputSiteId
          }

          $parentPath = Split-Path -parent $currentIISPath
          $packageFolderName = Split-Path -leaf $currentIISPath
          $backupFileName = "${packageFolderName}_backup.zip"
          $backupPath = Join-Path -Path $parentPath -ChildPath $backupFileName
          Compress-Archive -Path $currentIISPath -DestinationPath $backupPath -Force #压缩备份
          Write-Host "Copying files to IIS site path"
          $excludeFiles = @("appsettings.json", "web.config") #排除appsettings.json和web.config
          $excludeParams = $excludeFiles | ForEach-Object { "/XF", $_ }
          #复制所有子目录,包括空目录, 只复制源文件较新的文件,目标中源目录不存在的文件不删除,排除appsetting.json web.config,失败时重试2次,重试等待1s
          robocopy . $currentIISPath /E /XO /XN /XX $excludeParams /R:2 /W:1

          Write-Host "Starting IIS app pool $inputSiteId"
          Start-WebAppPool -Name $inputSiteId
          Write-Host "Deployment complete"
          exit 0

方式二:通过设置app_offline.html来接管请求

需要在仓库设置里配置变量IIS_APP_PATH,值为目标站点的目录

deploy-test-win:
    #if: github.ref.name == 'develop'
    needs: dotnet-publish #表示需要等dotnet-publish任务执行完毕后才会执行该任务
    runs-on: iis #运行在有iis标签的runner
    steps:
      - uses: christopherhx/gitea-download-artifact@v4
        with:
          name: myApp #记得修改为上传时配置的name
      - name: Deploy to IIS
        shell: pwsh
        run: |
          $currentIISPath = ${{ vars.IIS_APP_PATH }}
          if(([string]::IsNullOrEmpty($currentIISPath)) -Or (-Not (Test-Path $currentIISPath))) #判断iis站点目录是否存在
          {
              Write-Host Cannot deploy - IIS path is wrong
              exit 1
          }
          if(-Not (Test-Path $currentIISPath\app_offline.htm))
          {
              New-Item -Path $currentIISPath -Name "app_offline.htm" -ItemType "file"
          }

          $parentPath = Split-Path -parent $currentIISPath
          $packageFolderName = Split-Path -leaf $currentIISPath
          $backupFileName = "${packageFolderName}_backup.zip"
          $backupPath = Join-Path -Path $parentPath -ChildPath $backupFileName
          Compress-Archive -Path $currentIISPath -DestinationPath $backupPath -Force #压缩备份
          Write-Host "Copying files to IIS site path"
          $excludeFiles = @("appsettings.json", "web.config") #排除appsettings.json和web.config
          $excludeParams = $excludeFiles | ForEach-Object { "/XF", $_ }
          #复制所有子目录,包括空目录, 只复制源文件较新的文件,目标中源目录不存在的文件不删除,排除appsetting.json web.config,失败时重试2次,重试等待1s
          robocopy . $currentIISPath /E /XO /XN /XX $excludeParams /R:2 /W:1

          Remove-Item -Path $currentIISPath\app_offline.htm
          exit 0
posted @ 2025-06-23 11:47  turingguo  阅读(44)  评论(0)    收藏  举报