mattrzr
mattrzr

Reputation: 73

How to use Github Actions to generate a checksum and set env variable?

Would like to generate a checksum via Github Actions and set to env variable CHECKSUM like so:

     - name: Create checksum
       run: echo CHECKSUM=$(shasum -a 1 foo.zip | awk '{ print $1 }') >> $CHECKSUM

but it returns an error:

/home/runner/work/_temp/b6f2fd2a-359b-4052-a439-4f5b0a629a85.sh: line 1: $CHECKSUM: ambiguous redirect

Upvotes: 2

Views: 2410

Answers (2)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4759

Below git hub action generates md5of the file result.json using md5sum and generates and output named original_md5

- name: MD5 of result json file
    id: original_md5_results
    shell: bash
    run: |
      set -e
      echo "original_md5=$(md5sum result.json | awk '{ print $1 }')" >> $GITHUB_OUTPUT

Now in subsequent step where we need this value.

  - name: Compare MD5 post processing
    shell: bash
    run: |
      set -e
      originalMD5="${{steps.original_md5_results.outputs.original_md5}}"
      updatedMD5="$(md5sum result.json | awk '{ print $1 }')"
      echo "Original MD5 ::  $originalMD5"
      echo "Updated MD5 :: $updatedMD5"
      if [ "$originalMD5" = "$updatedMD5" ]; then
        echo "No change results."
      else
        echo 1>&2 "ERROR:: result.json compromised. Please re-check
        exit 1
      fi

Importan Steps:

  1. Assign an id to the job which is producing the output. In our case its id: original_md5_results
  2. Write the output to $GITHUB_OUTPUT in key=value pattern in our case "original_md5=md5valueofthefile"
  3. Now in the consuming step, read the value from the previous step using id and outputs for e.g. ${{steps.original_md5_results.outputs.original_md5}}

Upvotes: 0

Benjamin W.
Benjamin W.

Reputation: 52132

Setting environment variables works differently: you append to a file whose name is stored in the $GITHUB_ENV variable, i.e., something like

run: |
  echo CHECKSUM="$(shasum foo.zip | awk '{ print $1 }')" >> "$GITHUB_ENV"

Upvotes: 2

Related Questions