Skip to content

Instantly share code, notes, and snippets.

@rafou
Last active June 9, 2022 16:43
Show Gist options
  • Save rafou/7b8ef67941ded80fc57f7e6fe3a67379 to your computer and use it in GitHub Desktop.
Save rafou/7b8ef67941ded80fc57f7e6fe3a67379 to your computer and use it in GitHub Desktop.
import os
import subprocess
import stat
import shutil
from git import Repo
def main(event, lambda_context):
environment_variables = event["environment"]
os.environ.update(environment_variables)
shutil.rmtree("/tmp/builds", ignore_errors=True)
os.mkdir("/tmp/builds")
shutil.rmtree("/tmp/cache", ignore_errors=True)
os.mkdir("/tmp/cache")
print("start cloning")
Repo.clone_from(
(
"https://gitlab-ci-token:"
+ os.environ["CI_JOB_TOKEN"]
+ "@"
+ "<YOUR_GITLAB_URL>"
+ os.environ["CI_PROJECT_PATH"]
+ ".git"
),
"/tmp/builds/" + os.environ["CI_PROJECT_PATH"],
# 99% of the time we only need a shallow clone
depth=1,
single_branch=True,
branch=os.environ["CI_COMMIT_REF_NAME"],
)
print("start dumping script")
with open("/tmp/script.sh", "w") as script:
script.write(event["command"])
st = os.stat("/tmp/script.sh")
os.chmod("/tmp/script.sh", st.st_mode | stat.S_IEXEC)
# the copy is just to be extra-safe
copy_env_variables = os.environ.copy()
# for all the binary added by the requirements.txt
lambda_root_dir = os.environ["LAMBDA_TASK_ROOT"]
# we add lambda_root_dir for git-lfs
# and lambda_root_dir/bin for binary installed by pip
copy_env_variables["PATH"] = (
copy_env_variables["PATH"] + f":{lambda_root_dir}:{lambda_root_dir}/bin"
)
copy_env_variables["PYTHONPATH"] = (
copy_env_variables.get("PYTHONPATH", "") + ":" + lambda_root_dir
)
print("start executing script")
proc = subprocess.Popen(
"/tmp/script.sh",
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=copy_env_variables,
)
(stdout, _) = proc.communicate()
print("all done")
return {
"return_code": proc.returncode,
"output": stdout.decode("utf-8"),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment