You can write a Python script to execute a geoprocessing workflow and publish it as a geoprocessing service. The primary way to execute a script is to use ArcPy. Once the customized tool has been executed successfully, you can use the arcpy CreateGPSDDraft function to create an SDDraft file. From there, you can use the Stage Service tool to stage the service, and use the Upload Service Definition tool to finish the publishing process. For more discussion and code samples, see CreateGPSDDraft.