content="" SPACE="" currentDate=${{ parameters.currentDate }} version=${{…
- User
content="" SPACE="" currentDate={{ parameters.version }} if [[ "{{ parameters.builtImage }} shaId={{ parameters.builtImage }} | cut -d '@' -f 2) fi
- Assistant
content="" SPACE="" currentDate={{ parameters.version }} if [[ "{{ parameters.builtImage }} shaId={{ parameters.builtImage }} | cut -d '@' -f 2) fi
FILE=smtp.py
if [ ! -f "$FILE" ]; then
cat <<EOF > smtp.py
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import json
version="$version"
content=""
if "${{ parameters.isPreviousStageSucceeded }}" == "Succeeded":
content="<div><h4>Pipeline Executed Successfully for below images:</h4>"
content+="""<div>Image Name: ${{ parameters.imageName }}</div>
<label>Tag:<label>
<ul style="margin-top: 0px;margin-bottom: 0px;">
<li>{}-$currentDate.${{ parameters.buildVersion }}</li>
<li>{}-$currentDate</li>
<li>{}</li>
<ul style="margin-top: 0px;margin-bottom: 0px;">
<div><h5>SHA_ID - $shaId</h5></div>
</ul>
<br><div>Pipeline Link: <span style="background-color: #E6E8DF">$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId) </span><br><br>""".format(version,version,version)
else:
content="""<div><h4>Pipeline failed, please find below URL:</h4>
<div>$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)</div>"""
content+="</div>"
print(content)
toAddrStr='${{ parameters.emailList }}'
toAddrStr=toAddrStr.split(",")
toCC='$(cc)'
toCC=toCC.split(",")
tolist="Successfully sent email to ${{ parameters.emailList }}"
fromaddr = '$(from)'
toaddr = toAddrStr + toCC
ccaddr = '$(cc)'
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = '${{ parameters.emailList }}'
msg['Cc'] = '$(cc)'
msg['Subject'] = "Pipeline Status of ${{ parameters.platform }}-${{ parameters.version }}"
msg.attach(MIMEText(content, 'html'))
try:
server = smtplib.SMTP('$(smtp_server_host)', $(smtp_server_port))
server.starttls()
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print(tolist)
except Exception, e:
print "Error: unable to send email"
print e
EOF
fi
python smtp.py
if [[ "${{ parameters.isPreviousStageSucceeded }}" == "Succeeded" ]];
then
docker image rm ${{ parameters.builtImage }}
fi
This script corresponds to the execution of a pipeline, particularly for setting up a Docker container confirmed by the success of a previous stage. It also includes a step for sending out notifications via email, depending on the success or failure of that stage. Below is a structured breakdown and explanation of the script.
Environment Variables Declaration:
content=""
SPACE=""
currentDate=${{ parameters.currentDate }}
version=${{ parameters.version }}
Pull Docker Image if Previous Stage Succeeded:
if [[ "${{ parameters.isPreviousStageSucceeded }}" == "Succeeded" ]];
then
docker pull ${{ parameters.builtImage }}
shaId=$(docker inspect --format='{{index .RepoDigests 0}}' ${{ parameters.builtImage }} | cut -d '@' -f 2)
fi
SMTP and Email Configuration:
smtp.py script that will handle the sending of emails.FILE=smtp.py
if [ ! -f "$FILE" ]; then
cat <<EOF > smtp.py
...
EOF
fi
python smtp.py
Email Sending Logic in smtp.py:
Set up imports and initial configurations:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import json
version="$version"
content=""
Create the content of the email based on the success or failure of the previous stage:
if "${{ parameters.isPreviousStageSucceeded }}" == "Succeeded":
content="<div><h4>Pipeline Executed Successfully for below images:</h4>"
content+="""<div>Image Name: ${{ parameters.imageName }}</div>
<label>Tag:<label>
<ul style="margin-top: 0px;margin-bottom: 0px;">
<li>{}-$currentDate.${{ parameters.buildVersion }}</li>
<li>{}-$currentDate</li>
<li>{}</li>
<ul style="margin-top: 0px;margin-bottom: 0px;">
<div><h5>SHA_ID - $shaId</h5></div>
</ul>
<br><div>Pipeline Link: <span style="background-color: #E6E8DF">$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId) </span><br><br>""".format(version,version,version)
else:
content="""<div><h4>Pipeline failed, please find below URL:</h4>
<div>$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)</div>"""
content+="</div>"
print(content)
Define email recipients:
toAddrStr='${{ parameters.emailList }}'
toAddrStr=toAddrStr.split(",")
toCC='$(cc)'
toCC=toCC.split(",")
tolist="Successfully sent email to ${{ parameters.emailList }}"
fromaddr = '$(from)'
toaddr = toAddrStr + toCC
ccaddr = '$(cc)'
Setup email structure:
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = '${{ parameters.emailList }}'
msg['Cc'] = '$(cc)'
msg['Subject'] = "Pipeline Status of ${{ parameters.platform }}-${{ parameters.version }}"
msg.attach(MIMEText(content, 'html'))
Attempt to send the email and handle exceptions:
try:
server = smtplib.SMTP('$(smtp_server_host)', $(smtp_server_port))
server.starttls()
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print(tolist)
except Exception as e:
print("Error: unable to send email")
print(e)
Clean Up Docker Image if Previous Stage Succeeded:
if [[ "${{ parameters.isPreviousStageSucceeded }}" == "Succeeded" ]];
then
docker image rm ${{ parameters.builtImage }}
fi
This script automates a part of pipeline maintenance, ensuring the relevant stakeholders are informed about the pipeline status by email with detailed information about the build status and artifacts.
Not yet included in a published release.