How to define a test run in github-actions in a specific branch for a python script? - python-3.x

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

Related

Why my github action's cron is not working?

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

Reuse Workflow in github action - Azure deployment

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

Solana: How to setup github action (CI) for an Anchor Project

I've my anchor project build with serum's anchor framework.
I want to set up github action, So that whenever a new pull request is raised or any commit is made on the main branch, I can be sure that no code has been broken and flag any such pull request.
Here is what I've tried. But it needs around 18 to 20 minutes to run and still unsuccessful.
name: Rust
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Install AVM
run: cargo install --git https://github.com/project-serum/anchor avm --force
- name: Install Anchor
run: avm install 0.24.2 && avm use 0.24.2
- name: Build
run: anchor build
- name: Run tests
run: anchor test
The initial approach was not efficient
cons:
need many manual installations of solana, nvm, node and yarn
hence too much time for a run
complex
reinventing wheel
When dug deep found out that found out that serum releases docker images for this purpose only. So I modified my action file to
name: Rust
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
container: projectserum/build:v0.24.2
steps:
- uses: actions/checkout#v3
- name: List rustup toolchains
run: rustup toolchain list
- name: Set default toolchain
run: rustup default stable
- name: List rustup toolchains
run: rustup toolchain list
- name: Generate new keygen
run: solana-keygen new
- name: Set solana target cluster to local
run: solana config set --url http:localhost:8899
- name: Check solana config
run: solana config get
- name: Install yarn dependencies
run: yarn install
- name: Build
run: anchor build
- name: Run tests
run: anchor test
It brought down the run time from 18 minutes to 3 minutes approximately.
References:
https://github.com/project-serum/anchor/tree/master/docker
https://github.com/yourarj/solana-twitter-enhanced/blob/main/.github/workflows/rust.yml

How to swap users in Linux through GitHub workflow?

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

sync python code on GitHub repo and deploy on Azure function

I would like to sync the code (in an azure functions folder). I followed the doc to create a github actions cd. However in azure portal in my functions sections functions, my folder does not appear. Anyone have any idea what the problem is. I have no errors in my workflow.
I have an error when i'm trying to Redeploy/sync my code: it's not found...
My Worflow File
Just post how I deploy a function from GitHub repo.
My file structure:
https://github.com/Paprika-a11y/pythonfunc.git
Navigate to the Deployment Center page, configure the settings:
After saving the settings, check the deployment process on GitHub Action:
If your function project is correct locally, then it should appear on portal:
If you need the workflow, here is the file generated automatically:
# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions
name: Build and deploy Python project to Azure Function App - pyfunctemp
on:
push:
branches:
- main
workflow_dispatch:
env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
PYTHON_VERSION: '3.7' # set this to the python version to use (supports 3.6, 3.7, 3.8)
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout#master
- name: Setup Python ${{ env.PYTHON_VERSION }} Environment
uses: actions/setup-python#v1
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: 'Resolve Project Dependencies Using Pip'
shell: bash
run: |
pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
python -m pip install --upgrade pip
pip install -r requirements.txt --target=".python_packages/lib/site-packages"
popd
- name: 'Run Azure Functions Action'
uses: Azure/functions-action#v1
id: fa
with:
app-name: 'pyfunctemp'
slot-name: 'production'
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
publish-profile: ${{ secrets.AzureAppService_PublishProfile_xxxxxx }}

Resources