Home > other >  GitHub actions ocasionaily silently fails to install node_modules
GitHub actions ocasionaily silently fails to install node_modules

Time:09-10

I'm writing a custom action that will be used in a few repos. It generates a TS client from an openapi file and builds it. The problem only happens 25% of the time, and reruns eventually resolves the failed runs. Sometimes, the npm i or yarn install command silently fails and doesn't install any node_modules. Then when running the orval binary, the file isn't found and it errors (npm run orval, etc. also fail). It's not related to the specific packages, as all binaries are missing. In fact, yarn install executes almost instantly when it goes wrong but not when it works. I'm totally stuck on what is making this so flaky.

Update: I've tried using jq instead of the json command used below. But I still have a problem. The second file is missed. My command looks like jq -s '.[0] * .[1]' ./file1.json ./dir/file2.json and the error is jq: error (at client/package.json:9): object ({"author":"...) and null (null) cannot be multiplied.

name: 'Generate Client'
description: 'Generates a package from a source openapi.yaml file.'
author: ''

inputs:
  API_NAME:
    description: 'Name of the API'
    required: true
  API_TARGET_NAME:
    description: 'API name to use for the schemas output target'
    required: true

runs:
  using: 'composite'
  steps:
  # Checkout generator repo
  - uses: actions/checkout@v3
    with:
      repository: InformaticsMatters/openapi-js-client-generator
      branch: main
  - run: cat yarn.lock
    shell: bash
  - uses: actions/checkout@v3
    with:
      path: "./client"
  # We need Node 16
  - uses: actions/setup-node@v2
    with:
      node-version: 16
  # Get the openapi.yaml file
  - run: |
      curl -H "Private-Token: ${GITLAB_API_TOKEN}" ${GITLAB_API}/${GITLAB_PROJECT}/repository/files/${OPENAPI_PATH}/${OPENAPI_FILE}/raw?ref=${CI_COMMIT_TAG} --output ${OPENAPI_FILE}
      tail ${OPENAPI_FILE}
      cat ${OPENAPI_FILE} | wc -l
    shell: bash
  - run: npm i --location=global json
    shell: bash
  - run: cat package.json client/package.json | json --deep-merge | tee package.json
    shell: bash
  - run: |
      sed -i s/'ORVAL_API_NAME'/'"'${{ inputs.API_NAME }}'"'/ orval.config.ts
      sed -i s/'API_TARGET_NAME'/''${{ inputs.API_TARGET_NAME }}''/ orval.config.ts
      sed -i s/'API_TARGET_NAME'/${{ inputs.API_TARGET_NAME }}/ src/index.ts
    shell: bash
  - run: yarn
    shell: bash
  - run: pwd
    shell: bash
  - run: ls ./node_modules/.bin
    shell: bash
  - run: |
      ./node_modules/.bin/orval
      ./node_modules/.bin/tsup
      ls dist/
      node setup-entrypoints.js
    shell: bash

CodePudding user response:

You must not read and write a single file from two different processes:

cat package.json client/package.json | json --deep-merge | tee package.json

This is very likely to not work as expected (similar to how cmd <file >file will result in an empty file).

What you should do instead:

cat package.json client/package.json | json --deep-merge | tee package.json.tmp
mv package.json.tmp package.json

or maybe

cat package.json client/package.json | json --deep-merge > package.json.tmp
tee package.json < package.json.tmp 

There is also GNU sponge which could be helpful, but is generally not available on all systems:

cat package.json client/package.json | json --deep-merge | sponge package.json
  • Related