How to generate and deploy bpmn with fluent api?

I know how to use the fluent API to generate BPMN.
But have no idea how to deploy the generated BPMN at runtime.

I tried the below code to deploy the BPMN but it does not work.

repositoryService.createDeployment().addModelInstance(key + ".bpmn",instance).deploy();


Deployment deployment = repositoryService.createDeployment().
                            new ByteArrayInputStream(
                                    ((GeneratedProcessDefinition) definition).getBpmn().getBytes())

Above code not throw any exception but after that executed when I try to get the process definition by the key the process engine says there is no such process definition exists.

How can I deploy a BPMN at runtime?

@himly, Refer this docs page for fluent-builder-api and bpmn-model-api and create-a-bpmn-model.

Refer the below example on how to use fluent api to create a model and deploy it.

// validate and write model to file
File file = File.createTempFile("bpmn-model-api-", ".bpmn");
Bpmn.writeModelToFile(file, modelInstance);

To deploy the bpmn file:

private RepositoryService repositoryService;

public DeploymentWithDefinitions deployProcess(){
   DeploymentWithDefinitions deploymentWithDefinitions = repositoryService.createDeployment().name("myProcess")
			.addInputStream("bpmnFile", new ByteArrayInputStream(bpmnModelByteArray)).deployWithResult();

	Assert.notEmpty(deploymentWithDefinitions.getDeployedProcessDefinitions(), "Process should not be empty");
	return deploymentWithDefinitions;