如何从Argo工作流创建分支?

huangapple go评论80阅读模式
英文:

How can I create a branch from an Argo workflow?

问题

我正在尝试在Argo工作流中发送HTTP POST请求以在GitHub上创建一个分支。它一直失败,我无法弄清楚原因。看起来像是一些微小的问题,我会感激任何帮助来解决这个问题。

在执行过程中,我只看到一条消息 successCondition 'response.statusCode == 201' evaluated false。日志中没有看到任何信息。

以下是我的工作流程:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Workflow
  3. metadata:
  4. generateName: http-template-
  5. spec:
  6. entrypoint: main
  7. serviceAccountName: argo-workflow-sa
  8. templates:
  9. - name: main
  10. steps:
  11. - - name: getmainsha
  12. template: get-sha
  13. arguments:
  14. parameters:
  15. - name: url
  16. value: "https://api.github.com/repos/<owner>/<repo>/git/refs/heads/main"
  17. - - name: print-main-sha
  18. template: print
  19. arguments:
  20. parameters:
  21. - name: message
  22. value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
  23. - - name: generate-branch-name
  24. template: generate-unique-name
  25. arguments:
  26. parameters:
  27. - name: string-name
  28. value: "new-branch"
  29. - - name: create-branch-github
  30. template: create-branch-post
  31. arguments:
  32. parameters:
  33. - name: url
  34. value: "https://api.github.com/repos/<owner>/<repo>/git/refs"
  35. - name: branch-name
  36. value: "{{steps.generate-branch-name.outputs.result}}"
  37. - name: main-branch-sha
  38. value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
  39. #--------------------------------------------------------------templates-----------------------------------------------------------------------------#
  40. - name: print
  41. inputs:
  42. parameters:
  43. - name: message
  44. container:
  45. image: docker/whalesay:latest
  46. command: [cowsay]
  47. args: ["{{inputs.parameters.message}}"]
  48. - name: get-sha
  49. inputs:
  50. parameters:
  51. - name: url
  52. http:
  53. timeoutSeconds: 20 # 默认值 30
  54. url: "{{inputs.parameters.url}}"
  55. method: "GET"
  56. headers:
  57. - name: "Accept"
  58. value: "application/vnd.github+json"
  59. - name: "Authorization"
  60. value: "Bearer <Token>"
  61. - name: "X-GitHub-Api-Version"
  62. value: "2022-11-28"
  63. successCondition: "response.statusCode == 200"
  64. - name: generate-unique-name
  65. inputs:
  66. parameters:
  67. - name: string-name
  68. script:
  69. image: python:alpine3.6
  70. command: [python]
  71. source: |
  72. import random
  73. i = random.randint(1, 1000)
  74. print('refs/heads/'+'{{inputs.parameters.string-name}}'+'-'+str(i))
  75. - name: create-branch-post
  76. inputs:
  77. parameters:
  78. - name: url
  79. - name: branch-name
  80. - name: main-branch-sha
  81. http:
  82. timeoutSeconds: 20 # 默认值 30
  83. url: "{{inputs.parameters.url}}"
  84. method: "POST"
  85. headers:
  86. - name: "Accept"
  87. value: "application/vnd.github+json"
  88. - name: "Authorization"
  89. value: "Bearer <Token>"
  90. - name: "X-GitHub-Api-Version"
  91. value: "2022-11-28"
  92. body: "{'ref':{{inputs.parameters.branch-name}},'sha':{{inputs.parameters.main-branch-sha}}}"
  93. successCondition: "response.statusCode == 201"

我期望在GitHub上创建一个分支。

英文:

I am trying to send a HTTP post request in Argo workflows to create a branch in GitHub. It keeps failing and I am not able to figure out why. It looks like something sticky and small, and I would appreciate any help in figuring this out.

While executing, I just see a message successCondition 'response.statusCode == 201' evaluated false. I don't see anything in logs.

