AlexPshul / nxazure

MIT License
16 stars 7 forks source link

Programming Model v4 deployments #30

Closed JonSilver closed 9 months ago

JonSilver commented 9 months ago

Has anyone had any luck getting their @nxazure/func projects to deploy? We've tried GitHub actions and the VSCode Azure Extension, but even when it appears to have been deployed successfully, there are no functions recognised by the function app. But it all runs just fine locally using the :start target. We're using the new Programming Model v4 with Typescript code.

AlexPshul commented 9 months ago

Hi @JonSilver , I'd be happy to help you figure this out. In my project, I'm publishing the functions via GitHub actions. One of the things that the package provides is the ability to also publish it to the cloud.

Simply use the executor publish to do so. :) Here is the nx console command: npx nx publish my-nx-app-name -n my-deployed-func-app-name

For GitHub Actions, you should make sure you install dependencies and also login to Azure before you publish it. Here is a job example:

publish-function:
    name: ⚡ Publish function
    runs-on: ubuntu-latest
    steps:
      # Azure Login
      - name: ☁️🪧 Azure Login
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      # Publish App
      - name: ✅ Checkout
        uses: actions/checkout@v3
      - name: 👷‍♂️🛠️ Install Dependencies
        run: npm install # Or whatever type of dependencies installation (or cache download) you have
      - name: ⚡ Publish Function
        run: npx nx publish my-nx-app-name -n my-deployed-func-app-name

Let me know if it works or not

AlexPshul commented 9 months ago

Thinking about it, I'll even add this to the docs later :)

JonSilver commented 9 months ago

Thanks @AlexPshul - I shall try this today. We've been using GitHub Actions based upon a modified version of the default one, incorporating the Azure/functions-action@v1 deployment step, having done all the nx building on the deployment server. I saw the publish target in the project.json file and tried it locally with no success, but never thought of running it during the deployment script. It makes sense now! 😂

JonSilver commented 9 months ago

a little later... Having significant problems getting azure/login@v1 to work, despite having a set of credentials generated by the Azure Portal and successfully inserted into the repo's secrets. But I'll struggle on...

JonSilver commented 9 months ago

Right... so... I did the following:

  1. created a managed identity using az ad sp create-for-rbac --name "myApp" --role contributor --scopes /subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.Web/sites/<app-name> --json-auth
  2. put the creds JSON into a repo secret on GitHub
  3. referenced the creds secret in a script almost identical to yours

Alas, the deployment is failing during the publish phase:

Run npx nx publish pm4test -n pm4trial --typescript
  npx nx publish pm4test -n pm4trial --typescript
  shell: /usr/bin/bash -e ***0***
  env:
    NPM_TOKEN: ***

> nx run pm4test:publish -n pm4trial --typescript
Compiling TypeScript files for project "pm4test"...
Done compiling TypeScript files for project "pm4test".
added 9 packages, and audited 10 packages in [2](https://github.com/company/repo/actions/runs/7856371064/job/21439060662#step:6:2)s
1 package is looking for funding
  run `npm fund` for details
found 0 vulnerabilities
Can't determine project language from files. Please use one of [--csharp, --javascript, --typescript, --java, --python, --powershell, --custom]
Your Azure Function App has 'FUNCTIONS_WORKER_RUNTIME' set to 'node' while your local project is set to 'None'.
You can pass --force to update your Azure app with 'None' as a 'FUNCTIONS_WORKER_RUNTIME'

Any idea how I can get past this @AlexPshul ?

Thanks! 😊

AlexPshul commented 9 months ago

Glad you made it work till this point. Based on the error, it looks like you don't have the FUNCTIONS_WORKER_RUNTIME app setting. Could you add FUNCTIONS_WORKER_RUNTIME to your local.settings.json file and set it to node? It should have been done automatically via the @nxazure/func tool but maybe it was removed for some reason at some point.

Anyway, this should solve the problem. Also, before running the GitHub Action each time, it should also work locally. If it works locally, it should also work on your GitHub action workflow. Running the publish command locally will just give you much shorter trial & error cycle. :)

Let me know if you solved it or not. I'm curios! 😁

JonSilver commented 9 months ago

Aha, we routinely .gitignore our local.settings.json in case it's been used for anything containing keys (yes, probably encouraging bad practices! 😊), so it never reaches the deployment server as it's not in the repo.

So I could insert a workflow step to locally create the file with that setting... or just check in the file and ensure anything sensitive goes in the key vault.

I'll try one of these and get back to you.

The one time I tried it, the publish target didn't work and ended in an error, but it's many hundreds of attempts ago and I can't recall what it was so I'll try that again too. 😁

Thanks again!

JonSilver commented 9 months ago

@AlexPshul Yay! It all works! Thank you for all your help. We now have a process that is entirely repeatable and simple to implement for as many function apps as we choose to put in our nx monorepo.

Can I thank you by contributing some documentation?

AlexPshul commented 9 months ago

Glad it worked out! I'm always happy for contributions to the repo! 😃

What did you end up doing? Adding the local.settings.json? Please share the steps that helped you, it could save someone else a lot of time. :)

JonSilver commented 9 months ago

We ended up being good kids and using the key vault 🤣

Once I’ve documented everything for us, I’ll submit a PR with an .md file you can link from the ReadMe, containing all the detailed steps and options for setting up and operating the deployment process.

Once again, thanks for all your help @AlexPshul.

AlexPshul commented 9 months ago

We ended up being good kids and using the key vault 🤣

LOL! I'm glad I pushed you towards being good kids. 😜 Anyway, I'll close this issue for now and whenever you're ready, feel free to open that PR you've mentioned. I'll be super happy. :)