작업 명세의 action 블록

작업 명세의 action 블록 (action block in the job specification)

배치 job -> group -> task -> **action**

action 블록은 작업 작성자(author)가 사용자 지정 명령을 정의할 수 있게 해줘요. 이 명령은 필요한 권한을 가진 운영자가 실행 중인 할당에서 실행할 수 있어, 태스크와 상호작용하는 제어된 방법을 제공해요.

액션의 이름은 영숫자 문자와 대시(dash)를 포함할 수 있어요. 이 이름은 태스크 내에서 고유해야 하며 128자를 초과할 수 없어요.

출처: 문서

본문

action 매개변수 (action Parameters)

  • command (string: <required>) — 실행할 명령을 지정해요.
  • args (array<string>: []) — 명령에 전달할 인수 목록을 제공해요.
job "my-job" {
  group "my-group" {
    task "my-task" {
      action "get-changelog" {
        command = "/usr/bin/curl"
        args = [
          "-s",
          "https://raw.githubusercontent.com/hashicorp/nomad/main/CHANGELOG.md"
        ]
      }
      # ...
    }
  }
}

action 예제 (action Examples)

기본 액션 (Basic Action)

이 예제는 현재 날짜와 시간을 출력하는 간단한 액션을 보여줘요:

job "example" {
  # ...
  group "demo" {
    # ...
    task "show-date" {
      # ...
      action "current-date" {
        command = "/bin/date"
      }
    }
  }
}

인수가 있는 액션 (Action with Arguments)

여기서 액션은 인수를 사용해 특정 작업을 수행해요:

job "example" {
  # ...
  group "demo" {
    # ...
    task "list-files" {
      # ...
      action "list-tmp" {
        command = "/bin/ls"
        args    = ["-l", "/tmp"]
      }
    }
  }
}

템플릿이 있는 액션 (Action with Template)

이 고급 예제는 셸 스크립트를 사용해 Nomad GitHub 저장소에서 최신 변경 로그를 가져와 포맷하는 액션을 보여줘요. 내장 환경 변수와 여러 줄 스크립트로 템플릿 사용을 보여줘요.

action "fetch-latest-nomad-changelog" {
  command = "/bin/sh"
  args    = ["-c",
    <<EOT
curl -s https://raw.githubusercontent.com/hashicorp/nomad/main/CHANGELOG.md |

awk 'BEGIN{
    # Setting record and field separators
    RS="## "; FS="\n";
    section=""; count=0
}
{
    # Processing only the first 3 sections after the header
    if (count < 3 && NR > 1){
        # Splitting the version line into components
        split($1, versionInfo, /[()]/);
        version=versionInfo[1];
        gsub(" ", "", version); # Removing spaces from version
        releaseDate=versionInfo[2];
        # Formatting URL components
        urlVersion=version; gsub("[\.]", "", urlVersion); # Remove dots from version
        urlDate=releaseDate; gsub(" ", "-", urlDate); gsub(",", "", urlDate); # Replace spaces with hyphens and remove comma
        # Counting items under each section
        for(i=1; i<=NF; i++){
            if($i ~ /^[A-Z ]+:$/){
                gsub(":", "", $i);
                section=$i;
                itemCount[section]=0;
            }
            if(section && $i ~ /^\*/){
                itemCount[section]++;
            }
        }
        # Printing the extracted information
        printf "Version: %s\nRelease Date: %s\n", version, releaseDate;
        for (s in itemCount) {
            printf "%d %s, ", itemCount[s], s;
        }
        printf "\nLink: https://github.com/hashicorp/nomad/blob/main/CHANGELOG.md#%s-%s\n\n", urlVersion, tolower(urlDate);
        delete itemCount;  # Clear the itemCount array for the next version
        count++;
    }
}'
    EOT
  ]
}

더 알아보기 (Learn more)