Here is my workflow:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Workflow
  3. metadata:
  4. generateName: http-template-
  5. spec:
  6. entrypoint: main
  7. serviceAccountName: argo-workflow-sa
  8. templates:
  9. - name: main
  10. steps:
  11. - - name: getmainsha
  12. template: get-sha
  13. arguments:
  14. parameters:
  15. - name: url
  16. value: "https://api.github.com/repos/<owner>/<repo>/git/refs/heads/main"
  17. - - name: print-main-sha
  18. template: print
  19. arguments:
  20. parameters:
  21. - name: message
  22. value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
  23. - - name: generate-branch-name
  24. template: generate-unique-name
  25. arguments:
  26. parameters:
  27. - name: string-name
  28. value: "new-branch"
  29. - - name: create-branch-github
  30. template: create-branch-post
  31. arguments:
  32. parameters:
  33. - name: url
  34. value: "https://api.github.com/repos/<owner>/<repo>/git/refs"
  35. - name: branch-name
  36. value: "{{steps.generate-branch-name.outputs.result}}"
  37. - name: main-branch-sha
  38. value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
  39. #--------------------------------------------------------------templates-----------------------------------------------------------------------------#
  40. - name: print
  41. inputs:
  42. parameters:
  43. - name: message
  44. container:
  45. image: docker/whalesay:latest
  46. command: [cowsay]
  47. args: ["{{inputs.parameters.message}}"]
  48. - name: get-sha
  49. inputs:
  50. parameters:
  51. - name: url
  52. http:
  53. timeoutSeconds: 20 # Default 30
  54. url: "{{inputs.parameters.url}}"
  55. method: "GET"
  56. headers:
  57. - name: "Accept"
  58. value: "application/vnd.github+json"
  59. - name: "Authorization"
  60. value: "Bearer <Token>"
  61. - name: "X-GitHub-Api-Version"
  62. value: "2022-11-28"
  63. successCondition: "response.statusCode == 200"
  64. - name: generate-unique-name
  65. inputs:
  66. parameters:
  67. - name: string-name
  68. script:
  69. image: python:alpine3.6
  70. command: [python]
  71. source: |
  72. import random
  73. i = random.randint(1, 1000)
  74. print('refs/heads/'+'{{inputs.parameters.string-name}}'+'-'+str(i))
  75. - name: create-branch-post
  76. inputs:
  77. parameters:
  78. - name: url
  79. - name: branch-name
  80. - name: main-branch-sha
  81. http:
  82. timeoutSeconds: 20 # Default 30
  83. url: "{{inputs.parameters.url}}"
  84. method: "POST"
  85. headers:
  86. - name: "Accept"
  87. value: "application/vnd.github+json"
  88. - name: "Authorization"
  89. value: "Bearer <Token>"
  90. - name: "X-GitHub-Api-Version"
  91. value: "2022-11-28"
  92. body: "{'ref':{{inputs.parameters.branch-name}},'sha':{{inputs.parameters.main-branch-sha}}}"
  93. successCondition: "response.statusCode == 201"

I expect a branch to be created in GitHub.

答案1

得分: 0

以下是已翻译的内容:

"So silly of me to make this mistake."
很愚蠢的我犯了这个错误。

As per the official github documentation the ref in the request body should be like this "ref":"refs/heads/<branch-name>".
根据官方的GitHub文档,请求体中的ref应该像这样:"ref":"refs/heads/<branch-name>"

Github api create reference
GitHub API 创建引用

Here is the updated template
这是更新后的模板

  1. kind: Workflow
  2. metadata:
  3. generateName: expand-pvc-template-
  4. spec:
  5. entrypoint: main
  6. serviceAccountName: argo-workflow-sa
  7. arguments:
  8. parameters:
  9. - name: uri
  10. value: "<github-uri>"
  11. templates:
  12. - name: main
  13. steps:
  14. - - name: create-branch-github
  15. template: github-http-post-put
  16. arguments:
  17. parameters:
  18. - name: url
  19. value: "{{workflow.parameters.uri}}/git/refs"
  20. - name: method
  21. value: "POST"
  22. - name: requestbody
  23. value: |
  24. {
  25. "ref":"refs/heads/new-branch",
  26. "sha":"<main-branch-sha>"
  27. }
  28. - name: success-condition
  29. value: "response.statusCode == 201"
  30. --------------------------------------------------------
  31. - name: github-http-post
  32. inputs:
  33. parameters:
  34. - name: url
  35. - name: method
  36. - name: requestbody
  37. - name: success-condition
  38. http:
  39. timeoutSeconds: 30
  40. url: "{{inputs.parameters.url}}"
  41. method: "{{inputs.parameters.method}}"
  42. headers:
  43. - name: "Accept"
  44. value: "application/vnd.github+json"
  45. - name: "Authorization"
  46. value: "Bearer <>"
  47. - name: "X-GitHub-Api-Version"
  48. value: "2022-11-28"
  49. body: "{{inputs.parameters.requestbody}}"
  50. successCondition: "{{inputs.parameters.success-condition}}"
英文:

So silly of me to make this mistake.

As per the official github documentation the ref in the request body should be like this "ref":"refs/heads/<branch-name>".

Github api create reference

Here is the updated template

  1. kind: Workflow
  2. metadata:
  3. generateName: expand-pvc-template-
  4. spec:
  5. entrypoint: main
  6. serviceAccountName: argo-workflow-sa
  7. arguments:
  8. parameters:
  9. - name: uri
  10. value: "<github-uri>"
  11. templates:
  12. - name: main
  13. steps:
  14. - - name: create-branch-github
  15. template: github-http-post-put
  16. arguments:
  17. parameters:
  18. - name: url
  19. value: "{{workflow.parameters.uri}}/git/refs"
  20. - name: method
  21. value: "POST"
  22. - name: requestbody
  23. value: |
  24. {
  25. "ref":"refs/heads/new-branch",
  26. "sha":"<main-branch-sha>"
  27. }
  28. - name: success-condition
  29. value: "response.statusCode == 201"
  30. --------------------------------------------------------
  31. - name: github-http-post
  32. inputs:
  33. parameters:
  34. - name: url
  35. - name: method
  36. - name: requestbody
  37. - name: success-condition
  38. http:
  39. timeoutSeconds: 30
  40. url: "{{inputs.parameters.url}}"
  41. method: "{{inputs.parameters.method}}"
  42. headers:
  43. - name: "Accept"
  44. value: "application/vnd.github+json"
  45. - name: "Authorization"
  46. value: "Bearer <>"
  47. - name: "X-GitHub-Api-Version"
  48. value: "2022-11-28"
  49. body: "{{inputs.parameters.requestbody}}"
  50. successCondition: "{{inputs.parameters.success-condition}}"

huangapple
  • 本文由 发表于 2023年6月15日 12:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定