我正在通过代码管道部署我的应用程序。在此期间,我调用了一个 Lambda 函数来通知 Slack 上的用户。我想在其中添加提交名称。知道我如何获得这些信息。
法典:
import json
import http.client
import urllib.parse
from time import strftime
import boto3
import datetime
import dateutil.tz
WEBHOOK_URL = "https://hooks.slack.com/services/TOKEN"
NOTIFICATION_CHANNEL = "CHANNEL-NAME"
eastern = dateutil.tz.gettz('Europe/Berlin')
final_time = datetime.datetime.now(tz=eastern).strftime("%a, %d %b %Y %I:%M:%S %p %Z")
def lambda_handler(event, context):
send_message("Deployment for https://ourdomain.com has started at %s"%final_time,":ghost:","webhookbot")
job_id = event['CodePipeline.job']['id']
put_job_success(job_id,"Code executed")
def put_job_success(job, message):
print('Putting job success')
print(message)
code_pipeline = boto3.client('codepipeline')
code_pipeline.put_job_success_result(jobId=job)
def send_message(message, icon, username):
payload = get_payload(username, icon, message)
data = get_encoded_data_object(payload)
headers = get_headers()
response = send_post_request(data, headers)
def get_payload(username, icon, message):
payload_dict = {
'channel': NOTIFICATION_CHANNEL,
'username': username,
'icon_emoji': icon,
'text': message,
}
payload = json.dumps(payload_dict)
return payload
def get_encoded_data_object(payload):
values = {'payload': payload}
str_values = {}
for k, v in values.items():
str_values[k] = v.encode('utf-8')
data = urllib.parse.urlencode(str_values)
return data
def get_headers():
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
return headers
def send_post_request(body, headers):
https_connection = get_https_connection_with_slack()
https_connection.request('POST', WEBHOOK_URL, body, headers)
response = https_connection.getresponse()
return response
def get_https_connection_with_slack():
h = http.client.HTTPSConnection('hooks.slack.com')
return h
您可以在构建阶段将其写入文件(我假设您将在之前执行(,并将构建阶段的输出构件作为输入构件传递给 lambda。然后在 lambda 中,您可以访问其输入工件(例如,使用 s3 客户端(并读取提交。
在这里,您可以找到包含输入工件的事件示例: https://medium.com/@codershunshun/how-to-invoke-aws-lambda-in-codepipeline-d7c77457af95