forked from MShawon/github-clone-count-badge
94 lines (80 loc) · 3.57 KB
/
myrepo-gh-clone-stats.yml
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
name: GitHub clones counter for 14 days at every 8 hours and clones accumulator
on:
schedule:
- cron: "0 */8 * * *"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
traffic-clones:
runs-on: ubuntu-latest
env:
traffic_clones_dir: traffic/clones
steps:
# step is required to workaround variable expansion in variables
- name: declare dependent variables
run: |
echo "traffic_clones_json=$traffic_clones_dir/latest.json" >> $GITHUB_ENV
- name: allocate directories
run: |
mkdir $GITHUB_WORKSPACE/gh-workflow $GITHUB_WORKSPACE/gh-stats
echo "WORKFLOW_TEMPDIR=$(mktemp -d)" >> $GITHUB_ENV
- name: allocate variables
run: |
echo "write_error_to_file=$WORKFLOW_TEMPDIR/err.log" >> $GITHUB_ENV
echo "write_warning_to_file=$WORKFLOW_TEMPDIR/warn.log" >> $GITHUB_ENV
- name: checkout gh-workflow
uses: actions/checkout@v2
with:
repository: '{{REPO_OWNER}}/gh-workflow'
path: gh-workflow
- name: checkout gh-stats
uses: actions/checkout@v2
with:
repository: '{{REPO_OWNER}}/{{REPO}}--gh-stats'
path: gh-stats
- name: request traffic clones json
run: |
curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/{{REPO_OWNER}}/{{REPO}}/traffic/clones \
> "$GITHUB_WORKSPACE/gh-stats/$traffic_clones_json"
- name: accumulate traffic clones
id: accum-traffic-clones
shell: bash
continue-on-error: true
run: |
cd $GITHUB_WORKSPACE/gh-stats
cat "$traffic_clones_json"
chmod ug+x $GITHUB_WORKSPACE/gh-workflow/bash/github/accum-traffic-clones.sh
$GITHUB_WORKSPACE/gh-workflow/bash/github/accum-traffic-clones.sh
# caution: no operation after that line if non zero exit code happens above
- name: allocate error and warning variables
if: steps.accum-traffic-clones.outcome != 'success'
run: |
# allocate variables here to make it visible in the next step
[[ -f "$write_error_to_file" ]] && echo "error_string<<EOF" >> $GITHUB_ENV
[[ -f "$write_error_to_file" ]] && cat "$write_error_to_file" >> $GITHUB_ENV
[[ -f "$write_error_to_file" ]] && echo "EOF" >> $GITHUB_ENV
[[ -f "$write_warning_to_file" ]] && echo "warning_string<<EOF" >> $GITHUB_ENV
[[ -f "$write_warning_to_file" ]] && cat "$write_warning_to_file" >> $GITHUB_ENV
[[ -f "$write_warning_to_file" ]] && echo "EOF" >> $GITHUB_ENV
exit 0
- name: print errors and warnings
if: steps.accum-traffic-clones.outcome != 'success'
run: |
[[ -n "$error_string" ]] && echo "::error ::$error_string"
[[ -n "$warning_string" ]] && echo "::warning ::$warning_string"
exit 255
- name: commit gh-stats
run: |
cd $GITHUB_WORKSPACE/gh-stats
git add .
git config --global user.name "GitHub Action"
git config --global user.email "action@github.com"
git commit -m "Automated traffic/clones update"
- name: push gh-stats
uses: ad-m/github-push-action@master
with:
repository: '{{REPO_OWNER}}/{{REPO}}--gh-stats'
directory: gh-stats
github_token: ${{ secrets.SECRET_TOKEN }}