GitHub Actions Weather Bot #280
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#2024/06/15 07:03:20 GMT+08:00 | |
name: 'GitHub Actions Weather Bot' | |
on: | |
push: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 21 * * *' | |
jobs: | |
bot: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Checkout codes' | |
uses: actions/checkout@v1 | |
- name: 'Get IP Address' | |
run: | | |
set +x | |
echo "IP_ADDRESS=$(curl -s https://api64.ipify.org?format=json | jq -r '.ip')" >> $GITHUB_ENV | |
set -x | |
- name: 'Get Weather' | |
run: bash ./weather.sh | |
timeout-minutes: 10 # 如果执行时间超过十分钟,则取消执行 | |
- name: 'Get Date' | |
run: echo "REPORT_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T')" >> $GITHUB_ENV | |
- name: '将 IP 地址写入到 result.html' | |
run: | | |
# 如果 IP 地址存在 | |
if [ -n "${IP_ADDRESS}" ]; then | |
# 将 IP 地址追加写入到 result.html 文件 | |
echo "Github ip: ${{ env.IP_ADDRESS }}" >> result.html | |
else | |
# 如果 IP 地址不存在,则写入一条相应的消息 | |
echo "IP获取失败。" >> result.html | |
fi | |
- name: 'Send mail' | |
uses: dawidd6/action-send-mail@master | |
with: | |
server_address: smtp.office365.com | |
server_port: 587 | |
username: ${{ secrets.MAIL_USERNAME }} | |
password: ${{ secrets.MAIL_PASSWORD }} | |
subject: 每日天气 (${{ env.REPORT_DATE }}) | |
body: file://result.html | |
to: ${{ secrets.MAIL_TO }} | |
from: 气象快报 | |
content_type: text/html |