windows 获取Azure DevOps上当前正在运行的构建的进程ID

deyfvvtc  于 6个月前  发布在  Windows
关注(0)|答案(1)|浏览(70)

我正在尝试拼凑一些脚本,以将CodeQL扫描添加到Azure DevOps上的现有构建管道中。对于.NET等编译语言,需要使用预编译命令来创建CodeQL数据库以监视编译。我已将其设置如下:
YAML:

parameters:
  - name: githubToken
    default: ''
  - name: buildType
    default: ''
  - name: codeql_db
    default: "codeql-db"

steps:
- script: |
    echo "##vso[task.prependpath]/apps/ado/tools/codeql"
  displayName: 'Setup codeql'
- task: PythonScript@0
  displayName: 'CodeQL setup environment'
  inputs:
    scriptSource: 'filepath'
    scriptPath: '$(Pipeline.Workspace)/utils/codeql_setup.py'
    arguments: '--github-token ${{ parameters.githubToken }} --build-type ${{ parameters.buildType }} --repository-name $(Build.Repository.Name) --repository-path $(Build.Repository.LocalPath) --agent-os $(agent.os) --codeql-db ${{ parameters.codeql_db }}'
    workingDirectory: $(Pipeline.Workspace)

字符串
codeql_setup.py:

if build_type in compiled_buildtypes:
    print('Compiled build type identified. Setting up indirect build tracing.', flush=True)
    codeql_setup_command = ['codeql', 'database', 'init','--source-root', repository_local_path, '--language', repo_languages_argument, '--begin-tracing', codeql_db_name, '--overwrite']
    
    # Set additional options
    if len(repo_languages) > 1 :
        print('Multiple languages detected.', flush=True)
        codeql_setup_command.append('--db-cluster')
    if 'windows' in agent_os.lower():
        print('Windows Agent detected.', flush=True)
        codeql_setup_command.append(f'--trace-process-level {PROCESS_NUMBER}')

    database_init_proc = subprocess.run(codeql_setup_command, env=os.environ.copy())
    print('CodeQL database setup for indirect build tracing.', flush=True)


我的问题是第二个额外的参数。对于Windows代理,进程号或父进程名是codeQL监视编译所必需的。
有没有一种简单的方法来获取构建的进程ID?类似于我检索操作系统的方法。

nbnkbykc

nbnkbykc1#

打开CodeQL扫描码和Github文档,需要获取当前Agent.Worker.exe进程ID。
为了满足您的要求,您可以使用以下PowerShell命令来获取进程ID。

Get-Process Agent.Worker  | Select id

字符串
然后,您可以将ProcessID设置为Pipeline变量。
举例来说:

steps:
- powershell: |
   Get-Process Agent.Worker  | Select id
   
   $test = Get-Process Agent.Worker  | Select id
   
   echo $test.id
   
    echo "##vso[task.setvariable variable=ProcessID]$test.id"


您可以在接下来的任务中使用变量:$(ProcessID)

相关问题