-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(workflows): add execute with and without arguments #10255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iennae
merged 3 commits into
GoogleCloudPlatform:main
from
alarconesparza:alarconesparza-update-workflows-507097366
Apr 30, 2026
+394
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
workflows/cloud-client/resources/myFirstWorkflow.workflows.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START workflows_myfirstworkflow_yaml_java] | ||
| # This workflow accepts an optional "searchTerm" argument for the Wikipedia API. | ||
| # If no input arguments are provided or "searchTerm" is absent, | ||
| # it will fetch the day of the week in Amsterdam and use it as the search term. | ||
|
|
||
| main: | ||
| params: [input] | ||
| steps: | ||
| - validateSearchTermAndRedirectToReadWikipedia: | ||
| switch: | ||
| - condition: '${map.get(input, "searchTerm") != null}' | ||
| assign: | ||
| - searchTerm: '${input.searchTerm}' | ||
| next: readWikipedia | ||
| - getCurrentTime: | ||
| call: http.get | ||
| args: | ||
| url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam | ||
| result: currentTime | ||
| - setFromCallResult: | ||
| assign: | ||
| - searchTerm: '${currentTime.body.dayOfWeek}' | ||
| - readWikipedia: | ||
| call: http.get | ||
| args: | ||
| url: 'https://en.wikipedia.org/w/api.php' | ||
| query: | ||
| action: opensearch | ||
| search: '${searchTerm}' | ||
| result: wikiResult | ||
| - returnOutput: | ||
| return: '${wikiResult.body[1]}' | ||
| # [END workflows_myfirstworkflow_yaml_java] |
78 changes: 78 additions & 0 deletions
78
workflows/cloud-client/src/main/java/com/example/workflows/ExecuteWithArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.workflows; | ||
|
|
||
| import com.google.cloud.workflows.executions.v1.CreateExecutionRequest; | ||
| import com.google.cloud.workflows.executions.v1.Execution; | ||
| import com.google.cloud.workflows.executions.v1.ExecutionsClient; | ||
| import com.google.cloud.workflows.executions.v1.WorkflowName; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| // [START workflows_execute_with_arguments] | ||
|
|
||
| public class ExecuteWithArguments { | ||
| public static void main(String[] args) | ||
| throws IOException, InterruptedException, ExecutionException { | ||
|
alarconesparza marked this conversation as resolved.
|
||
| String projectId = "your-project-id"; | ||
| String location = "your-location"; // For example: us-central1 | ||
| String workflowId = "your-workflow-id"; | ||
|
|
||
| executeWorkflowWithArguments(projectId, location, workflowId); | ||
| } | ||
|
|
||
| public static void executeWorkflowWithArguments( | ||
| String projectId, String location, String workflowId) | ||
| throws IOException, InterruptedException, ExecutionException { | ||
|
alarconesparza marked this conversation as resolved.
|
||
| try (ExecutionsClient executionsClient = ExecutionsClient.create()) { | ||
| WorkflowName parent = WorkflowName.of(projectId, location, workflowId); | ||
|
|
||
| CreateExecutionRequest executionRequest = | ||
| CreateExecutionRequest.newBuilder() | ||
| .setParent(parent.toString()) | ||
| .setExecution( | ||
| Execution.newBuilder().setArgument("{\"searchTerm\":\"Cloud\"}").build()) | ||
| .build(); | ||
|
|
||
| Execution executionCreated = executionsClient.createExecution(executionRequest); | ||
|
|
||
| System.out.println("Created execution: " + executionCreated.getName()); | ||
|
|
||
| // Wait for execution to finish using exponential backoff | ||
| long backoffDelay = 1_000; | ||
| Execution execution; | ||
| System.out.println("Poll for result..."); | ||
| while (true) { | ||
| execution = executionsClient.getExecution(executionCreated.getName()); | ||
|
|
||
| // Check if execution finished | ||
| if (execution.getState() != Execution.State.ACTIVE) { | ||
| break; | ||
| } | ||
|
|
||
| // Wait using exponential backoff | ||
| System.out.println("- Waiting for results..."); | ||
| Thread.sleep(backoffDelay); | ||
| backoffDelay *= 2; | ||
| } | ||
|
|
||
| System.out.println("Execution finished with state: " + execution.getState().name()); | ||
| System.out.println("Execution results: " + execution.getResult()); | ||
| } | ||
| } | ||
| } | ||
| // [END workflows_execute_with_arguments] | ||
77 changes: 77 additions & 0 deletions
77
workflows/cloud-client/src/main/java/com/example/workflows/ExecuteWithoutArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.workflows; | ||
|
|
||
| // [START workflows_execute_without_arguments] | ||
|
|
||
| import com.google.cloud.workflows.executions.v1.CreateExecutionRequest; | ||
| import com.google.cloud.workflows.executions.v1.Execution; | ||
| import com.google.cloud.workflows.executions.v1.ExecutionsClient; | ||
| import com.google.cloud.workflows.executions.v1.WorkflowName; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
alarconesparza marked this conversation as resolved.
|
||
|
|
||
| public class ExecuteWithoutArguments { | ||
| public static void main(String[] args) | ||
| throws IOException, InterruptedException, ExecutionException { | ||
|
alarconesparza marked this conversation as resolved.
|
||
| String projectId = "your-project-id"; | ||
| String location = "your-location"; // For example: us-central1 | ||
| String workflowId = "your-workflow-id"; | ||
|
|
||
| executeWorkflowWithoutArguments(projectId, location, workflowId); | ||
| } | ||
|
|
||
| public static void executeWorkflowWithoutArguments( | ||
| String projectId, String location, String workflowId) | ||
| throws IOException, InterruptedException, ExecutionException { | ||
|
alarconesparza marked this conversation as resolved.
|
||
| try (ExecutionsClient executionsClient = ExecutionsClient.create()) { | ||
| WorkflowName parent = WorkflowName.of(projectId, location, workflowId); | ||
|
|
||
| CreateExecutionRequest executionRequest = | ||
| CreateExecutionRequest.newBuilder() | ||
| .setParent(parent.toString()) | ||
| .setExecution(Execution.newBuilder().build()) | ||
| .build(); | ||
|
|
||
| Execution executionCreated = executionsClient.createExecution(executionRequest); | ||
|
|
||
| System.out.println("Created execution: " + executionCreated.getName()); | ||
|
|
||
| // Wait for execution to finish using exponential backoff | ||
| long backoffDelay = 1_000; | ||
| Execution execution; | ||
| System.out.println("Poll for result..."); | ||
| while (true) { | ||
| execution = executionsClient.getExecution(executionCreated.getName()); | ||
|
|
||
| // Check if execution finished | ||
| if (execution.getState() != Execution.State.ACTIVE) { | ||
| break; | ||
| } | ||
|
|
||
| // Wait using exponential backoff | ||
| System.out.println("- Waiting for results..."); | ||
| Thread.sleep(backoffDelay); | ||
| backoffDelay *= 2; | ||
| } | ||
|
|
||
| System.out.println("Execution finished with state: " + execution.getState().name()); | ||
| System.out.println("Execution results: " + execution.getResult()); | ||
| } | ||
| } | ||
| } | ||
| // [END workflows_execute_without_arguments] | ||
72 changes: 72 additions & 0 deletions
72
workflows/cloud-client/src/test/java/com/example/workflows/ExecuteWithArgumentsIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.workflows; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.PrintStream; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| public class ExecuteWithArgumentsIT { | ||
| private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
| private static String LOCATION = "us-central1"; | ||
| private static String WORKFLOW_ID = "java_myFirstWorkflow_" + UUID.randomUUID(); | ||
| private static ByteArrayOutputStream bout; | ||
|
|
||
| private static void requireEnvVar(String varName) { | ||
| assertNotNull( | ||
| "Environment variable " + varName + " is required to perform these tests.", | ||
| System.getenv(varName)); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setUp() throws IOException, InterruptedException, ExecutionException { | ||
| requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
|
|
||
| // Create workflow | ||
| Utils.createWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| bout = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(bout)); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDown() throws IOException, InterruptedException, ExecutionException { | ||
| // Delete workflow | ||
| Utils.deleteWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| System.setOut(null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteWorkflowWithArguments() | ||
| throws IOException, InterruptedException, ExecutionException { | ||
| ExecuteWithArguments.executeWorkflowWithArguments(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| String output = bout.toString(); | ||
| assertThat(output).contains("Execution finished with state: SUCCEEDED"); | ||
| assertThat(output).contains("Execution results: "); | ||
| assertThat(output).contains("Cloud"); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
workflows/cloud-client/src/test/java/com/example/workflows/ExecuteWithoutArgumentsIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.workflows; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.PrintStream; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| public class ExecuteWithoutArgumentsIT { | ||
| private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
| private static String LOCATION = "us-central1"; | ||
| private static String WORKFLOW_ID = "java_myFirstWorkflow_" + UUID.randomUUID(); | ||
| private static ByteArrayOutputStream bout; | ||
|
|
||
| private static void requireEnvVar(String varName) { | ||
| assertNotNull( | ||
| "Environment variable " + varName + " is required to perform these tests.", | ||
| System.getenv(varName)); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setUp() throws IOException, InterruptedException, ExecutionException { | ||
| requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
|
|
||
| // Create workflow | ||
| Utils.createWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| bout = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(bout)); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDown() throws IOException, InterruptedException, ExecutionException { | ||
| // Delete workflow | ||
| Utils.deleteWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| System.setOut(null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteWorkflowWithoutArguments() | ||
| throws IOException, InterruptedException, ExecutionException { | ||
| ExecuteWithoutArguments.executeWorkflowWithoutArguments(PROJECT_ID, LOCATION, WORKFLOW_ID); | ||
|
|
||
| String output = bout.toString(); | ||
| assertThat(output).contains("Execution finished with state: SUCCEEDED"); | ||
| assertThat(output).contains("Execution results: "); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.