Github Action使用现有的Python脚本并获得字符串输出。

问题描述 投票:0回答:1

我是第一次参加工作 Github Action 用于在安卓系统中设置CICD工作流程

我有一个现有的 Python script 其中产生 Change log 作为 HTML String out put.

我想使用在github行动和接收变化日志作为字符串输出。

我有这样的东西

- name: Generate Change logs
  id: changelogs
  working-directory: .github/workflows/
  run: python release_mail_generator.py ${{ secrets.GITHUB_TOKEN }}  v2.21

想用这样的东西来获得 changelog 输出字符串

- name: Generate Change logs
  id: changelogs
  working-directory: .github/workflows/
  run: echo "::set-output name=changelog::$(echo python release_mail_generator.py ${{ secrets.GITHUB_TOKEN }}  v2.21)"

我的python脚本

from __future__ import print_function

import sys
from collections import defaultdict
from github import Github

# Install dependencies using command pip3 install -r scripts/delivery/requirements.txt
# Run script by running command python3 scripts/delivery/release_mail_generator.py ${token} ${milestone}
class ReleaseMailGenerator:
    MASTER_LABEL_LIST = set(
        ["Collections", "Core Experience", "Growth", "Platform", "Product Initiatives", "User Success", "Translations"]
    )
    PAGE_ITEM_COUNT = 200

    TITLE_TAG_TMPL = '<h2>{}</h2>'
    LABEL_SECTION_TMPL = '<br/><b><i>{}</i></b>'
    LIST_OPEN_TAG = '<ul style="list-style-type:disc">'
    LIST_LINE_ITEM_TMPL = '<li>{}<a target="_blank" href=\"{}\"> (#{})</a> by {}<br/></li>'
    LIST_CLOSE_TAG = '</ul>'
    MESSAGE = '<p>Android app {} has been pushed to Play Store on internal track. Please review the build and give sign off soon</p>'
    INTERNAL_TEST_DISCLAIMER = '<p>Click on the following link to join internal test distribution program on Play Store</p>' + '<a href="https://play.google.com/apps/internaltest/xxxxxxxx">https://play.google.com/apps/internaltest/xxxxxx</a>'

    def get_pull_request_details(self, label, pull_requests_list):
        """

        :param label:
        :param pull_requests_list:
        :return:
        """
        pr_details = self.LABEL_SECTION_TMPL.format(label) + self.LIST_OPEN_TAG
        for pr in pull_requests_list:
            if pr.user.name:
                name = pr.user.name
            else:
                name = pr.user.login
            pr_details += self.LIST_LINE_ITEM_TMPL.format(pr.title, pr.html_url, pr.number, name)
        pr_details += self.LIST_CLOSE_TAG

        return pr_details

    def get_pull_request_label_map(self, repo, issues):
        """

        :param issues:
        :param repo:
        :return:
        """
        pull_request_label_map = defaultdict(list)

        for issue in issues:
            if not issue.pull_request:
                continue

            pr = repo.get_pull(issue.number)
            if not pr.is_merged():
                continue

            valid_label_list = set([label.name for label in issue.labels]).intersection(self.MASTER_LABEL_LIST)

            if not valid_label_list:
                pull_request_label_map['Other'].append(issue)

            for label in valid_label_list:
                pull_request_label_map[label].append(issue)
        return pull_request_label_map

    def get_pull_requests(self, repo, milestone_name):

        try:
            milestones = repo.get_milestones()
            for milestone in milestones:
                if milestone.title == milestone_name:
                    milestone = milestone
                    break
            else:
                print('---------------------------------------------------------------------------------------\n')
                print('Incorrect Milestone, Please Check\n')
                print('----------------------------------------------------------------------------------------')
                return
        except Exception:
            print(e)
            return

        return repo.get_issues(milestone, state="closed")

    def get_changelog(self, auth_token, milestone):
        """

        :param auth_token:
        :param milestone:
        :return:
        """

        github = Github(login_or_token=auth_token, per_page=self.PAGE_ITEM_COUNT)
        repo = github.get_repo("karthikkolanji/ShaadiDemo")
        pull_requests = self.get_pull_requests(repo, milestone)
        if not pull_requests:
            print('---------------------------------------------------------------------------------------\n')
            print('No pull requests found for given Milestone, Please check \n')
            print('----------------------------------------------------------------------------------------')
            return
        pull_request_label_map = self.get_pull_request_label_map(repo, pull_requests)

        if not pull_request_label_map:
            print('---------------------------------------------------------------------------------------\n')
            print('No pull requests is merged for given Milestone, Please check \n')
            print('----------------------------------------------------------------------------------------')
            return

        changelog = self.MESSAGE.format(milestone) + self.INTERNAL_TEST_DISCLAIMER + self.TITLE_TAG_TMPL.format('Changelog')
        for label, pull_requests_list in pull_request_label_map.items():
            changelog += self.get_pull_request_details(label, pull_requests_list)

        print(changelog)

        return changelog


length = len(sys.argv)

if length < 3:
    print('This script requires two command line areguments\n1. Github access token\n2. Release milestone')
    sys.exit()

ReleaseMailGenerator().get_changelog(sys.argv[1], sys.argv[2])
python android continuous-integration workflow github-actions
1个回答
1
投票

echo python script.py 是无效的语法。你必须运行python脚本没有 echo 直接在 set-output 命令或将结果赋值到一个变量中,然后将这个变量设置为输出。由于你的 python 脚本会产生多行输出,第二种方案将适合你,因为你需要转义 \r, \n 符号(根据 这个 讨论)。) 所以,这是我的工作配置。

测试python脚本

import sys

print("argument {}".format(sys.argv[1]))
print("changelog 1")
print("changelog 2")
print("changelog 3")

.githubworkflowstest.yml。

---
name: Test

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - run: |
          output=$(python release_mail_generator.py v2.21)
          output="${output//'%'/'%25'}"
          output="${output//$'\n'/'%0A'}"
          output="${output//$'\r'/'%0D'}" 
          echo "::set-output name=changelog::$output"
        id: step1
      - name: Print changelog
        run: echo "${{ steps.step1.outputs.changelog }}"

结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.