Skip to content

Instantly share code, notes, and snippets.

@todgru
Created April 20, 2023 19:25
Show Gist options
  • Save todgru/182023a9e3795446a65f490f13368873 to your computer and use it in GitHub Desktop.
Save todgru/182023a9e3795446a65f490f13368873 to your computer and use it in GitHub Desktop.
Github Actions - sharing string files between jobs, base64

Github Actions - sharing string files between jobs

Store multiline string as base64 -w 0 job output, and consume in subsequent job.

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      envFile: ${{ steps.tempEnvFile.outputs.envFile }}
    steps:
      ... some step that builds .env file
      - name: Cat .env file, encode and save as job output
        id: tempEnvFile
        run: |
          ENV_FILE="$(cat .env | base64 -w 0)"
          echo "envFile=$ENV_FILE" >> "$GITHUB_OUTPUT"
  use-env-file:
    needs: build
    runs-on: ubuntu-latest
    env:
      envFile: ${{ needs.build.outputs.envFile }}
    steps:
      - name: Decode previous job output and save to file.
        run: echo $envFile | base64 -d > .env

Github Actions ubuntu-latest images supports base64 -w 0, disabling column width restriction. This allows a single long string to be later decoded. Watch out for strings that are too long. Max size is 1Mb.

From base64 man page:

‘-w cols’

‘--wrap=cols’

During encoding, wrap lines after cols characters. This must be a positive number.

The default is to wrap after 76 characters. Use the value 0 to disable line wrapping altogether.

‘-d’

‘--decode’

Change the mode of operation, from the default of encoding data, to decoding data. Input is expected to be base64 encoded data, and the output will be the original data.

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