Skip to content

Instantly share code, notes, and snippets.

@russau
Created July 14, 2021 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russau/8dab81cbb54d10e7a57a4d162f152d21 to your computer and use it in GitHub Desktop.
Save russau/8dab81cbb54d10e7a57a4d162f152d21 to your computer and use it in GitHub Desktop.
import boto3
import json
import sys
# Create SQS client
sqs = boto3.client('sqs')
queue_url = 'https://sqs.ap-southeast-2.amazonaws.com/414514743156/pipeline-events'
COLOR = {
"HEADER": "\033[95m",
"BLUE": "\033[94m",
"GREEN": "\033[92m",
"RED": "\033[91m",
"ENDC": "\033[0m",
}
while True:
# Long poll for message on provided SQS queue
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=1,
MessageAttributeNames=[
'All'
],
WaitTimeSeconds=20
)
if 'Messages' in response:
try:
rh = response['Messages'][0]['ReceiptHandle']
body = json.loads(response['Messages'][0]['Body'])
pipeline = body["detail"]['pipeline']
detail_type = body["detail-type"]
time = body["time"]
stage = body["detail"]['stage'] if "stage" in body["detail"] else None
action = body["detail"]['action'] if "action" in body["detail"] else None
state = body["detail"]['state']
except:
print("Unexpected error:", sys.exc_info()[0])
print(json.dumps(response, indent=4))
raise
print(f'{COLOR["HEADER"]} {detail_type} {COLOR["ENDC"]}')
print(f'{time} : {COLOR["GREEN"]} Pipeline:{pipeline} Stage:{stage} Action:{action} State:{state} {COLOR["ENDC"]}')
#print(json.dumps(body, indent=4))
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=rh
)
# {
# "version": "0",
# "id": "d5ec3e41-2631-67bf-9b0a-a4f70c3a5e72",
# "detail-type": "CodePipeline Action Execution State Change",
# "source": "aws.codepipeline",
# "account": "414514743156",
# "time": "2021-07-14T02:09:39Z",
# "region": "ap-southeast-2",
# "resources": [
# "arn:aws:codepipeline:ap-southeast-2:414514743156:MyPipeline"
# ],
# "detail": {
# "pipeline": "MyPipeline",
# "execution-id": "63868e09-47af-4a2a-bd2f-b163de212eda",
# "stage": "Deploy",
# "action": "Deploy",
# "input-artifacts": [
# {
# "name": "SourceArtifact",
# "s3location": {
# "bucket": "c2-pipeline-codepipelineartifactstores3bucket-knnx7tb8ob0i",
# "key": "MyPipeline/SourceArti/4lUeQ2l"
# }
# }
# ],
# "state": "STARTED",
# "region": "ap-southeast-2",
# "type": {
# "owner": "AWS",
# "provider": "CodeDeploy",
# "category": "Deploy",
# "version": "1"
# },
# "version": 1
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment