GitHub Action - 如何在多个环境上部署版本?

for*_*u82 5 github github-actions

就我而言,有四种环境:Dev、QA、UAT 和 Prod,基于 Windows 的自托管运行器。一旦构建了包,就应该以 Dev\xef\x83\xa0QA\xef\x83\xa0UAT\xef\x83\xa0PROD 的方式成功地将其部署到每个环境中。下面的工作流程是为开发环境构建和部署包而编写的。现在,要在 QA、UAT 和 Prod 环境上进一步部署该版本,是否应针对 QA、UAT 和 Prod 重复相同的部署部分。是否没有像类这样的概念,可以使用参数多次调用部署版本的步骤?

\n

工作流程-API

\n
\n

构建部分

\n
\n
name: PASH-API-Build-Deployment \non: \n push:\n    branches: [dev]\n    paths: \n    - "Pash.Web/**" \njobs:\n build:\n    runs-on: dev-build\n    strategy:\n      matrix:\n        node-version: 3.1.301\n    env:\n      api-project:  "./Pash.Web/PASH.Api/PASH.Api.csproj"\n      test-project:  "./Pash.Web/PASH.Api.Test/PASH.Api.Test.csproj"\n      upload-artifacts-path: "./pashdotnetcorewebapp"\n      artifact-package-zip-directory-path: "_PASH-API-CI/Pash-drop"\n      \n    steps:\n    - name: Generate build number\n      id: buildnumber\n      uses: einaregilsson/build-number@v3 \n      with:\n        token: ${{ secrets.github_token }}\n    - name: Print build number - env\n      run: |\n       echo "::set-output name=VAR-BUILD-NUMBER::${env:BUILD_NUMBER}"\n       echo Build Id first way = ${env:BUILD_NUMBER}\n       echo Build Id second way = ${{ steps.buildnumber.outputs.build_number }}\n    \n    - uses: actions/checkout@v2\n\n    - name: Setup .NET Core - ${{ matrix.node-version }}\n      uses: actions/setup-dotnet@v1\n      with:\n        dotnet-version: ${{ matrix.node-version }}\n\n    - name: Install dependencies\n      run: dotnet restore ${{env.api-project}}\n\n    - name: Build\n      run: dotnet build ${{env.api-project}} --configuration Release --no-restore\n\n    - name: Test\n      run: dotnet test ${{env.test-project}} --no-restore --verbosity normal\n\n    - name: Publish\n      run: dotnet publish ${{env.api-project}} -c Release -o pashdotnetcorewebapp\n\n    - name: publish artifacts\n      uses: actions/upload-artifact@v2\n      with:\n       name: pash-api-artifact-${{ steps.buildnumber.outputs.build_number }}\n       path: ${{ env.upload-artifacts-path }}\n\n    - name: Store Build Number on Build Server \n      run: echo ${{ steps.buildnumber.outputs.build_number }} > ${{github.workspace}}/buildnumberapi.txt\n    \n    - name: Publish Upload Build File \n      uses: actions/upload-artifact@v2\n      with:\n        name: buildnumberapi\n        path: ${{github.workspace}}/buildnumberapi.txt\n
Run Code Online (Sandbox Code Playgroud)\n
\n

部署部分

\n
\n
  deploy:\n    needs: [build]\n    runs-on: dev-deploy\n\n    env:\n      deploy-powershell-script:  "D:\\\\github-deploy-ps.ps1"\n      stop-powershell-script:  "D:\\\\stop-website-ps.ps1"\n      pool-name: "PASHAPIPool"\n      site-name: "PASHServicesAPISite"\n\n    steps:\n    #- name: Print Build Number from BUILD job\n      #run: echo ${{ needs.build.output.VAR-BUILD-NUMBER}}\n\n    - name: Download Build File\n      uses: actions/download-artifact@v2\n      with:\n        name: buildnumberapi\n        path: ${{github.workspace}}\n\n    - name: Print build ID\n      run: |\n        $varbuildnumber = cat ${{github.workspace}}/buildnumberapi.txt\n        echo The result is $varbuildnumber\n        echo "::set-output name=var-build-number::$varbuildnumber"\n      id: selectbuildID\n\n    - name: Download Release Artifacts\n      uses: actions/download-artifact@v2\n      with:\n        name: pash-api-artifact-${{steps.selectbuildID.outputs.var-build-number}}\n        path: ${{github.workspace}}/${{ env.artifact-package-zip-directory-path }}\n \n    - name: Run Powershell commands to deploy release\n      run: |\n        & '${{env.deploy-powershell-script}}' ${{env.pool-name}} ${{env.site-name}} ${{env.physical-path}} ${{ env.port-name}} \n
