首页 > 解决方案 > Github Actions / 从分支名称中提取 TicketID

问题描述

您当前可以通过以下方式设置环境变量:

  - name: Configure Environment Variables
    uses: allenevans/set-env@v1.0.0
    with:
      CDN_PATH: app-foo/${{ github.run_id }}
      CDN_URL: 'https://cdn.mycompany.com'
      JIRA_TICKET_ID: ${{ match(github.ref, ...) }} # How can I extract a string from a branch name?

如何从分支名称中提取字符串?

JIRA_TICKET_ID: ${{ match(github.ref, ...) }}

标签: regexgithubyamlgithub-actions

解决方案


我不相信 github 操作中有内置函数可以做到这一点。但是你可以在你的行动之前运行一个步骤来为你获取 jira 票。

注意:您需要将 sed 正则表达式修改为获取票证的正则表达式。现在它只从 ref 中获取分支名称

- id: getjiraticket
  run:  echo "::set-output name=jiraticketid::`echo "${{ github.ref }}" | sed 's/.*\///'`"
- name: Configure Environment Variables
  uses: allenevans/set-env@v1.0.0
  with:
    CDN_PATH: app-foo/${{ github.run_id }}
    CDN_URL: 'https://cdn.mycompany.com'
    JIRA_TICKET_ID: ${{ steps.getjiraticket.outputs.jiraticketid }}

推荐阅读