Dev./Github Actions

[Cloud] CD: Github Actions - workflow_run & Dependency

Ivan'show 2023. 8. 23.
728x90
반응형

workflow_run & Dependency

workflow 의 dependency 설정 (조건부 실행)

workflow_dispatch 로 직접 실행시키게 되면, call-workflow step 에 따라 정해진 workflow 가 실행된다.

# lesson4_caller.yml

name: call workflow

on:
    workflow_dispatch:

jobs:
    get-workflow-name:
        name: echo workflow name
        runs-on: ubuntu-latest
        steps:
            - run: echo "This workflow is ${{ github.workflow }}"

    call-workflow:
        uses: ./.github/workflows/lesson4.yml
        strategy:
            matrix:
                person: ["Ivan", "Kim", "MH"]
        with:
            person: ${{ matrix.person }} # with 으로 변수를 넘겨주는
# lesson4.yml
name: resusable workflow

on:
    workflow_call:
        inputs:
            person:
                required: true
                type: string
    

jobs:
    get-workflow-name:
        name: echo workflow name
        runs-on: ubuntu-latest
        steps:
            -   run: echo "This workflow is ${{ github.workflow }}"

    greeting:
        name: greeting
        runs-on: ubuntu-latest
        steps:
            -   name: greeting
                run: echo "Hello, ${{ inputs.person }}"

또한 다른 workflow 에서 dependency 로 지정해 놓은 부분에서도 trigger 가 발동되 실행된다.

# lesson5.yml
name: workflow run
on:
    workflow_run: # 특정 workflow 의 상태에 따라서 실행
        workflows: ["call workflow"]
        types:
            - completed

jobs:
    on-success:
        runs-on: ubuntu-latest
        if: ${{ github.event.workflow_run.conclusion == 'success' }}
        steps:
            - run: echo "workflow success"

    on-failure:
        runs-on: ubuntu-latest
        if: ${{ github.event.workflow_run.conclusion == 'failure' }}
        steps:
            - run: echo "workflow failed"

trigger 에는 work flow 뿐만아니라 github 에서 제공하는 trigger 의 형태도 있다.

  • pull-request
  • push
  • release
  • schedule
  • workflow_dispatch
  • issue

예를들어, pull_request 가 발동되면 workflow 를 따라서 자동으로 실행되게 dependency 로 만들어 줄 수 있다.

# lesson5_up.yml
name: Upload PR data

on:
    pull_request:

jobs:
    upload:
        runs-on: ubuntu-latest
        steps:
            - name: Save PR number
              env:
                PR_NUMBER: ${{ github.event.number }}
              run: |
                mkdir ./pr
                echo $PR_NUMBER > ./pr/pr_number
            - uses: actions/upload-artifact@v3
              with:
                name: pr_number
                path: pr/
# lesson5_use.yml
name: Use the PR data

on:
    workflow_run:
        workflows: ["Upload PR data"]
        types:
            - completed

jobs:
    download:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/github-script@v6
              name: download artifact
              with:
                  script: |
                    let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
                      owner: context.repo.owner,
                      repo: context.repo.repo,
                      run_id: context.payload.workflow_run.id,
                    });
                    let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
                        return artifact.name == "pr_number"
                    })[0];
                    let download = await github.rest.actions.downloadArtifact({
                        owner: context.repo.owner,
                        repo: context.repo.repo,
                        artifact_id: matchArtifact.id,
                        archive_format: 'zip',
                    });
                    let fs = require('fs');
                    let filePath = `${process.env.GITHUB_WORKSPACE}/pr_number.zip`
                    fs.writeFileSync(filePath, Buffer.from(download.data));
            - name: 'Unzip artifact'
              run: unzip pr_number.zip

            - name: Comment on PR
              uses: actions/github-script@v6
              with:
                github-token: ${{ secrets.GITHUB_TOKEN }}
                script: |
                  let fs = require('fs');
                  let pr_number = Number(fs.readFileSync('./pr_number'));
                  await github.rest.issues.createComment({
                    owner: context.repo.owner,
                    repo: context.repo.repo,
                    issue_number: pr_number,
                    body: "Thank you for the PR!"
                  });

728x90
반응형

댓글