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

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

How can I create a branch from an Argo workflow?

问题

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

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

以下是我的工作流程:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: http-template-
spec:
  entrypoint: main
  serviceAccountName: argo-workflow-sa
  templates:
    - name: main
      steps:
        
        - - name: getmainsha
            template: get-sha
            arguments:
              parameters:
                - name: url
                  value: "https://api.github.com/repos/<owner>/<repo>/git/refs/heads/main"

        - - name: print-main-sha
            template: print
            arguments:
              parameters:
                - name: message
                  value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"

        - - name: generate-branch-name
            template: generate-unique-name
            arguments:
              parameters:
                - name: string-name
                  value: "new-branch"

        - - name: create-branch-github
            template: create-branch-post
            arguments:
              parameters:
                - name: url
                  value: "https://api.github.com/repos/<owner>/<repo>/git/refs"
                - name: branch-name
                  value: "{{steps.generate-branch-name.outputs.result}}"
                - name: main-branch-sha
                  value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
        
  #--------------------------------------------------------------templates-----------------------------------------------------------------------------#  

    - name: print
      inputs:
        parameters:
          - name: message
      container:
        image: docker/whalesay:latest
        command: [cowsay]
        args: ["{{inputs.parameters.message}}"]

    - name: get-sha
      inputs:
        parameters:
          - name: url
      http:
        timeoutSeconds: 20 # 默认值 30
        url: "{{inputs.parameters.url}}"
        method: "GET"
        headers:
          - name: "Accept"
            value: "application/vnd.github+json"
          - name: "Authorization"
            value: "Bearer <Token>"
          - name: "X-GitHub-Api-Version"
            value: "2022-11-28"
        successCondition: "response.statusCode == 200"

    - name: generate-unique-name
      inputs:
        parameters:
          - name: string-name
      script:
        image: python:alpine3.6
        command: [python]
        source: |
          import random
          i = random.randint(1, 1000)
          print('refs/heads/'+'{{inputs.parameters.string-name}}'+'-'+str(i))          

    - name: create-branch-post
      inputs:
        parameters:
          - name: url
          - name: branch-name
          - name: main-branch-sha
      http:
        timeoutSeconds: 20 # 默认值 30
        url: "{{inputs.parameters.url}}"
        method: "POST"
        headers:
          - name: "Accept"
            value: "application/vnd.github+json"
          - name: "Authorization"
            value: "Bearer <Token>"
          - name: "X-GitHub-Api-Version"
            value: "2022-11-28"
        body: "{'ref':{{inputs.parameters.branch-name}},'sha':{{inputs.parameters.main-branch-sha}}}"
        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:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: http-template-
spec:
  entrypoint: main
  serviceAccountName: argo-workflow-sa
  templates:
    - name: main
      steps:
        
        - - name: getmainsha
            template: get-sha
            arguments:
              parameters:
                - name: url
                  value: "https://api.github.com/repos/<owner>/<repo>/git/refs/heads/main"

        - - name: print-main-sha
            template: print
            arguments:
              parameters:
                - name: message
                  value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"

        - - name: generate-branch-name
            template: generate-unique-name
            arguments:
              parameters:
                - name: string-name
                  value: "new-branch"

        - - name: create-branch-github
            template: create-branch-post
            arguments:
              parameters:
                - name: url
                  value: "https://api.github.com/repos/<owner>/<repo>/git/refs"
                - name: branch-name
                  value: "{{steps.generate-branch-name.outputs.result}}"
                - name: main-branch-sha
                  value: "{{=jsonpath(steps.getmainsha.outputs.result, '$.object.sha')}}"
        
    
  #--------------------------------------------------------------templates-----------------------------------------------------------------------------#  

    - name: print
      inputs:
        parameters:
          - name: message
      container:
        image: docker/whalesay:latest
        command: [cowsay]
        args: ["{{inputs.parameters.message}}"]

    - name: get-sha
      inputs:
        parameters:
          - name: url
      http:
        timeoutSeconds: 20 # Default 30
        url: "{{inputs.parameters.url}}"
        method: "GET"
        headers:
          - name: "Accept"
            value: "application/vnd.github+json"
          - name: "Authorization"
            value: "Bearer <Token>"
          - name: "X-GitHub-Api-Version"
            value: "2022-11-28"
        successCondition: "response.statusCode == 200"

    - name: generate-unique-name
      inputs:
        parameters:
          - name: string-name
      script:
        image: python:alpine3.6
        command: [python]
        source: |
          import random
          i = random.randint(1, 1000)
          print('refs/heads/'+'{{inputs.parameters.string-name}}'+'-'+str(i))          

    - name: create-branch-post
      inputs:
        parameters:
          - name: url
          - name: branch-name
          - name: main-branch-sha
      http:
        timeoutSeconds: 20 # Default 30
        url: "{{inputs.parameters.url}}"
        method: "POST"
        headers:
          - name: "Accept"
            value: "application/vnd.github+json"
          - name: "Authorization"
            value: "Bearer <Token>"
          - name: "X-GitHub-Api-Version"
            value: "2022-11-28"
        body: "{'ref':{{inputs.parameters.branch-name}},'sha':{{inputs.parameters.main-branch-sha}}}"
        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
这是更新后的模板

kind: Workflow
metadata:
generateName: expand-pvc-template-
spec:
entrypoint: main
serviceAccountName: argo-workflow-sa
arguments:
parameters:
- name: uri
value: "<github-uri>"
templates:
- name: main
steps:
- - name: create-branch-github
template: github-http-post-put
arguments:
parameters:
- name: url
value: "{{workflow.parameters.uri}}/git/refs"
- name: method
value: "POST"
- name: requestbody
value: |
{
"ref":"refs/heads/new-branch",
"sha":"<main-branch-sha>"
}
- name: success-condition
value: "response.statusCode == 201"
--------------------------------------------------------
- name: github-http-post
inputs:
parameters:
- name: url
- name: method
- name: requestbody
- name: success-condition
http:
timeoutSeconds: 30
url: "{{inputs.parameters.url}}"
method: "{{inputs.parameters.method}}"
headers:
- name: "Accept"
value: "application/vnd.github+json"
- name: "Authorization"
value: "Bearer <>"
- name: "X-GitHub-Api-Version"
value: "2022-11-28"
body: "{{inputs.parameters.requestbody}}"
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

kind: Workflow
metadata:
generateName: expand-pvc-template-
spec:
entrypoint: main
serviceAccountName: argo-workflow-sa
arguments:
parameters:
- name: uri
value: "<github-uri>"
templates:
- name: main
steps:
- - name: create-branch-github
template: github-http-post-put
arguments:
parameters:
- name: url
value: "{{workflow.parameters.uri}}/git/refs"
- name: method
value: "POST"
- name: requestbody
value: |
{
"ref":"refs/heads/new-branch",
"sha":"<main-branch-sha>"
}
- name: success-condition
value: "response.statusCode == 201"
--------------------------------------------------------
- name: github-http-post
inputs:
parameters:
- name: url
- name: method
- name: requestbody
- name: success-condition
http:
timeoutSeconds: 30
url: "{{inputs.parameters.url}}"
method: "{{inputs.parameters.method}}"
headers:
- name: "Accept"
value: "application/vnd.github+json"
- name: "Authorization"
value: "Bearer <>"
- name: "X-GitHub-Api-Version"
value: "2022-11-28"
body: "{{inputs.parameters.requestbody}}"
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:

确定