Skip to content

Python용 GitLabCI적용 방법.

How To Upload Private Python Packages to Gitlab

Gitlab에서 제공하는 Package Registry 에 Python 패키지를 업로드 하고 싶다면:

  1. 프로젝트 생성
  2. Settings -> General 에서 Project ID 확인
  3. User Settings -> Access Tokens 에서 토큰 생성.
    • Token Name - 공백없는 간단한 토큰 이름 입력
    • Scope - api 스코프 선택
  4. wheel과 twine 설치.
  5. ~/.pypirc 파일 편집.

    *

[distutils] index-servers =

gitlab

[gitlab] repository = https://{gitlab-base-url}/api/v4/projects/{your-project-id}/packages/pypi username = {your-token-name} password = {your-token-value}

</syntaxhighlight>

* {gitlab-base-url} - For gitlab cloud users, the value is simply gitlab.com. For other gitlab users, the value is something like gitlab.yourcompany.com

#* {your-project-id} - Note that is your numeric project ID as seen above, not the plain text name you gave your project. Ex: 29074488

#* {your-token-name} - The name of your Github personal access token. Ex: pip-access-token

#* {your-token-value} - Ex: 9rdzBgYLB7_xMGFFoUFn

  1. python3 setup.py sdist bdist_wheel 명령으로 빌드. dist/폴더에 *.whl파일 생성 확인.
  2. python3 -m twine upload --repository gitlab dist/* 명령으로 업로드.
  3. Packages & Registries -> Package Registry 에서 확인 가능.
  4. 필요하다면 chmod 600 ~/.pypirc로 권한 설정하자.

.pypirc 파일을 사용하지 않을 경우

자세한 내용은 twine 항목을 참조. 간단히:

TWINE_PASSWORD=<personal_access_token or deploy_token> TWINE_USERNAME=<username or deploy_token_username> python3 -m twine upload --repository-url https://gitlab.example.com/api/v4/projects/<project_id>/packages/pypi dist/*

Example

# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml

# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:latest

# 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
# https://pip.pypa.io/en/stable/topics/caching/
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python --version  # For debugging
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - python setup.py test
    - pip install tox flake8  # you can also use tox
    - tox -e py36,flake8

run:
  script:
    - python setup.py bdist_wheel
    # an alternative approach is to install and run:
    - pip install dist/*
    # run the command here
  artifacts:
    paths:
      - dist/*.whl

pages:
  script:
    - pip install sphinx sphinx-rtd-theme
    - cd doc
    - make html
    - mv build/html/ ../public/
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

deploy:
  stage: deploy
  script: echo "Define your deployment script!"
  environment: production

recc에 적용했던 CI

image: python:3.8.9

services:
  - postgres:13
  - redis:6

variables:
  POSTGRES_USER: recc
  POSTGRES_PASSWORD: recc1234
  POSTGRES_HOST_AUTH_METHOD: trust
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  YARN_CACHE_FOLDER: "$CI_PROJECT_DIR/.cache/yarn"

cache:
  paths:
    - .cache/pip
    - .cache/yarn

stages:
  - test
  - deploy-python
  - deploy-docker

test-core:
  stage: test
  variables:
    RECC_DATABASE_HOST: postgres
    RECC_DATABASE_PORT: 5432
    RECC_DATABASE_USER: recc
    RECC_DATABASE_PW: recc1234
    RECC_DATABASE_NAME: recc.test
    RECC_DATABASE_TYPE: postgres
    RECC_CACHE_HOST: redis
    RECC_CACHE_PORT: 6379
    RECC_CACHE_TYPE: redis
    RECC_SUPPRESS_PRINT: 1
  only:
    - master
    - '/^core-.*$/'
  before_script:
    - bash core/requirements.sh
  script:
    - cd core
    - bash docker-tester.sh
  coverage: '/\d+\%\s*$/'

test-fe:
  image: node:14.15
  stage: test
  only:
    - master
    - '/^fe-.*$/'
  before_script:
    - apt-get -qq update
    - apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
  script:
    - cd fe
    - yarn
    - yarn test:unit
    - yarn test:e2e
    - yarn lint

deploy-core-python:
  stage: deploy-python
  only:
    - '/^core-.*$/'
  before_script:
    - bash core/requirements.sh
    - pip install devpi-client
  script:
    - cd core
    - devpi use "${DEVPI_URL}"
    - devpi login --password "${DEVPI_PW}" "${DEVPI_ID}"
    - devpi upload --with-docs --formats bdist_wheel
    - devpi logout

deploy-core-docker:
  stage: deploy-docker
  only:
    - '/^core-.*$/'
  before_script:
    - apt-get -qq update
    - apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    - curl -fsSL "https://download.docker.com/linux/debian/gpg" | apt-key add -
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    - apt-get -qq update
    - apt-get install -y docker-ce-cli
  script:
    - cd core
    - RECC_VERSION=$(python -c 'from recc.util.version import version_text; print(version_text);')
    - RECC_IMAGE_CURRENT=${REGISTRY_HOST}/answer/recc:${RECC_VERSION}
    - RECC_IMAGE_LATEST=${REGISTRY_HOST}/answer/recc:latest
    - echo "Current recc version is ${RECC_IMAGE_CURRENT}"
    - echo "${REGISTRY_PW}" | docker login --password-stdin -u "${REGISTRY_ID}" "${REGISTRY_HOST}"
    - docker build --tag ${RECC_IMAGE_CURRENT} .
    - docker tag "${RECC_IMAGE_CURRENT}" "${RECC_IMAGE_LATEST}"
    - docker push "${RECC_IMAGE_CURRENT}"
    - docker push "${RECC_IMAGE_LATEST}"
    - docker rmi "${RECC_IMAGE_CURRENT}"
    - docker rmi "${RECC_IMAGE_LATEST}"
    - docker logout

See also

Favorite site