mecograph
mecograph

Reputation: 702

Github actions can't seem to find private package in monorepo with yarn workspaces and lerna

I've created a monorepo based on yarn workspaces and lerna which contains the following package types:

And my project structure looks something like this

root
-- package.json
-- packages/
--- ui/
---- package.json
---- dist/
--- dto/
---- package.json
---- dist/
--- serverlessBackend1/
---- package.json
---- build/
--- serverlessBackend2/
---- package.json
---- build/
--- serverlessBackendN/
---- package.json
---- build/

The DTO package contains mostly types, which are used within every other package, therefore it's listed as dependency in every package.json of my packages.

In my root package.json I have the following three basic lerna scripts:

{
    [...]
    "workspaces": [
        "packages/*"
    ],
    "scripts": {
        "build": "lerna run build",
        "publish": "lerna publish --conventional-commits --yes",
        "deploy": "lerna run deploy"
    },
    "dependencies": {
        [...]
    },
    "devDependencies": {
        [...]
    }
}

Now I wanted to create a github actions pipeline, which takes care of distributing the different packages to their destinations. Ftp upload for the website bundle, publishing the dto package to npm and deploying all serverless projects to AWS.

As I'm pretty new to Github actions, I've dug my way through offical documentation, readmes, other projects, stackoverflow questions and managed to set up a pipeline, which works in two of three cases.

Unfortunately the step, where I want to deploy all serverless packages to AWS seems to have a weird issue. First, this is how the Job is configured:

Deploy-to-AWS:
    runs-on: ubuntu-latest
    env: 
        NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
    steps:
        - name: Checkout
          uses: actions/checkout@v2
          with:
            submodules: recursive
            token: ${{ secrets.GITHUB_TOKEN }}
        - name: Installing dependencies
          run: yarn
        - name: Add AWS credentials with profile
          run: |
            aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY }} --profile ${{ secrets.PROFILE_NAME }}
            aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} --profile ${{ secrets.PROFILE_NAME }}
        - name: Deploy to AWS dev
          run: yarn deploy

When I execute yarn deploy locally from within my root dir, everything works as expected and the deploy script in each serverless package is executed and all packages are deployed correctly. This is how the package.json does look like in the serverless packages:

{
    [...]
    "scripts": {
        "build": "tsc",
        "runDeployment": "serverless deploy -v --aws-profile my-profile-name",
        "deploy": "npm run build && npm run runDeployment"
    },
    "dependencies": {
        "@userName/my-private-dto-package": "^0.3.2",
        [...]
    },
    "devDependencies": {
        [...]
    }
}

But when I try the same within the Github actions workflow, I receive an error that the my private package module cannot be found:

2nd-serverless-package: path/to/file/where/dto/is/imported.ts(1,88): error TS2307: Cannot find module '@userName/my-private-dto-package' or its corresponding type declarations.

This seem to happen to every package but the first. So perhaps the dependency is just resolved for the first package?

I've searched the internet up and down but to no avail.

Upvotes: 1

Views: 2075

Answers (1)

mecograph
mecograph

Reputation: 702

I think it might has something to do with the dependencies being symlinked and therefore the DTO package is just available on root level and not directly inside the serverless package.

But I solved it by separating the workflows for each serverless package and installing the dependencies directly.

name: Serverless deployment package-name
on:
  push:
    branches:
      - main
    paths:
      - 'packages/serverlessBackend1/**'

jobs:
  Deploy-to-AWS:
    runs-on: ubuntu-latest
    env: 
      NODE_AUTH_TOKEN: ${{ secrets.NPMRC_AUTH_TOKEN }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup up node
        uses: actions/setup-node@v2
      
      - name: Provide profile credentials
        run: |
          aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY }} --profile my-profile-name
          aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} --profile my-profile-name
      
      - name: Install dependencies
        run: cd packages/serverlessBackend1 && npm install

      - name: Deploy event backend to AWS dev
        run: cd packages/serverlessBackend1 && npm run deploy

This solved it for me entirely. Not the solution to my initial question, therefore not marking it as answer but I thought my findings could perhaps help somebody else.

Upvotes: 1

Related Questions