nac123456
nac123456

Reputation: 113

Github Actions $GITHUB_OUTPUT does not seem to be setting job output

I have been using Github Actions to deploy changes for a data engineering project. I have been getting warnings that set-output command is deprecated and am attempting to use $GITHUB_OUTPUT but I am not able to set the output of the job using this.

if_merged:
      runs-on: ubuntu-latest
      if: github.event.pull_request.merged == true
      name: check diff changed
      steps:
        - name: Checkout
          uses: actions/checkout@v3
          with:
            # Checkout as many commits as needed for the diff
            fetch-depth: 2
        
        - shell: pwsh
          id: check_file_changed
          run: |
            # Diff HEAD with the previous commit
            # filters out deleted files
            $diff = git diff --name-only --diff-filter=d HEAD^ HEAD
            
            # Check what files were in the diff
            echo $diff
            
            # Check if a file Pipfile.lock or Dockerfile has changed (added, modified, deleted)
            $BuildDiff = $diff | Where-Object { $_ -match 'Pipfile.lock' -or $_ -match 'Dockerfile'}
            $HasBuildDiff = $BuildDiff.Length -gt 0

            # Check if k8s job has changed
            $K8sDiff = $diff | Where-Object { $_ -match 'kubernetes_job.py'}
            $HasK8sDiff = $K8sDiff.Length -gt 0

            # Check if sql file has changed
            $SqlDiff = $diff | Where-Object { $_ -match '.sql'}
            $HasSqlDiff = $SqlDiff.Length -gt 0

            # Check if flow file has changed
            $FlowDiff = $diff | Where-Object { $_ -match 'flow.py'}
            $HasFlowDiff = $FlowDiff.Length -gt 0

            # Check value of matched object
            echo BuildDiff $BuildDiff ---
            echo K8sDiff $K8sDiff ---
            # echo DeploymentDiff $DeploymentDiff ---
            echo FlowDiff $FlowDiff ---

            # Set the outputs
            Write-Host "::set-output name=build_changed::$HasBuildDiff"
            Write-Host "::set-output name=k8s_changed::$HasK8sDiff"
            Write-Host "::set-output name=sql_changed::$HasSqlDiff"
            Write-Host "flow_changed=$HasFlowDiff" >> $GITHUB_OUTPUT
            # Write-Host "::set-output name=flow_changed::$HasFlowDiff"
      outputs:
        build_changed: ${{ steps.check_file_changed.outputs.build_changed }}
        k8s_changed: ${{ steps.check_file_changed.outputs.k8s_changed }}
        sql_changed: ${{ steps.check_file_changed.outputs.sql_changed }}
        flow_changed: ${{ steps.check_file_changed.outputs.flow_changed }}

I commented out one portion of the Set the outputs step and updated it to $GITHUB_OUTPUT. However, when the job runs the flow_changed output is not set. I cant post images, but if I look at the complete job section after the action runs with $GITHUB_OUTPUT flow_changed is not set. It is set when I use the old set-output command.

Upvotes: 11

Views: 13676

Answers (3)

ordinarycobbler
ordinarycobbler

Reputation: 224

When the runner is executing commands in PowerShell, you'll need to format the new output setting like this:

echo "flow_changed=$flow_changed" >> $env:GITHUB_OUTPUT

Note the $env: that's missing from other examples.

Upvotes: 20

nac123456
nac123456

Reputation: 113

After testing I could not get powershell to set the outputs of the job so I wrote it in bash which worked for me.

# Diff HEAD with the previous commit
# filters out deleted files
git_diff=$(git diff --name-only --diff-filter=d HEAD^ HEAD)
            
# Check what files were in the diff
echo "$git_diff"

# set output variables to false
flow_changed="False"
build_changed="False"
sql_changed="False"
k8s_changed="False"
            
for f in ${git_diff[@]};
do
    if [[ $f =~ "flow.py" ]]; then
         flow_changed="True"
    fi

    if [[ $f =~ "Dockerfile" ]] || [[ $f =~ "Pipfile.lock" ]]; then
         build_changed="True"
    fi

    if [[ $f =~ ".sql" ]]; then
         sql_changed="True"
    fi

    if [[ $f =~ "kubernetes_job.py" ]]; then
         k8s_changed="True"
    fi
done

# Set the outputs
echo "build_changed=$build_changed" >> $GITHUB_OUTPUT
echo "k8s_changed=$k8s_changed" >> $GITHUB_OUTPUT
echo "sql_changed=$sql_changed" >> $GITHUB_OUTPUT
echo "flow_changed=$flow_changed" >> $GITHUB_OUTPUT

Upvotes: 0

jwpol
jwpol

Reputation: 1475

Try to set them like this:

echo "build_changed=$HasBuildDiff" >> $GITHUB_OUTPUT
echo "k8s_changed=$HasK8sDiff" >> $GITHUB_OUTPUT
echo "sql_changed=$HasSqlDiff" >> $GITHUB_OUTPUT
echo "flow_changed=$HasFlowDiff" >> $GITHUB_OUTPUT

More information under https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs

Upvotes: 0

Related Questions