user19975572
user19975572

Reputation:

azure pipelines create a build and release pipeline for a python script

At the moment got build and deployment of python script in the same pipeline. I want to separate this out and have the deployment in a release pipeline. how do I do this? do I use artifacts in build pipeline which is consumed in the release pipeline?

trigger: 
  none
pool:
  vmImage: "windows-latest"

steps:

  # Set the version of Python which the build agent should use
- task: UsePythonVersion@0
  displayName: Use Python 3.8
  inputs:
    versionSpec: '3.8'
    addToPath: true 
  
  # Run Pip installer for any prerequisite libraries
- bash: pip3 install -r requirements.txt
  displayName: Install pip package requirements
  workingDirectory: project-requirements

  #run script using azure cli
- task: AzureCLI@2
  inputs:
    azureSubscription: 'sub'
    scriptType: 'batch'
    scriptLocation: 'inlineScript'
    inlineScript: 'python etl.py'
    workingDirectory: src/scripts

Upvotes: 0

Views: 1216

Answers (1)

Bowman Zhu
Bowman Zhu

Reputation: 7251

Write a sample for you:

Build Pipeline:

trigger:
- none

pool:
  vmImage: windows-latest
variables:
- name: system.debug
  value: true
steps:
- task: CmdLine@2
  inputs:
    script: |
      python -m venv tutorial-env
      call tutorial-env\Scripts\activate.bat
      pip install -r requirements.txt

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(System.DefaultWorkingDirectory)'
    artifact: 'drop'
    publishLocation: 'pipeline'

Release Pipeline:

