我正在使用 Azure DevOps 進行一些應用程式部署。我需要有一個保存的變數內部版本號,每次我成功構建并將 apk/ipa 發送到商店時都會更新。
現在,根據我在 Azure 檔案和 StackOverflow 上的其他帖子中讀到的內容,我以這種方式設定了我的腳本。
這是我的管道變數

這是我當前的腳本

輸出是:

所以,它似乎更新了我的區域變數,而不是我的管道變數。我不確定為什么,因為這是隨處提供的示例。
資料來源:
我的快車道(紅寶石腳本)車道看起來像這樣
lane :tf do `echo $major` `echo $minor` `echo $patch` `echo $build` # Nothing `echo $MAJOR` `echo $MINOR` `echo $PATCH` `echo $BUILD` # Nothing `echo $(major)` # fails end那些什么都沒有顯示。
這種蔚藍的 DevOps 非常令人沮喪。它在這里說我可以對這個變數進行 bash 呼叫。

uj5u.com熱心網友回復:
在執行任務之前,使用宏語法的變數會在運行時擴展,這就是為什么最后一個日志中有值“1”,即使之前在上一步中將值設定為“2”也是如此。
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml,batch#runtime-expression-syntax
嘗試使用運行時運算式,它在運行時擴展
uj5u.com熱心網友回復:
為您提供兩種解決方案。
1.使用計數器運算式。
counter(<prefix>, <feed>)counter 運算式用于管道,它有兩個引數,前綴和種子。種子基于前綴。
首次設定前綴并運行時,計數器結果將從饋送值開始。但是對于后面基于相同前綴的運行,計數器結果將忽略 feed 值,它將是 'last time counter result 1'
2、直接更改管道定義。
例如,我可以使用下面的 python 代碼來獲取和更改經典管道的變數:
import json import requests org_name = "xxx" project_name = "xxx" pipeline_definition_id = "xxx" personal_access_token = "xxx" key = 'variables' var_name = 'BUILDNUMBER' url = "https://dev.azure.com/" org_name "/" project_name "/_apis/build/definitions/" pipeline_definition_id "?api-version=6.0" payload={} headers = { 'Authorization': 'Basic ' personal_access_token } response = requests.request("GET", url, headers=headers, data=payload) print(response.text) json_content = response.text def get_content_of_json(json_content, key, var_name): data = json.loads(json_content) return data[key][var_name].get('value') def change_content_of_json(json_content, key, var_name): data = json.loads(json_content) data[key][var_name]['value'] = str(int(get_content_of_json(json_content,key,var_name)) 1) return data json_data = change_content_of_json(json_content, key, var_name) url2 = "https://dev.azure.com/" org_name "/" project_name "/_apis/build/definitions/" pipeline_definition_id "?api-version=6.0" payload2 = json.dumps(json_data) headers2 = { 'Authorization': 'Basic ' personal_access_token, 'Content-Type': 'application/json' } response2 = requests.request("PUT", url2, headers=headers2, data=payload2)為您撰寫一個 JSON 演示,您可以將其匯入您的 DevOps 并基于此設計自己的經典管道:
{ "options": [ { "enabled": false, "definition": { "id": "5d58cc01-7c75-450c-be18-a388ddb129ec" }, "inputs": { "branchFilters": "[\" refs/heads/*\"]", "additionalFields": "{}" } }, { "enabled": false, "definition": { "id": "a9db38f9-9fdc-478c-b0f9-464221e58316" }, "inputs": { "workItemType": "Bug", "assignToRequestor": "true", "additionalFields": "{}" } } ], "variables": { "BUILDNUMBER": { "value": "7" }, "system.debug": { "value": "false", "allowOverride": true } }, "properties": {}, "tags": [], "_links": { "self": { "href": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/build/Definitions/359?revision=11" }, "web": { "href": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_build/definition?definitionId=359" }, "editor": { "href": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_build/designer?id=359&_a=edit-build-definition" }, "badge": { "href": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/build/status/359" } }, "jobAuthorizationScope": 2, "jobTimeoutInMinutes": 60, "jobCancelTimeoutInMinutes": 5, "process": { "phases": [ { "steps": [ { "environment": {}, "enabled": true, "continueOnError": false, "alwaysRun": false, "displayName": "PowerShell Script", "timeoutInMinutes": 0, "retryCountOnTaskFailure": 0, "condition": "succeeded()", "task": { "id": "e213ff0f-5d5c-4791-802d-52ea3e7be1f1", "versionSpec": "2.*", "definitionType": "task" }, "inputs": { "targetType": "inline", "filePath": "", "arguments": "", "script": "# Write your PowerShell commands here.\n\nWrite-Host \"Hello World\"\n\npip install requests\n", "errorActionPreference": "stop", "warningPreference": "default", "informationPreference": "default", "verbosePreference": "default", "debugPreference": "default", "progressPreference": "silentlyContinue", "failOnStderr": "false", "showWarnings": "false", "ignoreLASTEXITCODE": "false", "pwsh": "false", "workingDirectory": "", "runScriptInSeparateScope": "false" } }, { "environment": {}, "enabled": true, "continueOnError": false, "alwaysRun": false, "displayName": "Run a Python script", "timeoutInMinutes": 0, "retryCountOnTaskFailure": 0, "condition": "succeeded()", "task": { "id": "6392f95f-7e76-4a18-b3c7-7f078d2f7700", "versionSpec": "0.*", "definitionType": "task" }, "inputs": { "scriptSource": "inline", "scriptPath": "", "script": "import json\nimport requests\n\n\norg_name = \"BowmanCP\"\nproject_name = \"BowmanCP\"\npipeline_definition_id = \"359\"\npersonal_access_token = \"OnhlbXFzd29hdXJrdGhvNGJhemJza3hhenZldnRhbXhhZTVhNDMycXZoNzRicmo3YTZjc3E=\"\n\nkey = 'variables'\nvar_name = 'BUILDNUMBER'\n\nurl = \"https://dev.azure.com/\" org_name \"/\" project_name \"/_apis/build/definitions/\" pipeline_definition_id \"?api-version=6.0\"\n\npayload={}\nheaders = {\n 'Authorization': 'Basic ' personal_access_token\n}\n\nresponse = requests.request(\"GET\", url, headers=headers, data=payload)\nprint(response.text)\njson_content = response.text\ndef get_content_of_json(json_content, key, var_name):\n data = json.loads(json_content)\n return data[key][var_name].get('value')\n\ndef change_content_of_json(json_content, key, var_name):\n data = json.loads(json_content)\n data[key][var_name]['value'] = str(int(get_content_of_json(json_content,key,var_name)) 1)\n return data\n\njson_data = change_content_of_json(json_content, key, var_name)\n\n\nurl2 = \"https://dev.azure.com/\" org_name \"/\" project_name \"/_apis/build/definitions/\" pipeline_definition_id \"?api-version=6.0\"\n\npayload2 = json.dumps(json_data)\nheaders2 = {\n 'Authorization': 'Basic ' personal_access_token,\n 'Content-Type': 'application/json'\n}\n\nresponse2 = requests.request(\"PUT\", url2, headers=headers2, data=payload2)\n", "arguments": "", "pythonInterpreter": "", "workingDirectory": "", "failOnStderr": "false" } } ], "name": "Agent job 1", "refName": "Job_1", "condition": "succeeded()", "target": { "executionOptions": { "type": 0 }, "allowScriptsAuthAccessOption": false, "type": 1 }, "jobAuthorizationScope": 2 } ], "target": { "agentSpecification": { "identifier": "windows-2019" } }, "type": 1 }, "repository": { "properties": { "cleanOptions": "0", "labelSources": "0", "labelSourcesFormat": "$(build.buildNumber)", "reportBuildStatus": "true", "fetchDepth": "1", "gitLfsSupport": "false", "skipSyncSource": "false", "checkoutNestedSubmodules": "false" }, "id": "421488b2-be68-4b8e-8faf-8302da314071", "type": "TfsGit", "name": "XunitTest_Auto", "url": "https://dev.azure.com/BowmanCP/BowmanCP/_git/XunitTest_Auto", "defaultBranch": "refs/heads/main", "clean": "false", "checkoutSubmodules": false }, "processParameters": {}, "quality": 1, "authoredBy": { "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": "[email protected]", "imageUrl": "https://dev.azure.com/BowmanCP/_apis/GraphProfile/MemberAvatars/aad.ZGU3N2NiY2YtZTgzYy03ZDkwLWI0YTYtOTk3Nzg3NDczMzBl", "descriptor": "aad.ZGU3N2NiY2YtZTgzYy03ZDkwLWI0YTYtOTk3Nzg3NDczMzBl" }, "drafts": [], "queue": { "_links": { "self": { "href": "https://dev.azure.com/BowmanCP/_apis/build/Queues/18" } }, "id": 18, "name": "Azure Pipelines", "url": "https://dev.azure.com/BowmanCP/_apis/build/Queues/18", "pool": { "id": 9, "name": "Azure Pipelines", "isHosted": true } }, "id": 359, "name": "ChangeVariable", "url": "https://dev.azure.com/BowmanCP/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/build/Definitions/359?revision=11", "uri": "vstfs:///Build/Definition/359", "path": "\\", "type": 2, "queueStatus": 0, "revision": 11, "createdDate": "2022-11-07T10:14:18.003Z", "project": { "id": "c6358b04-e91a-4bd1-a894-1adb543134d6", "name": "BowmanCP", "url": "https://dev.azure.com/BowmanCP/_apis/projects/c6358b04-e91a-4bd1-a894-1adb543134d6", "state": 1, "revision": 178, "visibility": 0, "lastUpdateTime": "2022-09-05T06:02:02.693Z" } }uj5u.com熱心網友回復:
它似乎更新了我的區域變數,但沒有更新我的管道變數
請參閱此檔案: 在腳本中設定變數
管道中的腳本可以定義一個變數,以便管道中的后續步驟之一可以使用它。在腳本中設定變數
您用于更新管道變數值的方法是正確的。
它不適用于當前任務,但更新后的值可以用于下一個任務。
例如:
steps: - bash: | echo $(TestVariable) echo "##vso[task.setvariable variable=TestVariable;]2" displayName: 'Bash Script' - bash: | echo $(TestVariable)關于使用計數器運算式的要求,可以參考我的另一張工單:Azure DevOps: release version
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/528888.html
