Cloud 클라이언트 라이브러리를 사용하여 워크플로 실행
이 빠른 시작에서는 Cloud 클라이언트 라이브러리를 사용하여 워크플로를 실행하고 실행 결과를 확인하는 방법을 보여줍니다.
Cloud 클라이언트 라이브러리 설치 및 개발 환경 설정에 관한 자세한 내용은 Workflows 클라이언트 라이브러리 개요를 참고하세요.
터미널이나 Cloud Shell에서 Google Cloud CLI를 사용하여 다음 단계를 완료할 수 있습니다.
시작하기 전에
조직에서 정의한 보안 제약조건으로 인해 다음 단계를 완료하지 못할 수 있습니다. 문제 해결 정보는 제한된 Google Cloud 환경에서 애플리케이션 개발을 참고하세요.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- (선택사항) 로그를 Cloud Logging으로 전송하려면 서비스 계정에
roles/logging.logWriter
역할을 부여합니다.gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/logging.logWriter"
서비스 계정 역할 및 권한에 대한 자세한 내용은 Google Cloud 리소스에 액세스할 수 있도록 워크플로 권한 부여를 참고하세요.
- 필요한 경우 Git 소스 코드 관리 도구를 다운로드하여 설치합니다.
샘플 워크플로 배포
워크플로를 정의한 후 실행에 사용할 수 있도록 워크플로를 배포합니다. 또한 배포 단계 중 소스 파일을 실행할 수 있는지 확인합니다.
다음 워크플로는 요청을 공개 API에 전송한 후 API의 응답을 반환합니다.
다음 콘텐츠가 포함된 파일 이름이
myFirstWorkflow.yaml
인 텍스트 파일을 만듭니다.워크플로를 만든 후 배포해도 되지만 워크플로를 실행하지는 마세요.
gcloud workflows deploy myFirstWorkflow \ --source=myFirstWorkflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --location=CLOUD_REGION
CLOUD_REGION
을 워크플로의 지원되는 위치로 바꿉니다. 코드 샘플에 사용된 기본 지역은us-central1
입니다.
샘플 코드 가져오기
GitHub에서 샘플 코드를 클론할 수 있습니다.
샘플 앱 저장소를 로컬 머신에 클론합니다.
자바
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
또는 zip 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.
Workflows 샘플 코드가 포함된 디렉터리로 변경합니다.
자바
cd java-docs-samples/workflows/cloud-client/
Node.js
cd nodejs-docs-samples/workflows/quickstart/
Python
cd python-docs-samples/workflows/cloud-client/
다음 샘플 코드를 살펴봅니다. 각 샘플 앱은 다음을 실행합니다.
- Workflows용 클라우드 클라이언트 라이브러리를 설정합니다.
- 워크플로를 실행합니다.
- 실행이 종료될 때까지 워크플로 실행을 폴링합니다(지수 백오프 사용).
- 실행 결과를 출력합니다.
자바
Node.js
Python
샘플 코드 실행
샘플 코드를 실행하고 워크플로를 실행할 수 있습니다. 워크플로를 실행하면 워크플로와 연결된 배포된 워크플로 정의가 실행됩니다.
샘플을 실행하려면 먼저 종속 항목을 설치합니다.
자바
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
스크립트를 실행합니다.
자바
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME mvn compile exec:java -Dexec.mainClass=com.example.workflows.WorkflowsQuickstart
Node.js
npx tsx index.js
Python
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME python3 main.py
다음을 바꿉니다.
PROJECT_ID
: Google Cloud 프로젝트 이름CLOUD_REGION
: 워크플로의 위치 (기본값:us-central1
)WORKFLOW_NAME
: 워크플로 이름 (기본값:myFirstWorkflow
)
출력은 다음과 비슷합니다.
Execution finished with state: SUCCEEDED Execution results: ["Thursday","Thursday Night Football","Thursday (band)","Thursday Island","Thursday (album)","Thursday Next","Thursday at the Square","Thursday's Child (David Bowie song)","Thursday Afternoon","Thursday (film)"]
실행 요청에서 데이터 전달
클라이언트 라이브러리 언어에 따라 실행 요청에서 런타임 인수를 전달할 수도 있습니다. 예를 들면 다음과 같습니다.
자바
// Creates the execution object.
CreateExecutionRequest request =
CreateExecutionRequest.newBuilder()
.setParent(parent.toString())
.setExecution(Execution.newBuilder().setArgument("{\"searchTerm\":\"Friday\"}").build())
.build();
Node.js
// Execute workflow
try {
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
argument: JSON.stringify({"searchTerm": "Friday"})
}
});
const executionName = createExecutionRes[0].name;
런타임 인수 전달에 대한 자세한 내용은 실행 요청에서 런타임 인수 전달을 참조하세요.
삭제
이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 Google Cloud 프로젝트를 삭제합니다.
만든 워크플로를 삭제합니다.
gcloud workflows delete myFirstWorkflow
계속 진행할지 묻는 메시지가 표시되면
y
를 입력합니다.
워크플로가 삭제됩니다.