Skip to content

Commit

Permalink
Merge pull request #141 from plone/131-gitlab-ci
Browse files Browse the repository at this point in the history
Gitlab CI
  • Loading branch information
gforcada committed Jun 21, 2023
2 parents e4474c0 + 43d30ee commit deb3fb8
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
22 changes: 21 additions & 1 deletion config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Currently the files managed by `plone/meta` are the following.
- `.editorconfig`: configuration meant to be read by code editors
- `.flake8`: [`flake8`](https://pypi.org/project/flake8) configuration
- `.gitignore`: list of file/folders patterns that `git` should ignore
- `.github/workflows/meta.yml`: GitHub Actions to run the testing and QA tools on GitHub
- `.github/workflows/meta.yml`: GitHub Actions to run the testing and QA tools on GitHub (if the repository is hosted in GitHub.com)
- `.gitlab-ci.yml`: GitLab CI configuration (if the repository is hosted in GitLab.com)
- `.pre-commit-config.yaml`: [`pre-commit`](https://pypi.org/project/pre-commit) configuration
- `pyproject.toml`: configuration options for a wide variety of Python tooling
- `tox.ini`: [`tox`](https://pypi.org/project/tox) configuration, __the most important file__
Expand Down Expand Up @@ -126,6 +127,25 @@ _your own configuration lines_

_At this time there are no configuration options_.

### `.gitlab-ci.yml`

Add the `[gitlab]` TOML table in `.meta.toml`,
and set the extra configuration for GitLab CI under the `extra_lines` key.

```toml
[gitlab]
extra_lines = """
_your own configuration lines_
"""
```

Specify a custom docker image if the default does not fit your needs on the `custom_image` key.

```toml
[gitlab]
custom_image = "python:3.11-bullseye"
```

### `.pre-commit-config.yaml`

Add the `[pre_commit]` TOML table in `.meta.toml`,
Expand Down
27 changes: 26 additions & 1 deletion config/config-package.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from shared.git import get_branch_name
from shared.git import get_commit_id
from shared.git import git_branch
from shared.git import git_server_url
from shared.path import change_dir
from shared.toml_encoder import TomlArraySeparatorEncoderWithNewline

Expand All @@ -29,6 +30,8 @@

PLONE_CONSTRAINTS_FILE = 'https://dist.plone.org/release/6.0-dev/constraints.txt'

DOCKER_IMAGE = 'python:3.11-bullseye'


def handle_command_line_arguments():
"""Parse command line options"""
Expand Down Expand Up @@ -97,6 +100,16 @@ def __init__(self, args):
self.meta_cfg['meta']['template'] = self.config_type
self.meta_cfg['meta']['commit-id'] = get_commit_id()

with change_dir(self.path):
server_url = git_server_url()
self.is_github = 'github' in server_url
self.is_gitlab = 'gitlab' in server_url
if not self.is_github and not self.is_gitlab:
self.print_warning(
'CI configuration',
'The repository is not hosted in github nor in gitlab, no CI configuration will be done!'
)

def _read_meta_configuration(self):
"""Read and update meta configuration"""
meta_toml_path = self.path / '.meta.toml'
Expand Down Expand Up @@ -309,11 +322,22 @@ def news_entry(self):
return destination.relative_to(self.path)

def gha_workflows(self):
if not self.is_github:
return []
folder = self.path / '.github' / 'workflows'
folder.mkdir(parents=True, exist_ok=True)
destination = folder / 'meta.yml'
return self.copy_with_meta('meta.yml.j2', destination=destination)

def gitlab_ci(self):
if not self.is_gitlab:
return []
options = self._get_options_for('gitlab', ('custom_image', 'extra_lines', ))
if not options['custom_image']:
options['custom_image'] = DOCKER_IMAGE
destination = self.path / '.gitlab-ci.yml'
return self.copy_with_meta('gitlab-ci.yml.j2', destination=destination, **options)

def flake8(self):
options = self._get_options_for('flake8', ('extra_lines', ))
destination = self.path / '.flake8'
Expand Down Expand Up @@ -410,7 +434,8 @@ def configure(self):
self.tox,
self.news_entry,
self.flake8,
self.gha_workflows
self.gha_workflows,
self.gitlab_ci,
)
for method in methods:
files = method()
Expand Down
77 changes: 77 additions & 0 deletions config/default/gitlab-ci.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
image: %(custom_image)s
##
# Add extra configuration options in .meta.toml:
# [gitlab]
# custom_image = "python:3.11-bullseye"
##

stages:
- qa
- test

##
# JOBS
##
lint:
stage: qa
script:
- pip install tox
- tox -e lint
except:
- schedules

release-ready:
stage: qa
script:
- pip install tox
- tox -e release-check
except:
- schedules

dependencies:
stage: qa
script:
- pip install tox
- tox -e dependencies
except:
- schedules

circular-dependencies:
stage: qa
script:
- apt-get update
- apt-get install -y graphviz graphviz-dev
- pip install tox
- tox -e circular
except:
- schedules

testing:
stage: test
script:
- pip install tox
- tox -e test
except:
- schedules

coverage:
stage: test
script:
- pip install tox
- tox -e coverage
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
except:
- schedules

%(extra_lines)s
##
# Add extra configuration options in .meta.toml:
# [gitlab]
# extra_lines = """
# _your own configuration lines_
# """
##
6 changes: 6 additions & 0 deletions config/shared/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ def git_branch(branch_name) -> bool:
call('git', 'checkout', '-b', branch_name)
updating = False
return updating

def git_server_url():
"""Return the repository URL"""
output = call('git', 'remote', 'get-url', 'origin', capture_output=True)
url = output.stdout.splitlines()[0]
return url

0 comments on commit deb3fb8

Please sign in to comment.