GitHub Actions failed to find cdk command although it was installed from the previous jobs

682 views Asked by At

I expected to preset the environment of node and then it installs aws-cdk which will be used for the matrix-ed jobs which are supposed to be an efficient solution for execution.

Here is basically my script:

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        check-latest: true
        cache-dependency-path: package-lock.json # include aws-cdk already
    - name: Install AWS CDK
      run: 'npm ci'

  deploy:
    runs-on: ubuntu-latest
    needs: [setup]
    if: needs.build.result == 'success'
    strategy:
      matrix:
        folder: ${{ fromJSON(needs.detect-changed-files.outputs.matrix) }}
    steps:  
    - uses: actions/checkout@v3
      with:
        fetch-depth: 2
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
        role-to-assume: my-role
        role-duration-seconds: 14400 # You can find max duration by following this article, https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html#id_roles_use_view-role-max-session
    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1
    - name: Deploy Service
      working-directory: services/infra
      env:
        CLOUD_FORMATION_ROLE: my-role
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
      run: |
        ENV=$(cat ../../artifact.txt)
        cdk deploy "*" -c app_name=my-app -c environment=prod -c image=${{env.ECR_REGISTRY}}/${{ matrix.folder }}:prod --require-approval never -r ${{env.CLOUD_FORMATION_ROLE}}

The error trace says: "line 2: cdk: command not found" in the step of "Deploy Service". I have no clue since cdk should have been installed in the setup job.

3

There are 3 answers

0
Matteo On

Jobs are separated task that run in it's own container/host. could run in parallel. So, you need to install the required sw in each jobs that require it. So try to merge in a single job like:

jobs:
  setup-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 2
    - uses: actions/setup-node@v3
      with:
        check-latest: true
        cache-dependency-path: package-lock.json # include aws-cdk already
    - name: Install AWS CDK
      run: 'npm ci'
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
        role-to-assume: my-role
        role-duration-seconds: 14400 # You can find max duration by following this article, https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html#id_roles_use_view-role-max-session
    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1
    - name: Deploy Service
      working-directory: services/infra
      env:
        CLOUD_FORMATION_ROLE: my-role
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
      run: |
        ENV=$(cat ../../artifact.txt)
        cdk deploy "*" -c app_name=my-app -c environment=prod -c image=${{env.ECR_REGISTRY}}/${{ matrix.folder }}:prod --require-approval never -r ${{env.CLOUD_FORMATION_ROLE}}
1
DanielT On

You have to cache your artifacts between jobs in order to share data. This upload-artifact GitHub Action allows you to cache dependencies between jobs. So, to answer your question you will need to upload your dependency in the "setup" job with the upload-artifact and then, download it in the deploy job with the download-artifact

0
stephenkelzer On

You should update your call to be npx cdk deploy... When you aren't running a package.json script, you need to prefix your npm package calls with npx.

docs: https://docs.npmjs.com/cli/v10/commands/npx

Alternative, you can take your entire command, place it in the scripts section of your package.json file, then call npm run {command name} in your GitHub Action step.