Skip to content

Instantly share code, notes, and snippets.

@fabysdev
Created June 25, 2019 07:25
Show Gist options
  • Save fabysdev/6bea9ae7661ebda76856a11fea293d96 to your computer and use it in GitHub Desktop.
Save fabysdev/6bea9ae7661ebda76856a11fea293d96 to your computer and use it in GitHub Desktop.
import subprocess
import os
import sys
# pipelines definitions --------------------------------------------------
PIPELINES = {
"docker/ci-base/": "BUILD_CIBASE",
"docker/go-develop/": "BUILD_GODEVELOP",
"docker/js-develop/": "BUILD_JSDEVELOP",
"projects/landing/": "BUILD_LANDING"
}
# -----------------------------------------------------------------------
def main():
# resolve pipelines which need to run
run_pipelines = {}
commit_sha = os.getenv("CI_COMMIT_SHA")
commit_before_sha = os.getenv("CI_COMMIT_BEFORE_SHA")
commit_ref_name = os.getenv("CI_COMMIT_REF_NAME")
print("CI_COMMIT_REF_NAME:", commit_ref_name)
print("CI_COMMIT_SHA:", commit_sha)
print("CI_COMMIT_BEFORE_SHA:", commit_before_sha)
files = []
if commit_before_sha == "0000000000000000000000000000000000000000":
# skip initial master push
if commit_ref_name == "master":
sys.exit()
build_tag = os.getenv("CI_BUILD_TAG")
if build_tag:
# tag build ... message contains projects too build (e.g. [BUILD_FRONTEND])
tag_message = subprocess.check_output(
["git", "tag", "-n10", build_tag]).decode("utf-8")
for pipline_path in PIPELINES:
pipeline = PIPELINES[pipline_path]
if "[%s]" % pipeline in tag_message:
run_pipelines[pipeline] = True
else:
# initial branch build ... diff with master branch
res = subprocess.check_output(
["git", "diff", "--name-only", "origin/master"])
files = res.decode("utf-8").split("\n")
else:
res = subprocess.check_output(
["git", "diff", "--name-only", "%s..%s" % (commit_sha, commit_before_sha)])
files = res.decode("utf-8").split("\n")
for file in files:
for pipline_path in PIPELINES:
if file.startswith(pipline_path):
run_pipelines[PIPELINES[pipline_path]] = True
# trigger pipelines
err = False
for pipline in run_pipelines:
try:
print("TRIGGER:", pipline)
subprocess.check_output(
["curl", "-X", "POST", "-F", "token=%s" % (os.getenv("TRIGGER_TOKEN")), "-F", "ref=%s" % (commit_ref_name), "-F", "variables[%s]=true" % (pipline), "-F", "variables[SKIP_BOOTSTRAP]=true", "%s/projects/%s/trigger/pipeline" % (os.getenv("CI_API_V4_URL"), os.getenv("CI_PROJECT_ID"))])
except:
print(sys.exc_info())
err = True
if err:
sys.exit(1)
if (__name__ == "__main__"):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment