Search code examples
azuredrupalazure-web-app-servicegithub-actionscicd

Install Composer on Azure web apps(Linux)


I've made a website with Drupal 10, used Composer on my localhost. Then I migrated it to client's server(Azure web app Linux) using Github action CI/CD. The website works great.

Now We have new requirement where I have to install library. As we can not install library without composer.

I have to install composer on the production server but I can see composer folder already on the server.

How to install composer on the server?

If i install will composer folder be overwrite? will it affect on website?


Solution

  • In order for composer get installed and work with PHP + Drupal and Github Actions, Make sure you add composer.json with your PHP version and then install composer if composer.json exists in the build stage of Github Actions workflow:-

    My composer.json:-

    {
        "require": {
            "php": "8.2.14",
            "ext-json": "*"
        }
    }
    

    My Github Action workflow:-

    name: Build and deploy PHP app to Azure Web App - siliconphp
    
    on:
      push:
        branches:
          - master
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v4
    
          - name: Setup PHP
            uses: shivammathur/setup-php@v2
            with:
              php-version: '8.2'
    
          - name: Check if composer.json exists
            id: check_files
            uses: andstor/file-existence-action@v1
            with:
              files: 'composer.json'
    
          - name: Run composer install if composer.json exists
            if: steps.check_files.outputs.files_exists == 'true'
            run: composer validate --no-check-publish && composer install --prefer-dist --no-progress
    
          - name: Zip artifact for deployment
            run: zip release.zip ./* -r
    
          - name: Upload artifact for deployment job
            uses: actions/upload-artifact@v3
            with:
              name: php-app
              path: release.zip
    
      deploy:
        runs-on: ubuntu-latest
        needs: build
        environment:
          name: 'Production'
          url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
        permissions:
          id-token: write #This is required for requesting the JWT
    
        steps:
          - name: Download artifact from build job
            uses: actions/download-artifact@v3
            with:
              name: php-app
    
          - name: Unzip artifact for deployment
            run: unzip release.zip
          
          - name: Login to Azure
            uses: azure/login@v1
            with:
              client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_CAE11DA0DFBC4E8DB1AE0A113DDCC7EE }}
              tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_8F3D8711605B401D8FBC48CACF4E93F9 }}
              subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_7DB451A63F4244D3AD08C4F85F84EFDD }}
    
          - name: 'Deploy to Azure Web App'
            uses: azure/webapps-deploy@v2
            id: deploy-to-webapp
            with:
              app-name: 'siliconphp'
              slot-name: 'Production'
              package: .
              
    

    Output:-

    enter image description here

    You can install the composer manually by ssh'ing into the Web app like below:-

    curl -sS https://getcomposer.org/installer
    

    enter image description here