Fix CI command to build

This commit is contained in:
jpic 2020-05-31 03:53:53 +02:00
parent 0a983340b8
commit 70e354dd02
8 changed files with 99 additions and 34 deletions

View File

@ -6,9 +6,9 @@ build:
script: script:
- dnf install -y curl python38 - dnf install -y curl python38
- curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py - curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
- python3.8 get-pip.py - python3 get-pip.py
- pip3.8 install -U --user -e .[cli] - pip3 install -U --user -e .[cli]
- CACHE_DIR=$(pwd)/.cache ~/.local/bin/shlax ./shlaxfile.py build - CACHE_DIR=$(pwd)/.cache python3 ./shlaxfile.py build
stage: build stage: build
test: test:

View File

@ -5,6 +5,16 @@ from .packages import Packages
class User: class User:
"""
Create a user.
Example:
User('app', '/app', getenv('_CONTAINERS_ROOTLESS_UID', 1000)),
_CONTAINERS_ROOTLESS_UID allows to get your UID during build, which happens
in buildah unshare.
"""
def __init__(self, username, home, uid): def __init__(self, username, home, uid):
self.username = username self.username = username
self.home = home self.home = home

32
shlax/container.py Normal file
View File

@ -0,0 +1,32 @@
import os
from .image import Image
class Container:
def __init__(self, build=None, image=None):
self.build = build
self.image = self.build.image
prefix = os.getcwd().split('/')[-1]
repo = self.image.repository.replace('/', '-')
if prefix == repo:
self.name = repo
else:
self.name = '-'.join([prefix, repo])
async def start(self, target):
"""Start the container"""
await target.rexec(
'podman',
'run',
'--name',
self.name,
str(self.image),
)
async def stop(self, target):
"""Start the container"""
await target.rexec('podman', 'stop', self.name)
def __str__(self):
return f'Container(name={self.name}, image={self.image})'

View File

@ -4,18 +4,6 @@ import re
class Image: class Image:
ENV_TAGS = (
# gitlab
'CI_COMMIT_SHORT_SHA',
'CI_COMMIT_REF_NAME',
'CI_COMMIT_TAG',
# CircleCI
'CIRCLE_SHA1',
'CIRCLE_TAG',
'CIRCLE_BRANCH',
# contributions welcome here
)
PATTERN = re.compile( PATTERN = re.compile(
'^((?P<backend>[a-z]*)://)?((?P<registry>[^/]*[.][^/]*)/)?((?P<repository>[^:]+))?(:(?P<tags>.*))?$' # noqa '^((?P<backend>[a-z]*)://)?((?P<registry>[^/]*[.][^/]*)/)?((?P<repository>[^:]+))?(:(?P<tags>.*))?$' # noqa
, re.I , re.I
@ -45,12 +33,6 @@ class Image:
if self.registry == 'docker.io': if self.registry == 'docker.io':
self.format = 'docker' self.format = 'docker'
# figure tags from CI vars
for name in self.ENV_TAGS:
value = os.getenv(name)
if value:
self.tags.append(value)
# filter out tags which resolved to None # filter out tags which resolved to None
self.tags = [t for t in self.tags if t] self.tags = [t for t in self.tags if t]

26
shlax/pod.py Normal file
View File

@ -0,0 +1,26 @@
import cli2
from shlax.targets.base import Target
from shlax.actions.parallel import Parallel
class Pod:
"""Help text"""
def __init__(self, **containers):
self.containers = containers
async def _call(self, target, method, *names):
methods = [
getattr(container, method)
for name, container in self.containers.items()
if not names or name in names
]
await target(Parallel(*methods))
async def build(self, target, *names):
"""Build container images"""
await self._call(target, 'build', *names)
async def start(self, target, *names):
"""Start container images"""
await self._call(target, 'start', *names)

View File

@ -105,16 +105,19 @@ class Buildah(Target):
if name in layers: if name in layers:
self.base = self.image_previous = action_image self.base = self.image_previous = action_image
keep.append(action_image) keep.append(action_image)
self.output.skip(f'Found valid cached layer for {action}') self.output.skip(
f'Found layer for {action}: {action_image.tags[0]}'
)
else: else:
break break
return keep return keep
async def action_image(self, action): async def action_image(self, action):
if self.image_previous: prefix = str(self.image_previous)
prefix = self.image_previous.tags[0] for tag in self.image_previous.tags:
else: if tag.startswith('layer-'):
prefix = self.base prefix = tag
break
if hasattr(action, 'cachekey'): if hasattr(action, 'cachekey'):
action_key = action.cachekey() action_key = action.cachekey()
if asyncio.iscoroutine(action_key): if asyncio.iscoroutine(action_key):
@ -129,6 +132,7 @@ class Buildah(Target):
stop = await super().action(action, reraise) stop = await super().action(action, reraise)
if not stop: if not stop:
action_image = await self.action_image(action) action_image = await self.action_image(action)
self.output.info(f'Commiting {action_image} for {action}')
await self.parent.exec( await self.parent.exec(
'buildah', 'buildah',
'commit', 'commit',
@ -187,6 +191,24 @@ class Buildah(Target):
self.ctr, self.ctr,
)).out )).out
ENV_TAGS = (
# gitlab
'CI_COMMIT_SHORT_SHA',
'CI_COMMIT_REF_NAME',
'CI_COMMIT_TAG',
# CircleCI
'CIRCLE_SHA1',
'CIRCLE_TAG',
'CIRCLE_BRANCH',
# contributions welcome here
)
# figure tags from CI vars
for name in ENV_TAGS:
value = os.getenv(name)
if value:
self.image.tags.append(value)
if image.tags: if image.tags:
tags = [f'{image.repository}:{tag}' for tag in image.tags] tags = [f'{image.repository}:{tag}' for tag in image.tags]
else: else:

View File

@ -7,8 +7,7 @@ from shlax.shortcuts import *
shlax = Container( shlax = Container(
build=Buildah( build=Buildah(
Packages('python38', 'buildah', 'unzip', 'findutils'), Packages('python38', 'buildah', 'unzip', 'findutils', upgrade=False),
User('app', '/app', getenv('_CONTAINERS_ROOTLESS_UID')),
Copy('setup.py', 'shlax', '/app'), Copy('setup.py', 'shlax', '/app'),
Pip('/app'), Pip('/app'),
base='quay.io/podman/stable', base='quay.io/podman/stable',

View File

@ -25,9 +25,3 @@ def test_args(arg, expected):
im = Image(arg) im = Image(arg)
for k, v in expected.items(): for k, v in expected.items():
assert getattr(im, k) == v assert getattr(im, k) == v
def test_args_env():
os.environ['IMAGE_TEST_ARGS_ENV'] = 'foo'
Image.ENV_TAGS = ['IMAGE_TEST_ARGS_ENV']
im = Image('re/po:x,y')
assert im.tags == ['x', 'y', 'foo']