Skip to content

Run Python Script #1951

Run Python Script

Run Python Script #1951

Workflow file for this run

name: Run Python Script
on:
schedule:
- cron: '*/20 * * * *' # Every 20 minutes
workflow_dispatch:
jobs:
run-script:
runs-on: ubuntu-22.04
environment: Test
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install cryptography
pip install dnspython
- name: Create Python scripts
run: |
cat > decrypt_file.py << 'EOL'
from cryptography.fernet import Fernet
import os
def decrypt_file():
key = os.environ['ENCRYPTION_KEY'].encode()
f = Fernet(key)
with open('content_file.txt.encrypted', 'rb') as encrypted_file:
encrypted_data = encrypted_file.read()
decrypted_data = f.decrypt(encrypted_data)
with open('content_file.txt', 'wb') as decrypted_file:
decrypted_file.write(decrypted_data)
if __name__ == "__main__":
decrypt_file()
EOL
cat > encrypt_file.py << 'EOL'
from cryptography.fernet import Fernet
import os
def encrypt_file():
key = os.environ['ENCRYPTION_KEY'].encode()
f = Fernet(key)
with open('content_file.txt', 'rb') as file:
data = file.read()
encrypted_data = f.encrypt(data)
with open('content_file.txt.encrypted', 'wb') as encrypted_file:
encrypted_file.write(encrypted_data)
if __name__ == "__main__":
encrypt_file()
EOL
- name: Decrypt file if exists
env:
ENCRYPTION_KEY: ${{ secrets.ENCRYPTION_KEY }}
run: |
if [ -f "content_file.txt.encrypted" ]; then
python decrypt_file.py
fi
- name: Run script
env:
EMAIL: ${{ secrets.EMAIL }}
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
LOGIN_USERNAME: ${{ secrets.LOGIN_USERNAME }}
LOGIN_PASSWORD: ${{ secrets.LOGIN_PASSWORD }}
run: python script.py
- name: Encrypt and save file
env:
ENCRYPTION_KEY: ${{ secrets.ENCRYPTION_KEY }}
run: python encrypt_file.py
- name: Commit encrypted file
run: |
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
git add content_file.txt.encrypted
git diff --quiet && git diff --staged --quiet || (git commit -m "Update encrypted content [skip ci]" && git push)