C – Context You are working on a Symfony (version 6.1 or higher) web application. The…
- User
C – Context You are working on a Symfony (version 6.1 or higher) web application. The application is deployed using GitLab CI/CD to a remote server via rsync. After each deployment, it's critical to run Doctrine database migrations in a safe, idempotent, and production-ready manner. The focus of this prompt is to generate the Doctrine migration portion of the GitLab CI pipeline while maintaining modularity, security, and best practices. The rest of the pipeline (such as the build, rsync deployment, cache clearing, or service restarting) can be stubbed out using placeholders. R – Role You are an industry-leading DevOps engineer with over 20 years of experience designing CI/CD pipelines for PHP and Symfony applications in production. You are deeply familiar with GitLab CI, Symfony Console commands, Doctrine ORM, and robust deployment practices such as zero-downtime, atomic deploys, and secure SSH access. You also emphasize idempotent, fail-safe database migrations and clear separation of build vs deployment concerns. A – Action Follow these exact steps to generate your response:
- Output a complete .gitlab-ci.yml pipeline, but only fully implement the step responsible for running Doctrine migrations.
- Use placeholder stub comments for other steps (e.g., rsync deployment, cache clearing, etc.) so users can later fill in their own logic.
- For the Doctrine migration step:
- Ensure it runs only after the code is deployed (e.g., depends on a prior deploy stage).
- Use the Symfony CLI command php bin/console doctrine:migrations:migrate --no-interaction.
- Execute the command on the remote server via SSH using a secure method.
- Follow GitLab best practices (e.g., variables for SSH host, user, key).
- Add safety checks or recommendations for backups or maintenance mode if applicable.
- Clearly comment each line in the migration job to explain its purpose.
- Keep the configuration clean and production-ready, avoiding any dev/test-only shortcuts. F – Format Output your answer in a Markdown code block using YAML syntax. Comment important lines using # inline or above the relevant lines. Start the file with a stages: declaration. Do not include any explanation outside the code block — the YAML and its inline comments should be fully self-explanatory. T – Tone Professional and clean, suitable for senior Symfony or DevOps engineers reviewing a GitLab CI config. Prioritize clarity, maintainability, and scalability.
- Assistant
# GitLab CI/CD pipeline for Symfony application deployment with Doctrine migrations. # This pipeline separates concerns into stages: build, deploy, and migrate. # Only the migrate stage is fully implemented; others are stubs for customization. # Assumes GitLab variables are set: SSH_HOST, SSH_USER, SSH_PRIVATE_KEY (multiline), APP_PATH (remote app directory). stages: - build - deploy - migrate build: stage: build script: # Placeholder: Add your build steps here, e.g., composer install, asset building. - echo "Stub: Building application" deploy: stage: deploy script: # Placeholder: Add your deployment steps here, e.g., rsync to remote server. - echo "Stub: Deploying code via rsync" migrate: stage: migrate # Ensure this job runs only after successful deployment. dependencies: - deploy script: # Prepare SSH key for secure remote access. - mkdir -p ~/.ssh - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa # Use GitLab variable for private key. - chmod 600 ~/.ssh/id_rsa # Set secure permissions on the key. - ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts # Add host to known_hosts to avoid prompts. # Recommendation: Before migrations, consider enabling maintenance mode or taking a database backup. # Example: ssh $SSH_USER@$SSH_HOST "cd $APP_PATH && php bin/console maintenance:enable" # Run Doctrine migrations idempotently on the remote server. # The --no-interaction flag ensures non-interactive execution, suitable for CI. # Migrations are designed to be safe and idempotent if versions are managed properly. - ssh $SSH_USER@$SSH_HOST "cd $APP_PATH && php bin/console doctrine:migrations:migrate --no-interaction" # Recommendation: After migrations, disable maintenance mode if enabled. # Example: ssh $SSH_USER@$SSH_HOST "cd $APP_PATH && php bin/console maintenance:disable" # Optional: Add error handling or rollback logic if migration fails. # For production safety, ensure backups are taken prior to this job. # Run this job only on the main branch or as needed; customize rules accordingly. rules: - if: $CI_COMMIT_BRANCH == "main"