I'm facing an unexpected issue with GitHub Actions where I'm unable to retrieve the values of secrets within my Python script. Despite correctly defining secrets in my GitHub repository and attempting to access them using os.environ.get("SECRET_NAME"), I'm only getting None in return. Here's how I'm setting environment variables in my GitHub Actions workflow:
env: MONGO_USER: ${{ secrets.MONGO_USER }} And here's my Python snippet:
user = os.environ.get("MONGO_USER") password = os.environ.get("MONGO_PASSWORD") host = os.environ.get("MONGO_HOST") dbname = os.environ.get("MONGO_DBNAME") # Supposant que collection_name est utilisé ailleurs dans votre code. collection_name = os.environ.get("MONGO_COLLECTION_NAME") print("MONGO_USER:", os.environ.get("MONGO_USER")) uri = f"mongodb+srv://{user}:{password}@{host}/{dbname}?retryWrites=true&w=majority&appName={appName}" client = MongoClient(uri) main.yml
build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.11' - name: Install dependencies run: pip install pymongo - name: Run Python script env: MONGO_USER: ${{ secrets.MONGO_USER }} run: python ./path/to/your_script.py data.py
import os print("MONGO_USER:", os.environ.get("MONGO_USER")) My variables


Traceback (most recent call last): File "/home/runner/work/bourses/bourses/API/update_data.py", line 19, in client = MongoClient(uri) MONGO_USER: None ^^^^^^^^^^^^^^^^ File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pymongo/mongo_client.py", line 771, in __init__ res = uri_parser.parse_uri( ^^^^^^^^^^^^^^^^^^^^^ File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pymongo/uri_parser.py", line 557, in parse_uri dns_resolver = _SrvResolver(fqdn, connect_timeout, srv_service_name, srv_max_hosts) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pymongo/srv_resolver.py", line 81, in __init__ raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) pymongo.errors.ConfigurationError: Invalid URI host: none is not a valid hostname for 'mongodb+srv://'. Did you mean to use 'mongodb://'? Error: Process completed with exit code 1. Despite these configurations, user ends up being None. Has anyone encountered this issue before? How can I ensure my Python script correctly accesses the secret values?
Источник: https://stackoverflow.com/questions/781 ... -variables