{
    "source": 2,
    "revision": 8,
    "description": null,
    "variables": {},
    "variableGroups": [],
    "environments": [
        {
            "id": 14,
            "name": "Stage 2",
            "rank": 1,
            "owner": {
                "displayName": "Bowman Zhu",
                "url": "https://spsprodsea2.vssps.visualstudio.com/A64545e3d-c12d-4c81-b77f-4de83783d9bd/_apis/Identities/af91e22a-cc35-4c8e-8af3-f49c4a1b9b6a",
                "_links": {
                    "avatar": {
                        "href": "https://dev.azure.com/BowmanCP/_apis/GraphProfile/MemberAvatars/aad.ZGU3N2NiY2YtZTgzYy03ZDkwLWI0YTYtOTk3Nzg3NDczMzBl"
                    }
                },
                "id": "af91e22a-cc35-4c8e-8af3-f49c4a1b9b6a",
                "uniqueName": "xxx",
                "imageUrl": "https://dev.azure.com/BowmanCP/_apis/GraphProfile/MemberAvatars/aad.ZGU3N2NiY2YtZTgzYy03ZDkwLWI0YTYtOTk3Nzg3NDczMzBl",
                "descriptor": "aad.ZGU3N2NiY2YtZTgzYy03ZDkwLWI0YTYtOTk3Nzg3NDczMzBl"
            },
            "variables": {},
            "variableGroups": [],
            "preDeployApprovals": {
                "approvals": [
                    {
                        "rank": 1,
                        "isAutomated": true,
                        "isNotificationOn": false,
                        "id": 40
                    }
                ],
                "approvalOptions": {
                    "requiredApproverCount": null,
                    "releaseCreatorCanBeApprover": false,
                    "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped": false,
                    "enforceIdentityRevalidation": false,
                    "timeoutInMinutes": 0,
                    "executionOrder": 1
                }
            },
            "deployStep": {
                "id": 41
            },
            "postDeployApprovals": {
                "approvals": [
                    {
                        "rank": 1,
                        "isAutomated": true,
                        "isNotificationOn": false,
                        "id": 42
                    }
                ],
                "approvalOptions": {
                    "requiredApproverCount": null,
                    "releaseCreatorCanBeApprover": false,
                    "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped": false,
                    "enforceIdentityRevalidation": false,
                    "timeoutInMinutes": 0,
                    "executionOrder": 2
                }
            },
            "deployPhases": [
                {
                    "deploymentInput": {
                        "parallelExecution": {
                            "parallelExecutionType": 0
                        },
                        "agentSpecification": null,
                        "skipArtifactsDownload": false,
                        "artifactsDownloadInput": {
                            "downloadInputs": []
                        },
                        "queueId": 13,
                        "demands": [],
                        "enableAccessToken": false,
                        "timeoutInMinutes": 0,
                        "jobCancelTimeoutInMinutes": 1,
                        "condition": "succeeded()",
                        "overrideInputs": {}
                    },
                    "rank": 1,
                    "phaseType": 1,
                    "name": "Agent job",
                    "refName": null,
                    "workflowTasks": [
                        {
                            "environment": {},
                            "taskId": "61f2a582-95ae-4948-b34d-a1b3c4f6a737",
                            "version": "2.*",
                            "name": "Download Pipeline Artifact",
                            "refName": "",
                            "enabled": true,
                            "alwaysRun": false,
                            "continueOnError": false,
                            "timeoutInMinutes": 0,
                            "retryCountOnTaskFailure": 0,
                            "definitionType": "task",
                            "overrideInputs": {},
                            "condition": "succeeded()",
                            "inputs": {
                                "source": "specific",
                                "project": "c6358b04-e91a-4bd1-a894-1adb543134d6",
                                "pipeline": "347",
                                "preferTriggeringPipeline": "false",
                                "runVersion": "latest",
                                "runBranch": "refs/heads/master",
                                "runId": "",
                                "tags": "",
                                "allowPartiallySucceededBuilds": "false",
                                "allowFailedBuilds": "false",
                                "artifact": "",
                                "patterns": "**",
                                "path": "$(System.DefaultWorkingDirectory)"
                            }
                        },
                        {
                            "environment": {},
                            "taskId": "d9bafed4-0b18-4f58-968d-86655b4d2ce9",
                            "version": "2.*",
                            "name": "This step will show the packages that has been installed. You can also do other things here",
                            "refName": "",
                            "enabled": true,
                            "alwaysRun": false,
                            "continueOnError": false,
                            "timeoutInMinutes": 0,
                            "retryCountOnTaskFailure": 0,
                            "definitionType": "task",
                            "overrideInputs": {},
                            "condition": "succeeded()",
                            "inputs": {
                                "script": "echo You can do something here.\n\ndir\n\ncd drop/tutorial-env/Lib/site-packages\n\ndir\n\n\n",
                                "workingDirectory": "",
                                "failOnStderr": "false"
                            }
                        }
                    ]
                }
            ],
            "environmentOptions": {
                "emailNotificationType": "OnlyOnFailure",
                "emailRecipients": "release.environment.owner;release.creator",
                "skipArtifactsDownload": false,
                "timeoutInMinutes": 0,
                "enableAccessToken": false,
                "publishDeploymentStatus": true,
                "badgeEnabled": false,
                "autoLinkWorkItems": false,
                "pullRequestDeploymentEnabled": false
            },
            "demands": [],
            "conditions": [
                {
                    "name": "ReleaseStarted",
                    "conditionType": 1,
                    "value": ""
                }
            ],
            "executionPolicy": {
                "concurrencyCount": 1,
                "queueDepthCount": 0
            },
            "schedules": [],
            "currentRelease": {
                "id": 60,
                "url": "https://vsrm.dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/60",
                "_links": {}
            },
            "retentionPolicy": {
                "daysToKeep": 30,
                "releasesToKeep": 3,
                "retainBuild": true
            },
            "processParameters": {},
            "properties": {
                "BoardsEnvironmentType": {
                    "$type": "System.String",
                    "$value": "unmapped"
                },
                "LinkBoardsWorkItems": {
                    "$type": "System.String",
                    "$value": "False"
                }
            },
            "preDeploymentGates": {
                "id": 0,
                "gatesOptions": null,
                "gates": []
            },
            "postDeploymentGates": {
                "id": 0,
                "gatesOptions": null,
                "gates": []
            },
            "environmentTriggers": [],
            "badgeUrl": "https://vsrm.dev.azure.com/BowmanCP/_apis/public/Release/badge/c6358b04-e91a-4bd1-a894-1adb543134d6/14/14"
        }
    ],
    "artifacts": [],
    "triggers": [],
    "releaseNameFormat": "Release-$(rev:r)",
    "tags": [],
    "properties": {
        "DefinitionCreationSource": {
            "$type": "System.String",
            "$value": "ReleaseNew"
        },
        "IntegrateBoardsWorkItems": {
            "$type": "System.String",
            "$value": "False"
        },
        "IntegrateJiraWorkItems": {
            "$type": "System.String",
            "$value": "false"
        }
    },
    "id": 14,
    "name": "Python_Virtual_Env_Release",
    "path": "\\",
    "projectReference": null,
    "url": "https://vsrm.dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/definitions/14",
    "_links": {
        "self": {
            "href": "https://vsrm.dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/definitions/14"
        },
        "web": {
            "href": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_release?definitionId=14"
        }
    }
}

This release pipeline JSON definition is just a demo for you to refer, you need to create a release pipeline with your own configurations.

This is my reprository structure:

enter image description here

You can see that the dependencies are all been prepared on Release pipeline side:

enter image description here

Upvotes: 1

Related Questions