Skip to content

Commit a135d06

Browse files
committed
feat(release): add run trigger
1 parent 0b3be32 commit a135d06

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

rootfs/api/models/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,20 @@ def pipeline(self, release, force_deploy=False, rollback_on_failure=True):
222222
prefix = f"[pipeline] release {release.version_name}"
223223
try:
224224
self.log(f"{prefix} starts running...")
225+
procfile_types = release.diff_procfile_types()
225226
if release.build.dryccfile:
226-
if 'run' in release.build.dryccfile:
227+
if 'run' in release.build.dryccfile and release.get_run_trigger():
227228
self.log(f"{prefix} starts running pipeline.run")
228229
job_name = self.run(
229230
self.owner, release.get_run_image(), command=release.get_run_command(),
230-
args=release.get_run_args(), timeout=settings.DRYCC_PILELINE_RUN_TIMEOUT,
231-
expires=settings.DRYCC_PILELINE_RUN_TIMEOUT)
231+
args=release.get_run_args(), timeout=release.get_run_timeout(),
232+
expires=release.get_run_timeout())
232233
state, labels = 'initializing', {'job-name': job_name}
233234
for count, state in enumerate(self.scheduler().pod.watch(
234235
self.id, labels, settings.DRYCC_PILELINE_RUN_TIMEOUT)):
235236
self.log(f"{prefix} waiting for pipeline.run: {state} * {count}")
236237
if state != 'down':
237238
raise DryccException(f'pipeline run state error: {state}')
238-
procfile_types = release.diff_procfile_types()
239239
if procfile_types is None or len(procfile_types) > 0:
240240
self.log(f"{prefix} starts running pipeline.deploy")
241241
self.deploy(release, procfile_types, force_deploy, rollback_on_failure)

rootfs/api/models/release.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ def get_run_command(self):
7878
"""
7979
return self.build.dryccfile.get('run', {}).get('command', [])
8080

81+
def get_run_timeout(self):
82+
return int(self.build.dryccfile.get('run', {}).get(
83+
'timeout', settings.DRYCC_PILELINE_RUN_TIMEOUT))
84+
85+
def get_run_trigger(self):
86+
if 'ptypes' not in self.build.dryccfile.get('run', {}).get('when', {}):
87+
return True
88+
procfile_types = self.diff_procfile_types()
89+
if procfile_types is None:
90+
return True
91+
for procfile_type in procfile_types:
92+
if procfile_type in self.build.dryccfile['run']['when']['ptypes']:
93+
return True
94+
return False
95+
8196
def get_deploy_image(self, container_type):
8297
"""
8398
In the deploy phase of dryccfile

rootfs/api/tests/test_release.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,56 @@ def test_diff_procfile_types(self, mock_requests):
892892
prefix = f"[{release.app.id}]: [pipeline] release v{release.version}"
893893
exp_msg = f"{prefix} no changes, skip executing pipeline.deploy"
894894
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
895+
896+
@override_settings(DRYCC_PILELINE_RUN_TIMEOUT=600)
897+
def test_run(self, mock_requests):
898+
app_id = self.create_app()
899+
app = App.objects.get(id=app_id)
900+
user = User.objects.get(username='autotest')
901+
dryccfile = {
902+
"run": {
903+
"args": ["sleep", "60s"],
904+
"image": "registry.drycc.cc/drycc/base:bookworm"
905+
},
906+
"deploy": {
907+
"web": {
908+
"image": "registry.drycc.cc/drycc/python-dev",
909+
"args": ["python", "-m", "http.server", "5000"]
910+
},
911+
"task": {
912+
"image": "docker.io/library/nginx:mainline-bookworm-perl",
913+
"command": ["sleep"],
914+
"args": ["infinity"]
915+
}
916+
}
917+
}
918+
build = Build.objects.create(
919+
owner=user, app=app, image="qwerty", procfile={},
920+
sha='african-swallow', dockerfile={}, dryccfile=dryccfile)
921+
release = Release.objects.create(
922+
version=2, owner=user, app=app, config=app.config_set.latest(),
923+
build=build, state="succeed")
924+
self.assertEqual(release.get_run_timeout(), 600)
925+
self.assertEqual(release.get_run_trigger(), True)
926+
# change timeout
927+
dryccfile['run']['when'] = {'ptypes': ['web']}
928+
dryccfile['run']['timeout'] = 3600
929+
dryccfile['deploy']['web']['image'] = 'registry.drycc.cc/drycc/python-dev:latest'
930+
build = Build.objects.create(
931+
owner=user, app=app, image="qwerty", procfile={},
932+
sha='african-swallow', dockerfile={}, dryccfile=dryccfile)
933+
release = Release.objects.create(
934+
version=3, owner=user, app=app, config=app.config_set.latest(),
935+
build=build, state="succeed")
936+
self.assertEqual(release.get_run_timeout(), 3600)
937+
self.assertEqual(release.get_run_trigger(), True)
938+
# change task image
939+
dryccfile['deploy']['task']['image'] = 'docker.io/library/nginx:latest'
940+
build = Build.objects.create(
941+
owner=user, app=app, image="qwerty", procfile={},
942+
sha='african-swallow', dockerfile={}, dryccfile=dryccfile)
943+
release = Release.objects.create(
944+
version=4, owner=user, app=app, config=app.config_set.latest(),
945+
build=build, state="succeed")
946+
self.assertEqual(release.get_run_timeout(), 3600)
947+
self.assertEqual(release.get_run_trigger(), False)

0 commit comments

Comments
 (0)