github-actionsgithub-actions-reusable-workflowsgithub-actions-workflows

How to execute multiple commands in this GitHub Actions workflow job?


I am using GitHub Actions workflow for CI/CD. The main workflow uses a reusable workflow to deploy AWS CDK applications. Currently this uses just one command passed into the reusable workflow and works that way. I need to modify this setup so that I can add one more job to the main workflow which requires 2 commands to be executed back to back in the reusable workflow. In addition to calling the cdk deploy and passing in the args like I am doing here below, I also need to call npm run build just before the cdk deploy statement. How do I modify the 2 files below to make this happen?

The current main workflow with 2 jobs:

  api-deploy:
    needs: run-unit-rests
    uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
    with:
      environment: DEV
      working-directory: app/stacks/api_layer_stack
      cdk-command: deploy
      cdk-args: app_api -c myparam1=bla -c myparam2=bla --require-approval never


  lambda-deploy:
    needs: run-unit-rests
    uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
    with:
      environment: DEV
      working-directory: app/stacks/lambda_layer_stack
      cdk-command: deploy
      cdk-args: app_func -c myparam2=bla --require-approval never
    

The current reusable workflow (reusable_wf.yml) referenced in the main workflow above. This has one job with multiple steps.

on:
  # Reusable
  workflow_call:
    inputs:
      cdk-command:
        type: string
        required: true
      cdk-args:
        type: string
        required: true
      environment:
        type: string
        required: true
      working-directory:
        type: string
        required: true

    jobs:
      run-job:
        runs-on: ubuntu-latest
        environment: ${{ inputs.environment }}
        steps:      
          - name: Download source code
            uses: actions/download-artifact@v4
            with:
              name: app-package
    
   
          - name: Setup Python
            uses: actions/setup-python@v4
            with:
              python-version: '3.12'  
    
          - name: Install CDK
            run: npm install -g aws-cdk
    
          - name: Install Python dependencies
            run: pip install -r infra/requirements.txt
    
          - name: Install Node
            run: npm install node
    
          - name: Install TypeScript
            run: npm install -D typescript
    
          - name: Configure AWS credentials
            uses: aws-actions/configure-aws-credentials@v4
            with:
              role-to-assume: arn:aws:iam::...
              aws-region: ${{ vars.REGION }}
              role-session-name: GHA
    
  - name: Run CDK ${{ inputs.cdk-command }} for Account ${{ vars.ACCOUNT }}
    working-directory: ${{ inputs.working-directory }}
    run: cdk ${{ inputs.cdk-command }} ${{ inputs.cdk-args }}
    env:
      this_ACCOUNT: ${{ vars.ACCOUNT }}
      this_REGION: ${{ vars.REGION }}

I would prefer to keep the existing jobs as is without affecting how they run.


Solution

  • You can support both interfaces in your reusable workflow:

    jobs:
      run-job:
        runs-on: ubuntu-latest
        environment: ${{ inputs.environment }}
        steps:
          - name: Run CDK
            env:
              this_ACCOUNT: ${{ vars.ACCOUNT }}
              this_REGION: ${{ vars.REGION }}
            run: |
              cd ${{ inputs.working-directory }}
              if [ -n "${{ inputs.cdk-command }}" ]; then
                cdk ${{ inputs.cdk-command }} ${{ inputs.cdk-args}}
              elif [ -n "${{ inputs.run }}" ]; then               
                ${{ inputs.run }}
              else
                echo Neither cdk-command not run were provided 
              fi
    

    Then you can mix and match the invocation style:

    api-deploy:
      needs: run-unit-rests
      uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
      with:
        environment: DEV
        working-directory: app/stacks/api_layer_stack
        cdk-command: deploy
        cdk-args: app_api -c myparam1=bla -c myparam2=bla --require-approval never
    
    
    lambda-deploy:
      needs: run-unit-rests
      uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
      with:
        environment: DEV
        working-directory: app/stacks/lambda_layer_stack
        cdk-command: deploy
        cdk-args: app_func -c myparam2=bla --require-approval never
    
    
    api-deploy-multi-command:
      needs: run-unit-rests
      uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
      with:
        environment: DEV
        working-directory: app/stacks/api_layer_stack
        run: |
          cdk deploy app_api -c myparam1=bla -c myparam2=bla --require-approval never
          # Additional commands
    
    lambda-deploy-multi-command:
      needs: run-unit-rests
      uses: my/repo/.github/workflows/reusable_wf.yml@v1.0.1
      with:
        environment: DEV
        working-directory: app/stacks/lambda_layer_stack
        run: |
          cdk deploy app_func -c myparam2=bla --require-approval never
          # Additional commands