[Github Action] Set Zenhub Pipeline With workflow_dispatch
Jan 02, 2024
![[Github Action] Set Zenhub Pipeline With workflow_dispatch](https://image.inblog.dev?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255BGithub%2520Action%255D%2520Set%2520Zenhub%2520Pipeline%2520With%2520workflow_dispatch%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3Dyoung9xo&w=2048&q=75)
issue
- HCR repo에 수동으로 다음의 github action을 trigger할 수 있는 기능을 추가하기
- qc-local-done인 이슈티켓의 상태를 다음과 같이 바꾸는 작업
- qc-deploy-done로 라벨 변경
- pipeline ready to QA로 옮기기
prerequisite
- Generate Zenhub REST API Token
- Zenhub Dashboard에서 REST API token를 발급받는다.
- 회사로 등록된 계정의 경우 Zenhub REST API를 사용하기 위해서는 개인 REST API token를 발급받으면 된다.

- Find github repository_id and zenhub workspace_id, pipeline_id with Postman
- 아래 페이지를 참고해 Postman을 통해 move an issues between pipelines API 에 필요한 params(repository_id, workspace_id, pipeline_id)의 값들을 미리 확인한다.
instance.post(`/workspaces/:workspace_id/repositories/:repo_id/issues/${issueNumber}/moves`, {
pipeline_id: 'pipeline_id',
position: 'top',
})
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이든 상관없다.
- 명령어 실행 :
gh workflow run
[yml에 등록한 workflow name] --ref [해당 파일이 있는 branch name] -F [inputs의 key=value]
ex) gh workflow run after-qc-deploy --ref develop -F token={{zenhub REST API token}}
- 실행 결과
- gh The requested URL returned error: 403이라는 에러 시 조치방법



- repository > Actions tab > after-qc-deploy workflow 페이지로 들어가 Run workflow 버튼을 통해 실행할 수도 있다.

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에 이미 있다면 설치되지 않는다.

result

reference
Share article