[Github Action] Set Zenhub Pipeline With workflow_dispatch

Jan 02, 2024
[Github Action] Set Zenhub Pipeline With workflow_dispatch

issue

  • HCR repo에 수동으로 다음의 github action을 trigger할 수 있는 기능을 추가하기
    • qc-local-done인 이슈티켓의 상태를 다음과 같이 바꾸는 작업
        1. qc-deploy-done로 라벨 변경
        1. pipeline ready to QA로 옮기기

prerequisite

  1. Generate Zenhub REST API Token
  • 회사로 등록된 계정의 경우 Zenhub REST API를 사용하기 위해서는 개인 REST API token를 발급받으면 된다.
    • notion image
 
 
  1. Find github repository_id and zenhub workspace_id, pipeline_id with Postman

github-action yaml 작성하기

  • yaml file 위치는 .github/workflows 이다.
    • ex) .github/workflows/after-qc-deploy.yml
  • workflow_dispatch event를 등록하기 위해서는 처음 commit 시 on에 push/pull_request를 넣어준다.
on: push: //이부분 workflow_dispatch: inputs: token: required: true description: "zenhub REST API token" type: string
  • commit 후 gh cli를 이용해 token 정보를 전달한다.
    • 사전조건
      • gh(커맨드 라인으로 GitHub 를 다루는 프로그램) 설치 필요
        • brew install gh
      • 명령어 실행 시 경로가 hub-control-room이면 되고, 어떤 branch이든 상관없다.
      • notion image
         
    • 명령어 실행 : gh workflow run [yml에 등록한 workflow name] --ref [해당 파일이 있는 branch name] -F [inputs의 key=value]
notion image
 
 
name: "after-qc-deploy" on: workflow_dispatch: inputs: token: required: true description: "zenhub REST API token" type: string jobs: change-issues-label: runs-on: ubuntu-latest steps: - name: find issue with 'qc-local-done' label uses: actions/github-script@v7 with: script: | try { (async () => { const { data: issueList } = await github.rest.issues.listForRepo({ owner: context.repo.owner, repo: context.repo.repo, state: 'open', labels: 'qc-local-done', per_page: 100 }) const qCLocalDoneLabelExistIssueList = issueList.map(({ number }) => number) if(qCLocalDoneLabelExistIssueList.length > 0){ core.exportVariable('ISSUE_NUMBER_LIST', qCLocalDoneLabelExistIssueList) } })(); } catch (error){ console.log('[find "qc-local-done" error]', error) } - run: | echo ISSUE_NUMBER_LIST = "$ISSUE_NUMBER_LIST" - name: change to 'qc-deploy-done' label if: ${{ env.ISSUE_NUMBER_LIST != null }} uses: actions/github-script@v7 with: script: | try { const { ISSUE_NUMBER_LIST } = process.env const issueNumberList = JSON.parse(ISSUE_NUMBER_LIST) issueNumberList.forEach((issueNumber)=>{ github.rest.issues.removeLabel({ issue_number: issueNumber, owner: context.repo.owner, repo: context.repo.repo, name: "qc-local-done" }) github.rest.issues.addLabels({ issue_number: issueNumber, owner: context.repo.owner, repo: context.repo.repo, labels: ["qc-deploy-done"] }) }) } catch (error){ console.log('[change label to "local-deploy-done" error]', error) } - name: Set Node.js 17.x uses: actions/setup-node@v4 with: node-version: 17.x - name: Cache node modules uses: actions/cache@v3 id: cache with: path: node_modules key: npm-packages-${{ hashFiles('**/package-lock.json') }} - name: yarn add axios if: steps.cache.outputs.cache-hit != 'true' uses: borales/actions-yarn@v4 with: cmd: add axios - name: set pipeline to 'ready to QA' if: ${{ github.event.inputs.token != null }} uses: actions/github-script@v7 env: ZENHUB_TOKEN: ${{ github.event.inputs.token }} with: script: | try { const axios = require('axios') const { ISSUE_NUMBER_LIST, ZENHUB_TOKEN } = process.env const issueNumberList = JSON.parse(ISSUE_NUMBER_LIST) const instance = axios.create({ baseURL: 'https://api.zenhub.com/p2/', timeout: 1000, headers: {'X-Authentication-Token': ZENHUB_TOKEN} }); issueNumberList.forEach((issueNumber)=>{ instance.post(`/workspaces/:workspace_id/repositories/:repo_id/issues/${issueNumber}/moves`, { pipeline_id: 'pipeline_id', position: 'top', }) .then(function (response) { console.log('axios response', response); }) .catch(function (error) { console.log('axios error', error); }); }) } catch (error){ console.log('[change label to "local-deploy-done" error]', error) }
 
  • axios가 node_modules에 이미 있다면 설치되지 않는다.
notion image
 

result

notion image
 

reference

 
Share article

veganee