github-apiargo-workflows

How can I create a branch from an Argo workflow?


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.


Solution

  • 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}}"