-
Notifications
You must be signed in to change notification settings - Fork 4
155 lines (131 loc) · 5.62 KB
/
gen_CV.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
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
name: Generate CV file
on:
push:
branches:
- '*'
tags:
- '*'
jobs:
generate_cv:
runs-on: ubuntu-latest
permissions:
actions: write
checks: write
contents: write
deployments: write
id-token: write
issues: write
discussions: write
packages: write
pages: write
pull-requests: write
repository-projects: write
security-events: write
statuses: write
env:
API_KEY: ${{ secrets.API_KEY }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Check out the repository
uses: actions/checkout@v2
- name: Is run necessary
run: |
# Create the CVs directory if it does not exist
mkdir -p CVs
# Check if the script should be run based on file modification date and name
file_path_pattern="CVs/CMIP6Plus_CV_$(git rev-parse --abbrev-ref HEAD).json"
latest_commit_date=$(git log -n 1 --format=%cd)
# Check if the file exists
if [ -e "$file_path_pattern" ]; then
last_modification_date=$(git log -1 --format=%ad -- "$file_path_pattern")
# Compare the dates
if [ "$latest_commit_date" \< "$last_modification_date" ]; then
echo "The latest commit is older than the last modification date for the file."
echo "EXIT=true" >> $GITHUB_ENV
else
echo "The latest commit is not older than the last modification date for the file."
fi
else
echo "File $file_path_pattern not found."
# Create an empty CV file or perform any necessary initialization
touch "$file_path_pattern"
# echo "EXIT=false" >> $GITHUB_ENV
fi
- name: Set up Git
run: |
# Configure Git settings
git config user.email "[email protected]"
git config user.name "CMIP-IPO: Automated GitHub Action"
git config credential.helper store
git config --global user.email "[email protected]"
git config --global user.name "CMIP-IPO GitHub Action"
git config --global push.default current
GH_TOKEN=${{ secrets.GITHUB_TOKEN }}
echo "GH_TOKEN=${GH_TOKEN}" >> $GITHUB_ENV
echo "GITHUB_TOKEN=${GH_TOKEN}" >> $GITHUB_ENV
git pull
- name: Set GIT repo environment variables
run: |
# Set environment variables related to Git repository
echo "COMMIT_HASH=${GITHUB_SHA}" >> $GITHUB_ENV
TAG=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r .tag_name)
echo "TAG_VERSION=${TAG}" >> $GITHUB_ENV
COMMIT_DATE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}" | jq -r '.commit.author.date')
echo "COMMIT_DATE=${COMMIT_DATE}" >> $GITHUB_ENV
- name: Display GIT environment variables
run: |
# Display Git environment variables for debugging
echo "Commit Hash: $COMMIT_HASH"
echo "Tag Version: $TAG_VERSION"
echo "Commit Date: $COMMIT_DATE"
echo "${{github.repository}}"
- name: Print latest commit SHA
id: commit_sha
run: |
# Print the latest commit SHA
echo "commit=$(git rev-parse HEAD)" >> $GITHUB_ENV
echo "CVs/CMIP6Plus_CV_$(git rev-parse --abbrev-ref HEAD).json"
- name: Run Python Check
id: 'run-python-script'
run: |
# Run Python script if necessary
if [ ! "$EXIT" ]; then
file_path_pattern="$(git rev-parse --abbrev-ref HEAD)"
python create_cv.py -c "${COMMIT_HASH}" -b "$file_path_pattern" -t "${TAG_VERSION}" -d "${COMMIT_DATE}" -a "${GH_TOKEN}"
fi
working-directory: .github/libs
env:
PYTHON_SCRIPT_OUTPUT: ${{ steps.run-python-script.outputs.stdout }}
PYTHON_SCRIPT_ERROR: ${{ steps.run-python-script.outputs.stderr }}
continue-on-error: false
- name: Write new CV
run: |
# Write new CV if necessary
if [ -z "$ACT" ] && [ ! "$EXIT" ]; then
# Display the current Git branch
echo "Branch: $(git rev-parse --abbrev-ref HEAD)"
# Define file paths
file="CVs/CMIP6Plus_CV$(git rev-parse --abbrev-ref HEAD).json"
altfile="CVs/CMIP6Plus_CV.json"
# Check if either file or altfile exists
if [ -e $file ] || [ -e $altfile ]; then
# Choose the existing file (either file or altfile)
existing_file=$([ -e $file ] && echo $file || echo $altfile)
# Get the current date and time
current_datetime=$(date +"%Y-%m-%d %H:%M")
# Add all changes to the Git repository
git add -A
# Add the specific file to the Git repository
git add -f $existing_file
# Commit the changes with a message indicating the file creation
git commit -m "Automatically generated $existing_file: $current_datetime"
# Push the changes to the remote repository for the current branch
git push origin $(git rev-parse --abbrev-ref HEAD)
else
echo "Neither $file nor $altfile found. No action taken."
fi
else
# Skip this step in 'act' environment
echo "Skipping this step in 'act' environment."
fi
continue-on-error: true