Skip to content

Commit

Permalink
Aggiornamento automatico README
Browse files Browse the repository at this point in the history
Workflow per GitHub Actions che ogni mese aggiorna il file README.md con i prezzi PUN del mese precedente.
  • Loading branch information
virtualdj committed Dec 26, 2023
1 parent ecc993b commit 4532044
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
67 changes: 64 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:
branches: [ "main" ]
schedule:
- cron: '0 1 1 * *'
- cron: '0 5 1 * *'

permissions:
contents: read
Expand All @@ -15,6 +15,8 @@ jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: write

steps:
- uses: actions/checkout@v3
Expand All @@ -26,6 +28,65 @@ jobs:
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test application output
# Run application
- name: Run application
id: run_script
run: |
ANNO=$(( $(date +'%m') < 2 ? $(date +'%Y') - 1 : $(date +'%Y') ))
echo "Running app in $(date +'%m/%Y') for year $ANNO."
{
echo "result<<EOF"
python pun-fasce.py $ANNO
echo EOF
} >> $GITHUB_ENV
# Show output (using environment variable to avoid quoting)
- name: Show application output
run: echo -e "$RES"
shell: bash
env:
RES: ${{ env.result }}

# Environment variable character escaping
- name: Character escaping
id: run_escaping
run: |
RES="${RES//$'\n'/\\n}" # Escape new lines
RES="${RES//$'\t'/\\t}" # Escape tabs
RES=${RES//\//\\\/} # Escape slashes
echo "Escaped RES (env.result) = |$RES|"
echo "result=$RES" >> $GITHUB_ENV
shell: bash
env:
RES: ${{ env.result }}

# Update README with content of environment variable
- name: Update README.md
run: |
python pun-fasce.py
# Replace anything after line with "Il risultato"
sed -i -n -e '/Il risultato/{p;a\' -e '\n```text\n${{ env.result }}\n```' -e '}' -e '/Il risultato/,$!p' README.md
# Push changes to repository, if any
- name: Check for changes
id: check_changes
run: |
git diff --exit-code > /dev/null && echo 'changed=false' >> $GITHUB_ENV || echo 'changed=true' >> $GITHUB_ENV
- name: Pull-request
uses: repo-sync/pull-request@v2
if: env.changed == 'true'
with:
destination_branch: "main"
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Commit
if: env.changed == 'true'
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add .
git commit -m "Aggiornamento dati mensili README" -a
- name: Push changes
if: env.changed == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ I valori vengono scaricati dal sito [MercatoElettrico.org](https://www.mercatoel

`python3 ./pun-fasce.py <anno>`

L'`anno` è opzionale e in quel caso si intende l'anno corrente.
L'`anno` è opzionale e in quel caso si intende l'anno corrente.
Il risultato è simile a questo:

```text
Expand Down

0 comments on commit 4532044

Please sign in to comment.