Search code examples
bashgithubgithub-actions

Github Actions: How to share bash aliases across steps?


I am using the following workflow to test out bash aliases. I have defined a bash alias called lll in the first step that I am trying to re-use in the next step. However, the workflow fails in the second step as the lll is not found. So, is there a way to share aliases across steps?

on:
  push:
    branches:
      - master
  release:
    types:
      - created

jobs:
  ossie_job:
    runs-on: ubuntu-latest
    name: Audit Python dependencies 
    steps:
      - name: Print a greeting
        run: |
          shopt -s expand_aliases
          alias lll=ls
          echo 'alias lll=ls' >> ~/.bashrc
          lll
      - name: Test alias
        shell: bash {0}
        run: |
          shopt -s expand_aliases
          source ~/.bashrc
          lll

Solution

  • In Github Actions the default ~/.bashrc has this in the beginning:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    So source ~/.bashrc basically does nothing since it's not in an interactive shell.

    You can use a tmp file like this:

    steps:
      - shell: bash
        run: |
          rc=/tmp/rcfile
          echo 'shopt -s expand_aliases' > $rc
          echo 'alias ll="ls -l" ' >> $rc
    
      - shell: bash
        run: |
          source /tmp/rcfile
          ll