You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署.NET API时GitHub Actions报错:无法找到actions/powershell-script仓库

解决GitHub Actions部署.NET API到Windows服务器的错误

问题描述

用GitHub Actions部署.NET API到远程Windows服务器时,一直报错:Unable to resolve action actions/powershell-script, repository not found,原工作流文件如下:

name: Deploy .NET API

on:
    push:
      branches:
        - master
    workflow_dispatch:


jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '7.x'
          include-prerelease: true

      - name: Build API
        run: dotnet build

      - name: Publish API
        run: dotnet publish

  deploy:
    needs: build

    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v3

      - name: Download build artifacts
        uses: actions/github-script@v6

        with:
          script: |
            github.actions.downloadArtifact('build', {directory: './artifacts'})



        - name: PowerShell script
        uses: Amadevus/pwsh-script@v2.0.3
    

        with:
          script: |
            $ServerHost = ${{ secrets.REMOTE_SERVER_HOST }}
            $ServerUsername = ${{ secrets.REMOTE_SERVER_USERNAME }}
            $ServerPassword = ${{ secrets.REMOTE_SERVER_PASSWORD }}

            # Create a temporary directory
            $TempDir = New-TemporaryItem

            # Copy the build artifacts to the temporary directory
            Copy-Item -Path .\artifacts\* -Destination $TempDir

            # Create a PowerShell script to deploy the API to the remote server
            $DeployScript = New-Item -ItemType File -Path $TempDir\Deploy.ps1 -Force

            Add-Content -Path $DeployScript -Value \"@powershell -NoProfile -ExecutionPolicy Bypass -File $TempDir\Publish.ps1\"

            # Copy the PowerShell script to the remote server
            Copy-Item -Path $DeployScript -Destination \\$ServerHost\C$\Temp

            # Create a PowerShell session on the remote server
            $RemoteSession = New-PSSession -ComputerName $ServerHost -Credential (New-Object System.Net.NetworkCredential($ServerUsername, $ServerPassword))

            # Execute the PowerShell script on the remote server to deploy the API
            Invoke-Command -Session $RemoteSession -ScriptBlock { Import-Module PSReadLine; . \\$ServerHost\C$\Temp\Deploy.ps1 }

            # Close the PowerShell session on the remote server
            Remove-PSSession $RemoteSession

            # Remove the temporary directory
            Remove-Item -Path $TempDir -Force

错误原因

  1. 工作流里的Amadevus/pwsh-script@v2.0.3步骤缩进错误,导致GitHub Actions解析时误判为不存在的actions/powershell-script动作。
  2. 构建阶段未上传构建产物为Artifact,部署阶段根本下载不到有效文件。
  3. PowerShell脚本存在语法问题,比如转义符使用错误、路径引用逻辑冗余,远程执行步骤也无必要绕弯子。

修复后的完整工作流

name: Deploy .NET API

on:
    push:
      branches:
        - master
    workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '7.x'
          include-prerelease: true

      - name: Build API
        run: dotnet build --configuration Release

      - name: Publish API
        run: dotnet publish --configuration Release --output ./publish

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: api-build
          path: ./publish/

  deploy:
    needs: build
    runs-on: windows-latest

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: api-build
          path: ./artifacts

      - name: Deploy to remote Windows server
        shell: pwsh
        run: |
          $ServerHost = "${{ secrets.REMOTE_SERVER_HOST }}"
          $ServerUsername = "${{ secrets.REMOTE_SERVER_USERNAME }}"
          $ServerPassword = "${{ secrets.REMOTE_SERVER_PASSWORD }}"

          # 创建安全凭据对象
          $securePassword = ConvertTo-SecureString $ServerPassword -AsPlainText -Force
          $credential = New-Object System.Management.Automation.PSCredential ($ServerUsername, $securePassword)

          # 建立远程PowerShell会话
          $RemoteSession = New-PSSession -ComputerName $ServerHost -Credential $credential

          # 复制构建产物到远程服务器临时目录
          Copy-Item -Path ./artifacts/* -Destination "\\$ServerHost\C$\Temp\API" -Recurse -Force

          # 在远程服务器执行部署操作(根据你的实际服务调整)
          Invoke-Command -Session $RemoteSession -ScriptBlock {
              # 示例:停止API服务、替换文件、重启服务
              Stop-Service -Name "DotNetAPIService" -ErrorAction SilentlyContinue
              Copy-Item -Path "C:\Temp\API\*" -Destination "C:\inetpub\wwwroot\DotNetAPI" -Recurse -Force
              Start-Service -Name "DotNetAPIService"
          }

          # 清理远程临时文件
          Invoke-Command -Session $RemoteSession -ScriptBlock {
              Remove-Item -Path "C:\Temp\API" -Recurse -Force
          }

          # 关闭远程会话
          Remove-PSSession $RemoteSession

关键修复点

  • 修正缩进问题:删掉第三方pwsh-script动作,改用原生shell: pwsh直接执行脚本,避免解析错误。
  • 完善Artifact流转:构建阶段用actions/upload-artifact上传发布产物,部署阶段用actions/download-artifact下载,流程更可靠。
  • 优化PowerShell逻辑:
    • 改用安全凭据创建方式,避免明文密码风险
    • 删掉冗余的本地临时脚本,直接远程执行部署步骤
    • 修正路径转义和引用问题
  • 升级动作版本:把actions/checkout、actions/setup-dotnet等动作升到最新稳定版,兼容性更好。

内容的提问来源于stack exchange,提问作者Maddyyyy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 10:27:02