|
| 1 | +name: Run Python Script |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run at 10:00 a.m., 6:00 p.m., and 2:00 a.m. UTC |
| 6 | + - cron: "0 10 * * *" |
| 7 | + - cron: "0 18 * * *" |
| 8 | + - cron: "0 2 * * *" |
| 9 | + |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + run-python-script: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: github.repository_owner == 'MicrosoftDocs' |
| 16 | + |
| 17 | + steps: |
| 18 | + # Set up Python |
| 19 | + - name: Set up Python |
| 20 | + uses: actions/setup-python@v4 |
| 21 | + with: |
| 22 | + python-version: '3.12' |
| 23 | + |
| 24 | + # Install dependencies (if any) |
| 25 | + - name: Install dependencies |
| 26 | + run: | |
| 27 | + python -m pip install PyGithub |
| 28 | +
|
| 29 | + # Run the Python script |
| 30 | + - name: Run script |
| 31 | + env: |
| 32 | + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + run: | |
| 34 | + from datetime import datetime |
| 35 | + import os |
| 36 | + import sys |
| 37 | + import time |
| 38 | +
|
| 39 | + from github import Github |
| 40 | + from github.GithubException import GithubException |
| 41 | +
|
| 42 | + # Create a Github instance |
| 43 | + # Access the secret |
| 44 | + access_token = os.getenv("GITHUB_ACCESS_TOKEN") |
| 45 | + github = Github(access_token) |
| 46 | + # Support private |
| 47 | + repo = github.get_repo("MicrosoftDocs/SupportArticles-docs-pr") |
| 48 | + # Get the current date and time |
| 49 | + current_date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 50 | + # Pull request content |
| 51 | + title = f"Auto push to live {current_date_time}" |
| 52 | + body = "Daily auto push to live." |
| 53 | + head = "main" # Format: "username:branch" |
| 54 | + base = "live" # The branch you want to merge your changes into |
| 55 | + finish_commit = (f'Successfully push to live at {current_date_time}.') |
| 56 | + # Start process |
| 57 | + try: |
| 58 | + # Check if a PR from 'main' to 'live' already exists |
| 59 | + existing_prs = repo.get_pulls(state='open', head=head, base=base) |
| 60 | + if existing_prs.totalCount > 0: |
| 61 | + print(f'Found existing PR(s) from "{head}" to "{base}".\n {'\n '.join(pr.html_url for pr in existing_prs)}') |
| 62 | + sys.exit(1) |
| 63 | + print(f'Start creating pull request.') |
| 64 | + pull_request = repo.create_pull(title=title, body=body, head=head, base=base) |
| 65 | + print(f"PR created: {pull_request.html_url}. Start merging.") |
| 66 | + # Merge |
| 67 | + time.sleep(5) |
| 68 | + pull_request.merge() |
| 69 | + print('Success push to live.') |
| 70 | + sys.exit(0) |
| 71 | + except GithubException as e: |
| 72 | + print(f"Error: {str(e)}") |
| 73 | + sys.exit(1) |
| 74 | +
|
0 commit comments