Search code examples
continuous-integrationgithub-actions

Github Actions cannot find package.json in repo


I am using Github Actions for the first time and I am struggling to execute my whole workflow . This is my YAML so far:

name: Deploy authn configuration to prod
on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Download files from the current repository
        uses: actions/checkout@v2
      - name: Install Node.js
        uses: actions/setup-node@v1
        with:
          node-version: "14.x"

  install:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Install Auth0 Deploy CLI Action
        run: npm install -i -g auth0-deploy-cli

  deploy:
    runs-on: ubuntu-latest
    needs: install
    steps:
      - name: Change to root directory
        run: |
          ls -la 
          cd ./auth0_infrastructure
          ls -la
      - name: Import config to prod tenant
        env:
          AUTH0_CLIENT_SECRET: ${{ secrets.PROD_AUTH0_CLIENT_SECRET }}
        run: npm run import:staging

I get this error:

Run ls -la 
/home/runner/work/_temp/0cfef2ac-60f2-4f6a-8016-d1cc5ec1deaf.sh: line 2: cd: ./auth0_infrastructure: No such file or directory
total 8
drwxr-xr-x 2 runner docker 4096 Jul 18 13:21 .
drwxr-xr-x 3 runner docker 4096 Jul 18 13:21 ..
Error: Process completed with exit code 1.

If I remove the 'change to root directory step' in the install job, my error becomes:

Run npm run import:staging
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/runner/work/auth0_infrastructure/auth0_infrastructure/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/runner/work/auth0_infrastructure/auth0_infrastructure/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-07-18T13_13_02_927Z-debug-0.log
Error: Process completed with exit code 254.

What am I doing wrong?

.
├── README.md
├── config
│   ├── prod.json
│   └── staging.json
├── config.json
├── databases
│   └── Username-Password-Authentication
│       ├── change_password.js
│       ├── create.js
│       ├── delete.js
│       ├── get_user.js
│       ├── login.js
│       └── verify.js
├── docs
│   └── HOWTO.md
├── emailTemplates
├── hooks
├── package.json
├── pages
├── rules
│   └── Microsoft Profile Picture.js
└── tenant.yaml

I have used 3 separate jobs as I was under the impression that the steps in a singular job ran in parallel rather than sequentially .


Solution

  • You're running three different jobs which are dependent.

    So, your yaml file should looks like below

    name: Deploy authn configuration to prod
    on:
      push:
        branches: [main]
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Download files from the current repository
            uses: actions/checkout@v2
          - name: Install Node.js
            uses: actions/setup-node@v1
            with:
              node-version: "14.x"
          - name: build
            run: <build_command>
    
      install:
        runs-on: ubuntu-latest
        needs: build
        steps:
          - name: Download files from the current repository
            uses: actions/checkout@v2
          - name: Install Node.js
            uses: actions/setup-node@v1
            with:
              node-version: "14.x"
          - name: Install Auth0 Deploy CLI Action
            run: npm install -i -g auth0-deploy-cli
    
      deploy:
        runs-on: ubuntu-latest
        needs: install
        steps:
          - name: Download files from the current repository
            uses: actions/checkout@v2
          - name: Install Node.js
            uses: actions/setup-node@v1
            with:
              node-version: "14.x"
          - name: Change to root directory
            run: |
              ls -la 
              cd ./auth0_infrastructure
              ls -la
          - name: Import config to prod tenant
            env:
              AUTH0_CLIENT_SECRET: ${{ secrets.PROD_AUTH0_CLIENT_SECRET }}
            run: npm run import:staging
    

    The issue is, each jobs run in a different machine, and you have to clone the repo and setup NodeJS on each job.

    To share build between your jobs, checkout this actionUpload Artifact, Download Artifact where you can upload your build from build job, and download the same in deploy job.