linux_wiki:managed_gitlab_ci_python_example

Managed Gitlab CI: Python Example

General Information

If using GitLab.com, you can take advantage of their managed job runners with no setup beyond creating the gitlab-ci file.

This is an example of testing python code.

Checklist

  • Account on GitLab.com
  • A python script to test

Enable Shared Runners

Shared runners should be on by default for your project, but verify they are:

  • Login to your account on GitLab
  • Navigate to your project in GitLab
  • Settings > CI/CD > Click Expand by Runners
  • Ensure that they are enabled

CI File Examples

The .gitlab-ci.yml file belongs in the root of your project.

Create, commit and push the file to your project.


.gitlab-ci.yml
---
# GitLab CI Job
# Check python files syntax and security
 
# Docker image python 3.7
image: python:3.7
 
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
 
# Pip's cache doesn't store the python packages
# To also cache the installed packages, install
# them in a virtualenv and cache it as well.
# This allows multiple stages to use the same venv.
cache:
    paths:
        - .cache/pip
        - venv/
 
# Setup the Python virtualenv
before_script:
    - python -V
    - pip install virtualenv
    - virtualenv venv
    - source venv/bin/activate

stages:
    - build
    - test

build:install packages:
    # Install required packages for testing
    stage: build
    script: pip install bandit black

test:script syntax:
    # Check syntax and formatting
    stage: test
    script: black --check *.py

test:test script security:
    # Check for security issues - warn about them
    stage: test
    allow_failure: true
    script: bandit --recursive *.py
...
  • Note: This ci file could be shorter if the testing image you pull down already had black and bandit installed.

.gitlab-ci.yml
---
# GitLab CI Job
# Check python files syntax and security
 
# Docker image based on latest python 3
image: whowe/py3tester

stages:
    - build
    - test

build:verify packages:
    # Verify required packages for testing
    stage: build
    script:
        - "which bandit"
        - "which black"

test:script syntax:
    # Check syntax and formatting
    stage: test
    script:
        - "black --check *.py"
        - "black --check unit-tests/*.py"

test:test script security:
    # Check for security issues - warn about them
    stage: test
    allow_failure: true
    script: "bandit --recursive *.py"
...

  • linux_wiki/managed_gitlab_ci_python_example.txt
  • Last modified: 2019/07/20 14:50
  • by billdozor