Search code examples
asp.net-coregithub-actions

Deploy ASP.NET Core to Azure VM - dotnet: command not found


I have a VM set up in Microsoft Azure. I'm deploying an ASP.NET Core app via GitHub Actions to the Azure VM.

Every step in the deploy.yml file completes successfully, except the final step which throws an error

dotnet: command not found

despite dotnet being installed and me exporting the path.

There seems to be some issue with the path, given it cannot find dotnet.

Here is the deploy.yml file:

name: Deploy to Azure VM

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '8.x'

    - name: Cache .NET packages
      uses: actions/cache@v2
      with:
        path: |
          ~/.nuget/packages
          ~/.dotnet
        key: ${{ runner.os }}-dotnet-${{ hashFiles('**/*.csproj') }}
        restore-keys: |
          ${{ runner.os }}-dotnet-

    - name: Install .NET Core on Azure VM
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.AZURE_VM_IP_ADDRESS }}
        username: ${{ secrets.AZURE_VM_USERNAME }}
        key: ${{ secrets.AZURE_VM_SSH_PRIVATE_KEY }}
        script: |
          wget https://dot.net/v1/dotnet-install.sh
          chmod +x dotnet-install.sh
          sudo ./dotnet-install.sh --install-dir /usr/share/dotnet --version latest
          export PATH=$PATH:/usr/share/dotnet

    - name: Build and publish ASP.NET Core app
      run: |
        dotnet restore
        dotnet build -c Release
        dotnet publish -c Release -o ./publish

    - name: Cache published files
      uses: actions/cache@v2
      with:
        path: |
          ./publish
        key: ${{ runner.os }}-dotnet-publish-${{ hashFiles('**/*.csproj') }}
        restore-keys: |
          ${{ runner.os }}-dotnet-publish-

    - name: Create /applocation folder on Azure VM
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.AZURE_VM_IP_ADDRESS }}
        username: ${{ secrets.AZURE_VM_USERNAME }}
        key: ${{ secrets.AZURE_VM_SSH_PRIVATE_KEY }}
        script: |
          sudo mkdir -p /applocation
          sudo chown $USER:$USER /applocation

    - name: Copy files to Azure VM
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.AZURE_VM_IP_ADDRESS }}
        username: ${{ secrets.AZURE_VM_USERNAME }}
        key: ${{ secrets.AZURE_VM_SSH_PRIVATE_KEY }}
        source: './publish'
        target: '/applocation'

    - name: SSH into Azure VM and deploy
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.AZURE_VM_IP_ADDRESS }}
        username: ${{ secrets.AZURE_VM_USERNAME }}
        key: ${{ secrets.AZURE_VM_SSH_PRIVATE_KEY }}
        script: |
          cd /applocation
          source ~/.bashrc || source ~/.bash_profile || true
          export PATH=$PATH:/usr/share/dotnet
          echo "Current PATH: $PATH"
          which dotnet || true
          dotnet AzureVirtualMachineDeploymentDemo2.dll

    - name: Clean up
      run: |
        rm -rf ./publish

Solution

  • I dont think export PATH=$PATH:/usr/share/dotnet is sufficient between jobs. You could hard-code the path so dotnet restore becomes /usr/share/dotnet/dotnet restore etc to side-step the problem, or you can add uses and with statements to propagate the environment variables between jobs