githubgithub-actionsbuilding-github-actionsgithub-actions-reusable-workflows

How to share dependencies installation with reusable workflow in github actions


This is my setup.yml for one reusable workflow


on:
  workflow_call:
    secrets:
      ssh_private_key:
        required: true
      known_hosts:
        required: true

jobs:
  setup:
    name: 'Setup dependencies'
    runs-on: ubuntu-latest
    steps:

    # Set up SSH key
    - name: Configure ssh key server
      uses: shimataro/ssh-key-action@v2
      with:
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        name: id_rsa
        known_hosts: ${{ secrets.KNOWN_HOSTS }}
    
    # Set up Java
    - name: Set up JDK 17
      uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'temurin'
    
    # Set up Android SDK    
    - name: Setup Android SDK
      uses: android-actions/setup-android@v3
    
    # Set up Flutter.
    - name: Clone Flutter repository with stable channel
      uses: subosito/flutter-action@v2
      with:
        channel: stable
        cache: true
    - run: flutter doctor -v

    # Checkout debi app code and get packages.
    - name: Clone Debi app repo and get packages
      uses: actions/checkout@v3
    - run: flutter pub get

and this is my another test.yml use to run test

name: 'test flutter'

on:
  workflow_call:

jobs:
  test:
    name: 'run test'
    runs-on: ubuntu-latest
    steps:

    # Test 
    - name: Run tests
      run: |
        flutter pub get
        flutter test
        flutter analyze

so my parent workflow look like this

name: Release to Firebase App Distribution

on:
  workflow_dispatch:
    inputs:
      tester_group:
        description: "Select the group of testers to distribute the app to"
        required: true
        default: "cicd-test"
        type: choice
        options:
          - "cicd-test"
          - "debi-internal"
      release_notes:
        description: 'Release notes'
        type: string
        required: true
        default: 'No release notes provided'
      
      build_number:
        description: 'Build number'
        type: number
        required: true
        default: 34
      

jobs:
  setup:
    uses: ./.github/workflows/setup.yml
    secrets: inherit


  test:
    uses: ./.github/workflows/test.yml
    secrets: inherit
    needs: [setup]
    

  release:
    name: 'release to firebase app distribution'
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      
      # Configure key store
      - name: Create key.properties files and keystore file
        run: |
          echo "storePassword=${{ secrets.ANDROID_STORE_PASSWORD }}" > android/key.properties
          echo "keyPassword=${{ secrets.ANDROID_KEY_PASSWORD }}" >> android/key.properties
          echo "keyAlias=${{ secrets.ANDROID_KEY_ALIAS }}" >> android/key.properties
          echo "storeFile=../debi.jks" >> android/key.properties
          echo "${{ secrets.ANDROID_KEYSTORE_CONTENT_BASE64 }}" | base64 --decode > android/debi.jks
      
      # Build the app
      - name: Build the app
        run: flutter build apk --flavor staging -t lib/main_staging.dart --build-number=${{ inputs.build_number }}
 

however my workflow failed after running the test workflow where the error was

flutter: command not found

Updated

im using the composite action as below

name: 'common setup'

inputs:
  known_hosts:
    description: Known hosts
    required: true
  ssh_private_key:
    description: SSH private
    required: true

runs:
  using: composite
  steps:

    # Set up SSH key
    - name: Configure ssh key server
      shell: bash
      uses: shimataro/ssh-key-action@v2
      with:
        key: ${{ inputs.ssh_private_key }}
        name: id_rsa
        known_hosts: ${{ inputs.known_hosts }}

    # Set up Java
    - name: Set up JDK 17
      shell: bash
      uses: actions/setup-java@v3
      with:
        java-version: 17
        distribution: temurin

    # Set up Android SDK    
    - name: Setup Android SDK
      shell: bash
      uses: android-actions/setup-android@v3

    # Set up Flutter.
    - name: Clone Flutter repository with stable channel
      shell: bash
      uses: subosito/flutter-action@v2
      with:
        channel: stable
        cache: true
      run: flutter doctor -v


    # Checkout debi app code and get packages.
    - name: Clone Debi app repo and get packages
      shell: bash
      uses: actions/checkout@v3
      run: flutter pub get

and i use it in main workflow like this

  release:
    name: 'release to firebase app distribution'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      
      - name: reuse steps
        uses: ./.github/actions/
        with:
          known_hosts: ${{ secrets.KNOWN_HOSTS }}
          ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
      

but it turns out i get the error

workflow failed

how can i solve this


Solution

  • The use of a composite action is the right approach. To make the action work, remove the lines with a shell: key as it is invalid in a step that uses: an action. shell: can only be used with in a run: step.

          shell: bash