Skip to content

hugoverjus/test-gitlab-ci

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Client-Server Demo with GitLab CI

A simple Python REST API server and client application demonstrating GitLab CI/CD running on a GitHub-hosted repository.

Project Structure

├── server/
│   └── app.py          # Flask REST API server
├── client/
│   └── client.py       # Python client for the API
├── tests/
│   └── test_server.py  # Unit tests
├── .gitlab-ci.yml      # GitLab CI pipeline configuration
├── requirements.txt    # Python dependencies
└── README.md

Quick Start

1. Install Dependencies

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt

2. Run the Server

python -m server.app

The server will start on http://localhost:5000

3. Use the Client

# Check server health
python -m client.client health

# Add a message
python -m client.client add "Hello, World!" --author "John"

# List all messages
python -m client.client list

# Get a specific message
python -m client.client get 1

# Delete a message
python -m client.client delete 1

4. Run Tests

pytest tests/ -v

API Endpoints

Method Endpoint Description
GET /health Health check
GET /messages Get all messages
POST /messages Add a new message
GET /messages/<id> Get a specific message
DELETE /messages/<id> Delete a message

🔧 Setting Up GitLab CI with a GitHub Repository

This project is designed to have its code on GitHub while running CI/CD pipelines on GitLab. Here's how to set it up:

Option 1: GitLab CI/CD for External Repositories (Recommended)

  1. Create a GitLab account at gitlab.com if you don't have one

  2. Create a new project in GitLab:

    • Go to New ProjectRun CI/CD for external repository
    • Select GitHub as the source
    • Authorize GitLab to access your GitHub account
    • Select this repository from the list
  3. GitLab will automatically:

    • Mirror your GitHub repository
    • Run the CI pipeline defined in .gitlab-ci.yml on every push
    • Report pipeline status back to GitHub (as commit statuses)

Option 2: Manual Repository Mirroring

  1. Create an empty GitLab project

  2. Set up repository mirroring:

    • Go to SettingsRepositoryMirroring repositories
    • Add your GitHub repo URL: https://github.com/YOUR_USERNAME/test-gitlab-ci.git
    • Set direction to Pull
    • Add authentication (GitHub personal access token)
    • Enable Mirror only protected branches if desired
  3. Configure the mirror:

    • Set update frequency (GitLab.com mirrors every 5 minutes minimum)
    • Or trigger manual sync when needed

Option 3: Using GitLab CI with GitHub Actions Bridge

You can also trigger GitLab CI from GitHub Actions. Add this to .github/workflows/trigger-gitlab.yml:

name: Trigger GitLab CI
on: [push, pull_request]
jobs:
  trigger:
    runs-on: ubuntu-latest
    steps:
      - name: Trigger GitLab Pipeline
        run: |
          curl -X POST \
            -F token=${{ secrets.GITLAB_TRIGGER_TOKEN }} \
            -F ref=main \
            https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/trigger/pipeline

CI/CD Pipeline Stages

The GitLab CI pipeline includes:

1. Lint Stage

  • flake8 - Python code linting
  • black - Code formatting check

2. Test Stage

  • pytest - Unit tests with coverage reporting
  • Import verification

3. Build Stage

  • Application startup verification

Environment Variables

No environment variables are required for basic operation. For production deployments, consider setting:

  • FLASK_ENV - Set to production for production deployments
  • FLASK_SECRET_KEY - Secret key for session management

License

MIT License - Feel free to use this as a template for your own projects!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages