Skip to content

Instantly share code, notes, and snippets.

@rafou
Created June 1, 2022 00:26
Show Gist options
  • Save rafou/7be753f605d4ab894a0628991001b5c7 to your computer and use it in GitHub Desktop.
Save rafou/7be753f605d4ab894a0628991001b5c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import sys
import json
import base64
import boto3
from botocore.client import Config
def main():
with open(sys.argv[1]) as file:
command = file.read()
# the prefix CUSTOM_ENV is added by gitlab, see:
# https://docs.gitlab.com/runner/executors/custom.html#stages
job_environment = {
name[len("CUSTOM_ENV_"):] : value
for name, value in os.environ.items()
if name.startswith("CUSTOM_ENV_")
}
payload = json.dumps({
"command": command,
"environment": job_environment,
})
# we put a high read_timeout in order to let the lambda the time to finish
client = boto3.client('lambda', region_name="<AWS_REGION>", config=Config(read_timeout=1200))
response = client.invoke(
FunctionName='<YOUR_LAMBDA_FUNCTION_ARN>',
Payload=payload,
LogType='Tail'
)
json_result = response['Payload'].read().decode('utf-8')
result = json.loads(json_result)
print(result["output"])
# there was an error during the CI job, we need to tell
# the gitlab runner the job has failed
if result["return_code"] != 0:
# we terminate as recommanded by gitlab documentation
# https://docs.gitlab.com/runner/executors/custom.html#build-failure
exit(os.environ["BUILD_FAILURE_EXIT_CODE"])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment