Bird
Raised Fist0
Djangoframework~10 mins

CI/CD pipeline basics in Django - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - CI/CD pipeline basics
Code Commit
Trigger Pipeline
Build Stage
Test Stage
Deploy Stage
Production Environment
Feedback & Monitoring
This flow shows how code changes trigger automated steps: build, test, deploy, and then monitoring in production.
Execution Sample
Django
steps:
  - name: Build
    run: python manage.py collectstatic --noinput
  - name: Test
    run: python manage.py test
  - name: Deploy
    run: echo "Deploying to server..."
This sample pipeline runs build, test, and deploy commands for a Django project.
Execution Table
StepActionCommand RunResultNext Step
1Build Stagepython manage.py collectstatic --noinputStatic files collected successfullyProceed to Test Stage
2Test Stagepython manage.py testAll tests passedProceed to Deploy Stage
3Deploy Stageecho "Deploying to server..."Deployment simulatedPipeline Complete
4Pipeline End--No further steps
💡 Pipeline ends after deploy stage completes successfully
Variable Tracker
VariableStartAfter BuildAfter TestAfter Deploy
pipeline_statusNot startedBuild successfulTests passedDeployed
Key Moments - 2 Insights
Why does the pipeline stop if tests fail?
The pipeline only moves to deploy if tests pass, as shown in execution_table row 2 where 'All tests passed' is required to proceed.
What happens if static files are not collected properly?
If the build stage fails (row 1), the pipeline will not proceed to testing or deployment to avoid deploying broken code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the pipeline_status after the Test Stage?
ATests passed
BBuild successful
CDeployed
DNot started
💡 Hint
Check variable_tracker column 'After Test' for pipeline_status value.
At which step does the pipeline simulate deployment?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row with 'Deploy Stage' and the command run.
If tests fail, what happens to the pipeline?
AIt continues to deploy
BIt restarts build
CIt stops before deploy
DIt skips tests
💡 Hint
Refer to key_moments explanation about pipeline stopping on test failure.
Concept Snapshot
CI/CD pipeline automates code delivery:
1. Code commit triggers pipeline
2. Build stage prepares code (e.g., collect static files)
3. Test stage runs automated tests
4. Deploy stage releases code if tests pass
5. Pipeline stops if any stage fails
Automates safe, fast delivery of Django apps.
Full Transcript
A CI/CD pipeline for Django starts when code is committed. The pipeline runs a build step to collect static files, then runs tests to check code quality. If tests pass, it proceeds to deploy the app. If any step fails, the pipeline stops to prevent bad code from reaching production. This automation helps deliver updates quickly and safely.

Practice

(1/5)
1. What is the main purpose of a CI/CD pipeline in a Django project?
easy
A. To automate testing and deployment for faster and safer code delivery
B. To manually review code changes before deployment
C. To write Django models automatically
D. To create database backups

Solution

  1. Step 1: Understand CI/CD pipeline purpose

    A CI/CD pipeline automates the process of testing and deploying code changes to reduce errors and speed up delivery.
  2. Step 2: Match purpose with options

    To automate testing and deployment for faster and safer code delivery correctly states automation of testing and deployment, which is the core of CI/CD pipelines.
  3. Final Answer:

    To automate testing and deployment for faster and safer code delivery -> Option A
  4. Quick Check:

    CI/CD automates testing and deployment = D [OK]
Hint: CI/CD means automating tests and deploys [OK]
Common Mistakes:
  • Confusing CI/CD with manual code review
  • Thinking CI/CD creates Django code automatically
  • Assuming CI/CD is for backups
2. Which of the following is the correct syntax to define a job named test in a GitLab CI/CD pipeline YAML file?
easy
A. test: script: - python manage.py test
B. job test { run: python manage.py test }
C. - job: test steps: - script: python manage.py test
D. test => { script: 'python manage.py test' }

Solution

  1. Step 1: Recall GitLab CI YAML job syntax

    GitLab CI jobs are defined as job_name: followed by script: list with commands.
  2. Step 2: Compare options with correct YAML syntax

    test: script: - python manage.py test matches the correct YAML format for a job named test running the Django test command.
  3. Final Answer:

    test:\n script:\n - python manage.py test -> Option A
  4. Quick Check:

    GitLab CI job syntax uses job_name: and script: list = A [OK]
Hint: GitLab CI jobs use job_name: and script: list [OK]
Common Mistakes:
  • Using curly braces or other languages syntax
  • Missing the dash before script commands
  • Confusing job syntax with other CI tools
3. Given this GitHub Actions workflow snippet for a Django project:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: python manage.py test

What will happen when this workflow runs?
medium
A. The workflow will fail because Python version is not specified
B. The workflow will check out code, set Python 3.12, install dependencies, and run Django tests
C. The workflow will skip installing dependencies
D. The workflow will deploy the Django app automatically

Solution

  1. Step 1: Analyze each step in the workflow

    The workflow checks out code, sets up Python 3.12, installs dependencies from requirements.txt, then runs Django tests.
  2. Step 2: Confirm expected behavior

    All steps are valid and will run in order, so tests will execute after setup.
  3. Final Answer:

    The workflow will check out code, set Python 3.12, install dependencies, and run Django tests -> Option B
  4. Quick Check:

    Steps run in order: checkout, setup, install, test = A [OK]
Hint: Read steps top to bottom to predict workflow actions [OK]
Common Mistakes:
  • Assuming Python version missing causes failure
  • Thinking dependencies are skipped
  • Confusing test run with deployment
4. You have this GitLab CI job snippet:
test:
  script:
    - python manage.py test
  only:
    - main
    - develop

But tests are not running on your feature branch pushes. What is the likely problem?
medium
A. The script command is incorrect
B. The pipeline YAML file is missing
C. The job is limited to run only on main and develop branches
D. The tests are skipped because of syntax error in Python code

Solution

  1. Step 1: Understand the 'only' keyword in GitLab CI

    The 'only' keyword restricts job execution to specified branches, here main and develop only.
  2. Step 2: Analyze why feature branches don't run tests

    Since feature branches are not listed, the job does not run on them.
  3. Final Answer:

    The job is limited to run only on main and develop branches -> Option C
  4. Quick Check:

    'only' limits branches = B [OK]
Hint: 'only' controls branches where job runs [OK]
Common Mistakes:
  • Thinking script command is wrong without checking
  • Assuming pipeline file is missing
  • Blaming test code syntax without evidence
5. You want to create a CI/CD pipeline for your Django app that runs tests only if code changes affect models.py or views.py. Which GitLab CI configuration snippet correctly implements this?
hard
A. test: script: - python manage.py test only: refs: - main - develop
B. test: script: - python manage.py test except: changes: - models.py - views.py
C. test: script: - python manage.py test when: manual
D. test: script: - python manage.py test only: changes: - models.py - views.py

Solution

  1. Step 1: Understand 'only: changes' in GitLab CI

    This setting runs the job only if specified files change in the commit.
  2. Step 2: Match requirement with options

    test: script: - python manage.py test only: changes: - models.py - views.py uses 'only: changes' with models.py and views.py, so tests run only if these files change.
  3. Final Answer:

    test:\n script:\n - python manage.py test\n only:\n changes:\n - models.py\n - views.py -> Option D
  4. Quick Check:

    'only: changes' triggers job on file changes = C [OK]
Hint: 'only: changes' runs job on specific file changes [OK]
Common Mistakes:
  • Using 'except' instead of 'only' for changes
  • Using branch refs instead of file changes
  • Setting job to manual instead of automatic