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

如何在GitHub Actions中从JSON值创建环境变量

从GitHub Issue表单解析结果中提取指定字段作为环境变量

问题描述

我通过GitHub Issue表单收集用户输入,提交后用issue-ops/parser解析表单内容,执行echo ${{ steps.parser.outputs.json }}得到的输出格式如下:

source_branch:DEV target_branch:UAT folders_double_quoted_comma_separated_within:_No response_ contact_details:_No response_

我需要把其中source_branch、target_branch、folders_double_quoted_comma_separated_within这三个字段的值单独提取出来,作为环境变量传递给后续任务。但目前Test步骤中$SOURCE_BRANCH输出的是完整的解析结果,并非预期的单个字段值。

当前使用的GitHub Actions配置如下:

name: JSON

on:
  issues:
    types: [opened]

jobs:
  issue-retrieval:
    runs-on: ubuntu-latest

    permissions:
      issues: write
      contents: read
      packages: read

    steps:
      - name: Parse Issue
        id: parser
        uses: issue-ops/parser@v4.0.0
        with:
          body: ${{ github.event.issue.body }}

      - name: Output Issue JSON
        id: output-issue
        run: |
          echo ${{ steps.parser.outputs.json }}

          # Set environment variables
          echo "SOURCE_BRANCH=$(echo ${{ steps.parser.outputs.json }})" >> $GITHUB_ENV
          echo "TARGET_BRANCH=target_branch" >> $GITHUB_ENV
          echo "FOLDERS=folders_double_quoted_comma_separated_within" >> $GITHUB_ENV
          
      - name: Test
        run: |
          echo $SOURCE_BRANCH
          echo $TARGET_BRANCH
          echo $FOLDERS

解决办法

方法一:直接用Shell命令提取字段值

无需额外Action,通过awk命令从解析结果中提取目标字段的值,修改Output Issue JSON步骤的run逻辑即可:

- name: Output Issue JSON
  id: output-issue
  run: |
    echo ${{ steps.parser.outputs.json }}

    # 提取并设置环境变量
    echo "SOURCE_BRANCH=$(echo '${{ steps.parser.outputs.json }}' | awk -F 'source_branch:| ' '{print $2}')" >> $GITHUB_ENV
    echo "TARGET_BRANCH=$(echo '${{ steps.parser.outputs.json }}' | awk -F 'target_branch:| ' '{print $2}')" >> $GITHUB_ENV
    echo "FOLDERS=$(echo '${{ steps.parser.outputs.json }}' | awk -F 'folders_double_quoted_comma_separated_within:| ' '{print $2}')" >> $GITHUB_ENV

原理是用awk指定字段分隔符(目标键+空格),提取键后面的第一个值。用单引号包裹解析结果,避免Shell解析特殊字符。

方法二:使用actions/github-script解析(更灵活)

通过GitHub官方的actions/github-script Action,用JavaScript将空格分隔的键值对转为对象,再设置环境变量:

- name: Parse Issue
  id: parser
  uses: issue-ops/parser@v4.0.0
  with:
    body: ${{ github.event.issue.body }}

# 添加这个步骤替换原有的Output Issue JSON步骤
- name: Extract fields and set env vars
  uses: actions/github-script@v6
  env:
    ISSUE_PARSED_RESULT: ${{ steps.parser.outputs.json }}
  with:
    script: |
      const resultStr = process.env.ISSUE_PARSED_RESULT.trim();
      // 将空格分隔的键值对转换为对象
      const fieldMap = resultStr.split(' ').reduce((map, pair) => {
        const [key, value] = pair.split(':');
        map[key] = value;
        return map;
      }, {});
      
      // 设置环境变量
      core.exportVariable('SOURCE_BRANCH', fieldMap.source_branch);
      core.exportVariable('TARGET_BRANCH', fieldMap.target_branch);
      core.exportVariable('FOLDERS', fieldMap.folders_double_quoted_comma_separated_within);

- name: Test
  run: |
    echo $SOURCE_BRANCH
    echo $TARGET_BRANCH
    echo $FOLDERS

这种方式适合更复杂的解析场景,比如字段值包含空格的情况(需要调整分割逻辑),扩展性更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 13:10:15