-
-
Notifications
You must be signed in to change notification settings - Fork 0
158 lines (137 loc) · 6.14 KB
/
Copy pathbackup.yml
File metadata and controls
158 lines (137 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
name: Repository Backup
on:
schedule:
# Run weekly on Sunday at 2 AM UTC
- cron: '0 2 * * 0'
workflow_dispatch:
inputs:
organization:
description: 'GitHub organization to backup'
required: false
default: 'quantecon'
force:
description: 'Force backup even if already exists today'
required: false
default: false
type: boolean
jobs:
backup:
runs-on: ubuntu-latest
permissions:
id-token: write # Required for OIDC authentication
contents: read
issues: write # Required for creating/updating issues
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Recommended: Use OIDC authentication (no long-lived credentials)
# Requires AWS IAM Identity Provider and Role configured for GitHub Actions
# See README.md for setup instructions
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION || 'us-east-1' }}
- name: Run backup
env:
GITHUB_TOKEN: ${{ secrets.REPO_BACKUP_TOKEN || secrets.GITHUB_TOKEN }}
run: |
if [ "${{ github.event.inputs.force }}" = "true" ]; then
python -m src.main --config config.yml --task backup --force
else
python -m src.main --config config.yml --task backup
fi
- name: Generate backup report
id: report
if: always()
env:
GITHUB_TOKEN: ${{ secrets.REPO_BACKUP_TOKEN || secrets.GITHUB_TOKEN }}
run: |
python -m src.main --config config.yml --task report 2>&1 | tee /tmp/report.txt
echo "report<<EOF" >> $GITHUB_OUTPUT
cat /tmp/report.txt >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create or update monthly backup report issue
if: always()
uses: actions/github-script@v7
with:
script: |
const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
const now = new Date();
const date = now.toISOString().split('T')[0];
const dayOfMonth = now.getUTCDate();
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
const monthName = monthNames[now.getUTCMonth()];
const year = now.getUTCFullYear();
const issueTitle = `Backup Reports - ${monthName} ${year}`;
const jobStatus = '${{ job.status }}';
const isFailure = jobStatus === 'failure';
// Check if this is the last week of the month (day >= 25)
const isEndOfMonth = dayOfMonth >= 25;
// Build comment with collapsed report details
let comment = `### ${date} - ${jobStatus === 'success' ? '✅ Success' : '❌ Failed'}\n\n`;
comment += `**Workflow Run:** [View Details](${runUrl})\n\n`;
comment += `<details>\n<summary>📋 Report Output</summary>\n\n`;
comment += '```\n';
comment += `${{ steps.report.outputs.report }}`;
comment += '\n```\n\n';
comment += `</details>\n`;
if (isFailure) {
comment += `\n⚠️ @mmcky - backup failed, please investigate.\n`;
}
// Add end-of-month review reminder
if (isEndOfMonth && !isFailure) {
comment += `\n---\n📅 **End of month reminder:** @mmcky please review this month's backups and close this issue when done.\n`;
}
// Find existing issue for this month
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'backup-report'
});
const existingIssue = issues.data.find(i => i.title === issueTitle);
if (existingIssue) {
// Add comment to existing monthly issue
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: existingIssue.number,
body: comment
});
console.log(`Added comment to issue #${existingIssue.number}: ${issueTitle}`);
} else {
// Create new monthly issue
let issueBody = `# Backup Reports - ${monthName} ${year}\n\n`;
issueBody += `This issue tracks all backup runs for **${monthName} ${year}**.\n\n`;
issueBody += `## Summary\n\n`;
issueBody += `- **Schedule:** Weekly on Sunday at 2 AM UTC\n`;
issueBody += `- **Organization:** QuantEcon\n`;
issueBody += `- **Storage:** AWS S3\n\n`;
issueBody += `Each backup run will be posted as a comment below with a collapsed report.\n`;
const newIssue = await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
body: issueBody,
labels: ['backup-report']
});
// Add first run as comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: newIssue.data.number,
body: comment
});
console.log(`Created issue #${newIssue.data.number}: ${issueTitle}`);
}