I have 2 workflows inside my repository - one that is a CI that runs on each push and one that is supposed to execute a script on a scheduled time.
The scheduled workflow looks like that:
name: scheduled-run
on:
schedule:
# UTC time
- cron: "45 14 1,15,30 * *"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Python 3.7
uses: actions/setup-python#v2
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Execute python script
env:
TELEGRAM_API_TOKEN: ${{ secrets.TELEGRAM_API_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
AWS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}
run: |
python cci.py -r
No matter how and to when I set the cron schedule - the workflow does not start! Sometimes it would start way after the supposed time but mostly it does not start. I have tried to recreate the workflow from scratch but it doesn't seem to help. What's going on here?
I tested your workflow with slightly changed cron
name: scheduled-run
on:
schedule:
# UTC time
- cron: "55 13,14,15 1,4,15,30 * *"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Python 3.7
uses: actions/setup-python#v2
with:
python-version: 3.7
And it works as expected:
Please try with sth smaller (like above) and with slightly different cron so you would not wait long to verify your changes.
Related
I have written a github actions workflow yml file to schedule a job to run everyday at a particular time but it's not working. I have even used the cron written in official doc but still it is not working. Is it due to Indian TimeZone or some other reason??
name: run app.py
on:
schedule:
- cron: '30 4,17 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v3
- name: setup python
uses: actions/setup-python#v4
with:
python-version: '3.9' # install the python version needed
- name: install python packages
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: execute py script # run main.py
env:
DSA_SHEET: ${{ secrets.DSA_SHEET }}
run: python app.py
I have tried to change the cron many time but it was not working. It was only working when set to run every 5mins but when I set it to run everyday it is not working.
Github actions schedule is in UTC time.
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
I have the following yml that I have updated from a manual run:
name: run scrapper.py
# Controls when the workflow will run
on: [workflow_dispatch]
to the below (including cron schedule):
name: run scrapper.py
on:
schedule:
- cron: "40 16 * * *"
jobs:
build:
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout#v3
- uses: actions/setup-python#v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install -r requirements.txt || pip install --editable . || pip install .
- run: pip install safety
- run: safety check
- name: execute py script
run: python scrapper.py
env:
DB: ${{ secrets.DB }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASS: ${{ secrets.DB_PASS }}
It is looking (to me) as it should according to docs, but it's not triggering the python script. Any ideas?
I need to reuse a workflow in another workflow instead of repeating things.
The build workflow already performs pip install and I just need the installed package to be reused during the deploy workflow instead of re-running pip install
build:
runs-on: ubuntu-latest
needs: create-envfile
steps:
- uses: actions/checkout#v2
- name: Set up Python version
uses: actions/setup-python#v1
with:
python-version: '3.8'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: |
eval `ssh-agent -s`
cat ./.github/workflows/id_rsa | ssh-add -
pip install -r requirements.txt
- name: Upload artifact for deployment jobs
uses: actions/upload-artifact#v2
with:
name: python-app
path: |
.
!venv/
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.somesecret }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: python-app
path: .
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy#v2
id: deploy-to-webapp
with:
app-name: 'project-prod'
slot-name: 'Production'
publish-profile: ${{ secrets.somesecrethere }}
What happens here, is as long as the deploy workflow finishes downloading artifacts from build, it will restart running pip install the packages from requirements.txt, which of course a redundant. How to prevent this and reuse the already installed package instead
I'm having Github workflow to run a python file. Before running the python file I want to swap user so that no sudo is required to execute the python file.
I tried with 'su' but it's says su must be run from command line. How to do that through Github Workflow. Thank you
.yaml
name: CI
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
python: [3.8.1]
steps:
- uses: actions/checkout#v2
- name: Set up Python
uses: actions/setup-python#v2
with:
python-version: ${{ matrix.python }}
- name: run multiple commands
run: |
echo $PWD
whoami
su - user
python helloworld.py
What I have so far is this code:
name: test run
on:
push:
branches:
- V2.0
jobs:
build:
runs-on: [windows-2019]
steps:
- uses: actions/checkout#v2
- name: Set up Python 3.8
uses: actions/setup-python#v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pyautogui
pip install opencv-python
pip install numpy
pip install pynput
- name: Test
run: python Cristishor201/my_repo#V2.0/src/pytest.py
And I want to run pytest.py script which is inside my_repo repository, branch V2.0, and in folder src.
Does anyone have an idea how to do this ?
UPDATE 1:
I found this article when he put github.ref environment variable using if statement. The problem with this solution is that it skip the code, and I already filtered the branch in the trigger block.
name: my workflow
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Execute tests
run: exit 0
deploy:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/master'
steps:
- name: Deploy app
run: exit 0
I tried using env: instead if: but it didn't work.
Github action actions/checkout#v2 pulls the current branch for which this pipeline was triggered. Since you are specifically telling the pipeline to trigger on V2.0 then you don't need to specify a specific branch to checkout.
Now you are in the current working directory so you can just do the following to properly find your file in the path.
python .\src\pytest.py
Note: this assumes your repo directory structure contains src at the root level of your repo
src
└── pytest.py