drone / go-convert

Package convert provides tools for converting pipeline configuration files to the Drone format.
Apache License 2.0
10 stars 8 forks source link

[github] support `jobs.<job_id>.outputs` #43

Open jimsheldon opened 1 year ago

jimsheldon commented 1 year ago

A map of outputs for a job. Job outputs are available to all downstream jobs that depend on this job.

See

jimsheldon commented 1 year ago

Example v0 pipeline with outputs referenced in a step and following stage:

  stages:
    - stage:
        name: first
        identifier: first
        description: ""
        type: CI
        spec:
          cloneCodebase: false
          platform:
            os: Linux
            arch: Amd64
          runtime:
            type: Cloud
            spec: {}
          execution:
            steps:
              - step:
                  type: Run
                  name: one
                  identifier: one
                  spec:
                    shell: Sh
                    command: export SOMEVAR=somevalue
                    outputVariables:
                      - name: SOMEVAR
              - step:
                  type: Run
                  name: two
                  identifier: two
                  spec:
                    shell: Sh
                    command: |-
                      # both of these return the output
                      echo <+steps.one.output.outputVariables.SOMEVAR>
                      echo <+execution.steps.one.output.outputVariables.SOMEVAR>
    - stage:
        name: second
        identifier: second
        description: ""
        type: CI
        spec:
          cloneCodebase: false
          platform:
            os: MacOS
            arch: Arm64
          runtime:
            type: Cloud
            spec: {}
          execution:
            steps:
              - step:
                  type: Run
                  name: one
                  identifier: one
                  spec:
                    shell: Sh
                    command: echo <+pipeline.stages.first.spec.execution.steps.one.output.outputVariables.SOMEVAR>
jimsheldon commented 1 year ago

Here is an equivalent use of outputs from this documentation https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idoutputs

  stages:
    - stage:
        name: job1
        identifier: job1
        description: ""
        type: CI
        spec:
          cloneCodebase: false
          platform:
            os: Linux
            arch: Amd64
          runtime:
            type: Cloud
            spec: {}
          execution:
            steps:
              - step:
                  type: Run
                  name: step1
                  identifier: step1
                  spec:
                    shell: Sh
                    command: export OUTPUT1=hello
                    outputVariables:
                      - name: OUTPUT1
              - step:
                  type: Run
                  name: step2
                  identifier: step2
                  spec:
                    shell: Sh
                    command: export OUTPUT2=world
                    outputVariables:
                      - name: OUTPUT2
    - stage:
        name: job2
        identifier: job2
        description: ""
        type: CI
        spec:
          cloneCodebase: false
          platform:
            os: Linux
            arch: Amd64
          runtime:
            type: Cloud
            spec: {}
          execution:
            steps:
              - step:
                  type: Run
                  name: echo
                  identifier: echo
                  spec:
                    shell: Sh
                    command: echo $OUTPUT1 $OUTPUT2
                    envVariables:
                      OUTPUT1: <+pipeline.stages.job1.spec.execution.steps.step1.output.outputVariables.OUTPUT1>
                      OUTPUT2: <+pipeline.stages.job1.spec.execution.steps.step2.output.outputVariables.OUTPUT2>
jimsheldon commented 1 year ago

I started to work on this in https://github.com/drone/go-convert/compare/issue43

I have a test that currently fails.

The tricky part is that we need to take this:

    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}

And use those values to add the necessary outputs list to each step:

    steps:
    - id: step1
      spec:
        run: echo "test=hello" >> "$GITHUB_OUTPUT"
        outputs:
        - test
      type: script
    - id: step2
      spec:
        run: echo "test=world" >> "$GITHUB_OUTPUT"
        outputs:
        - test

NOTE: The above still won't work as-is, the variables have to be exported for Harness CI's outputs to pick them up. The run commands would have to change to export test=hello and export test=world.

I think we can wait on converting the references to outputs like these

      - env:
          OUTPUT1: ${{needs.job1.outputs.output1}}
          OUTPUT2: ${{needs.job1.outputs.output2}}

These would have to convert to this

      - env:
          OUTPUT1: <+pipeline.stages.job1.spec.execution.steps.step1.output.outputVariables.test>
          OUTPUT2: <+pipeline.stages.job1.spec.execution.steps.step2.output.outputVariables.test>