Run Code Online (Sandbox Code Playgroud)\n

现在的问题是,是否应该在 QA、UAT 和 Prod 的相同工作流程中再次重复相同的部署部分。这似乎不是一个好的选择?我是 GitHub Actions 的新手,您的建议将会非常有帮助。

\n

Krz*_*tof 6

有这样一个概念,叫做复合动作

您所需要的只是创建一个包含参数化步骤的文件。

例如octocat/say-hello/action.yml

inputs:
  name: 
    description: 'Your name'
    default: 'No name provided'
runs:
  using: "composite"
  steps: 
    - run: echo Hello ${{ inputs.name }}.
      shell: bash
    - run: echo "Nice to meet you!"
      shell: pwsh
Run Code Online (Sandbox Code Playgroud)

然后在您的工作流程中使用:

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - uses: octocat/say-hello@v1
      with: 
        name: OctoCat
Run Code Online (Sandbox Code Playgroud)

您的环境应该可用。请检查一下:

  • 复合动作
inputs:
  name: 
    description: 'Your name'
    default: 'No name provided'
  upload-artifacts-path: 
    description: 'Path'
    default: "some/path"
runs:
  using: "composite"
  steps: 
    - run: echo Hello ${{ inputs.name }}.
      shell: bash
    - run: echo "Nice to meet you!"
      shell: bash
    - run: echo "$deploy_powershell_script"
      shell: bash
    - run: echo "$stop_powershell_script"
      shell: bash
    - run: echo "${{ inputs.upload-artifacts-path }}"
      shell: bash
    
Run Code Online (Sandbox Code Playgroud)
  • 工作流程文件
name: composite
on:
  workflow_dispatch:

env:
  deploy_powershell_script:  "D:\\github-deploy-ps.ps1"


jobs:
  build:
    runs-on: windows-latest

    env:
      stop_powershell_script:  "D:\\stop-website-ps.ps1"
      upload_artifacts_path: "./pashdotnetcorewebapp"
      
    steps:
    - uses: actions/checkout@v2
    - uses: ./.github/actions/say-hello
      with: 
        name: OctoCat
        upload-artifacts-path: "$upload_artifacts_path"
    - run: echo "$deploy_powershell_script"
      shell: pwsh
    - run: echo "$upload_artifacts_path"
      shell: pwsh
    - name: Dump steps context
      env:
        STEPS_CONTEXT: ${{ toJson(steps) }}
      run: echo "$STEPS_CONTEXT"
Run Code Online (Sandbox Code Playgroud)
  • 日志
Run ./.github/actions/say-hello
  with:
    name: OctoCat
    upload-artifacts-path: $upload_artifacts_path
  env:
    deploy_powershell_script: D:\github-deploy-ps.ps1
    stop_powershell_script: D:\stop-website-ps.ps1
    upload_artifacts_path: ./pashdotnetcorewebapp
Hello OctoCat.
Nice to meet you!
D:\github-deploy-ps.ps1
D:\stop-website-ps.ps1
./pashdotnetcorewebapp
Run Code Online (Sandbox Code Playgroud)

我在这里测试了访问在作业级别定义的变量和环境变量的几个方面可在复合操作中使用。

但是,如果您希望将输入定义为环境变量,则需要为每个步骤单独映射它们。