Skip to content

Create publish2pages #600

Create publish2pages

Create publish2pages #600

Workflow file for this run

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