Skip to content

Having some issues with deprecated "set-output" using Github Files and this action #353

Description

@william-mcdaniels

Is your feature request related to a problem? Please describe.
We use this action to output vars to other parts of our workflow, but we are having difficulty getting this action to output when switching from the current usage to the new format.

Here is the current excerpt from a workflow where we use this action. The issue is when we try to change the last line to the new format, removing "set-output". We suspect that since it's embedded inside of Node.js that it doesn't like being flipped around.

steps:
  - name: "Filter workspace"
    id: filter_workspace
    uses: actions/github-script@v3
    with:
      github-token: ${{ secrets.GITHUB_TOKEN }}
      script: |
        var command = '${{ github.event.inputs.TERRAFORM_COMMAND }}'
        var execute = false 
        if (command != 'no-op') {
          var workspace = '${{ matrix.workspace }}'
          execute = true
          var filter = '${{ github.event.inputs.WORKSPACE_FILTER }}'
          if (filter.trim().length > 0) {
            var filter_array = '${{ github.event.inputs.WORKSPACE_FILTER }}'.split(',').map(function(item) {
              return item.trim()
            })
            if (filter_array.length > 0) {
              execute = false
              if (filter_array.includes(workspace)) {
                execute = true
              }
            }
          }
        }
        console.log("::set-output name=execute::" + execute)

Here is the github documentation suggesting refactor of the set-output command.

https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

Is there a recommended way to change this set-output command that will still work with your action? Does your action support Python? Are you aware of any that do? I know that I have not provided much more of a code reference, but it is not a public repository. What I can say is that the

Activity

  1. joshmgross commented on Mar 18, 2023

    @joshmgross
    Contributor

    Is there a recommended way to change this set-output command that will still work with your action?

    👋 This action includes @actions/core, which you can use to set outputs:

    setOutput

    For your example, it would look like:

    - console.log("::set-output name=execute::" + execute)
    + core.setOutput("execute", execute)

    Internally, setOutput handles writing to the GITHUB_OUTPUT file for you - https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/core/src/core.ts#L192-L200.

    actions/github-script@v3 will include the correct version of @actions/core, since it was backported in #348

    Does your action support Python? Are you aware of any that do?

    You could write a python script and execute it directly in a run step. I'm not super familiar with Python, but I think this is roughly equivalent:

    import os
    
    command = os.environ['TERRAFORM_COMMAND']
    execute = False
    if (command != 'no-op'):
      workspace = os.environ['WORKSPACE']
      execute = True
      workspace_filter = os.environ['WORKSPACE_FILTER']
      if (len(workspace_filter.strip()) > 0):
        filter_array = list(map(str.strip, workspace_filter.split(',')))
        if (len(filter_array) > 0):
          execute = workspace in filter_array
    
    output_file = open(os.environ['GITHUB_OUTPUT'], 'a')
    output_file.write(f'execute={str(execute).lower()}\n')
    output_file.close()

    If this is on a self-hosted runner, you'd also want to ensure Python is installed - https://github.com/actions/setup-python

    For inputs and outputs, we can set them as env variables on the run step:

    steps:
      - name: "Filter workspace"
        id: filter_workspace
        run: python script.py
        env:
          TERRAFORM_COMMAND: ${{ github.event.inputs.TERRAFORM_COMMAND }}
          WORKSPACE: ${{ matrix.workspace }}
          WORKSPACE_FILTER: ${{ github.event.inputs.WORKSPACE_FILTER }}

    GITHUB_OUTPUT should already be available in the step's environment.

  2. william-mcdaniels commented on Mar 26, 2023

    @william-mcdaniels
    Author

    Thanks! I will give this a try and let you know how it works for me (the setOutput change)

  3. william-mcdaniels commented on Mar 26, 2023

    @william-mcdaniels
    Author

    core.setOutput worked beautifully. Thanks again :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionA question on how to use this action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions