azurecontinuous-integration

Artisan migrate with ci/cd on azure container app


I am mouting a container app that is working fine. Before for migration I was passing by ssh directly in my container app. But now I want to after push and deploy my docker image, it will launch my migration and seeder.

In web app it was not complicated but I can't find a way to do that in container app

name: Trigger auto deployment for web

# When this action will be executed
on:
 # Automatically trigger it when detected changes in repo
 push:
   branches:
     [ test-arnaud ]
   paths:
   - '**'
   - '.github/workflows/web-AutoDeployTrigger-1ae8bc9c-b556-46e6-bf83-02fb4f7e5ef1.yml'

 # Allow manual trigger
 workflow_dispatch:

jobs:
 build-and-deploy:
   runs-on: ubuntu-latest
   permissions:
     id-token: write #This is required for requesting the OIDC JWT Token
     contents: read #Required when GH token is used to authenticate with private repo

   steps:
     - name: Checkout to the branch
       uses: actions/checkout@v2

     - name: Azure Login
       uses: azure/login@v1
       with:
         client-id: ${{ secrets.KINGWEB_AZURE_CLIENT_ID }}
         tenant-id: ${{ secrets.KINGWEB_AZURE_TENANT_ID }}
         subscription-id: ${{ secrets.KINGWEB_AZURE_SUBSCRIPTION_ID }}

     - name: Build and push container image to registry
       uses: azure/container-apps-deploy-action@v2
       with:
         appSourcePath: ${{ github.workspace }}
         _dockerfilePathKey_: _dockerfilePath_
         registryUrl: container.azurecr.io
         registryUsername: ${{ secrets.KINGWEB_REGISTRY_USERNAME }}
         registryPassword: ${{ secrets.KINGWEB_REGISTRY_PASSWORD }}
         containerAppName: web
         resourceGroup: king-plateforme-neu-dev-rg
         imageToBuild: container.azurecr.io/web:${{ github.sha }}
         _buildArgumentsKey_: |
           _buildArgumentsValues_


I try with some az containerapp exec with no success. I know for GCP it was little tricky but succeed by finding some obscur things :) but for azure container apps I am out of mind.

thanks a lot

UPDATE

FROM webdevops/php-nginx:8.3-alpine

# Installation dans votre Image du minimum pour que Docker fonctionne
RUN apk add oniguruma-dev libxml2-dev
RUN docker-php-ext-install \
        bcmath \
        ctype \
        fileinfo \
        mbstring \
        pdo_mysql \
        xml

# Installation dans votre image de Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Installation dans votre image de NodeJS
RUN apk add nodejs npm

ENV WEB_DOCUMENT_ROOT /app/public
ENV APP_ENV production
WORKDIR /app
COPY . .

# On copie le fichier .env.example pour le renommer en .env
# Vous pouvez modifier le .env.example pour indiquer la configuration de votre site pour la production
RUN cp -n .env.example .env

# Installation et configuration de votre site pour la production
# https://laravel.com/docs/10.x/deployment#optimizing-configuration-loading
RUN composer install --no-interaction --optimize-autoloader --no-dev
# Generate security key
RUN php artisan key:generate
# Optimizing Configuration loading
RUN php artisan config:cache
# Optimizing Route loading
RUN php artisan route:cache
# Optimizing View loading
RUN php artisan view:cache

# Compilation des assets de Breeze (ou de votre site)
RUN npm install
RUN npm run build

RUN chown -R application:application .
CMD ["sh", "-c", "run_migration_and_seed.sh"]

Adding that to my dockerfil seems not launching the .sh I added just a php artisan migrate into my sh

#24 [stage-0 16/16] RUN chown -R application:application .
#24 DONE 4.8s

#25 exporting to image
#25 exporting layers
#25 exporting layers 8.1s done
#25 writing image sha256:ae2742756b94b1973d1f6dd0b9178e233c73a2a86f0ba7657b59e7e5a75f5b2d done

UPDATE FINAL

I do a function APP in node JS to call the php artisan migrate and in my yaml I call this function for my container, it's working well too. thanks all


Solution

  • Above issue lies in the way you are defining and attempting to run the shell script (run_migration_and_seed.sh).

    FROM webdevops/php-nginx:8.3-alpine
    
    # Install required dependencies
    RUN apk add oniguruma-dev libxml2-dev
    RUN docker-php-ext-install \
        bcmath \
        ctype \
        fileinfo \
        mbstring \
        pdo_mysql \
        xml
    
    # Install Composer
    COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
    
    # Install Node.js and npm
    RUN apk add nodejs npm
    
    # Set environment variables
    ENV WEB_DOCUMENT_ROOT /app/public
    ENV APP_ENV production
    
    # Set the working directory
    WORKDIR /app
    
    # Copy application files
    COPY . .
    
    # Ensure `.env` exists
    RUN cp -n .env.example .env
    
    # Install dependencies and optimize the application
    RUN composer install --no-interaction --optimize-autoloader --no-dev
    RUN php artisan key:generate
    RUN php artisan config:cache
    RUN php artisan route:cache
    RUN php artisan view:cache
    
    # Build frontend assets
    RUN npm install
    RUN npm run build
    
    # Set permissions
    RUN chown -R application:application .
    
    # Copy and set permissions for the migration script
    COPY run_migration_and_seed.sh /usr/local/bin/run_migration_and_seed.sh
    RUN chmod +x /usr/local/bin/run_migration_and_seed.sh
    
    # Set the command to run migrations and start the server
    CMD ["sh", "-c", "/usr/local/bin/run_migration_and_seed.sh"]
    

    The CMD syntax is incorrect. check it uses the absolute path to the script.

    With Azure Container Apps Jobs, you can run specific workloads on-demand or on a schedule using the same container image as the main application, but configured to perform different tasks.

    You can reuse your existing container image or create a specific one for migrations and seeding.

    Dockerfile:

    FROM your-base-image
    
    # Add migration script
    COPY run_migration_and_seed.sh /usr/local/bin/run_migration_and_seed.sh
    RUN chmod +x /usr/local/bin/run_migration_and_seed.sh
    
    # Default to running the application
    CMD ["sh", "-c", "if [ \"$IS_MIGRATION\" = \"true\" ]; then run_migration_and_seed.sh; else start_server.sh; fi"]
    

    After deploying your container, you can trigger the job to run migrations in your GitHub Actions workflow.

    - name: Trigger migration job
      run: |
        az containerapp job run \
          --name migration-job \
          --resource-group king-plateforme-neu-dev-rg \
          --env-vars DATABASE_URL="your-db-connection-string"