How to swap users in Linux through GitHub workflow? - linux

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

Related

fatal: detected dubious ownership in repository at '/github/workspace' in github action for Firebase app-distribution

I am trying to create a CI/CD pipeline for Android project. What I want to achieve is to upload the debug build in Firebase App distribution when the github Action is triggered.
I am using https://github.com/marketplace/actions/firebase-app-distribution-action#firebase-app-distribution-github-action to upload the build in app distribution
I have also tried https://github.com/marketplace/actions/firebase-app-distribution
but getting the same issue in both the cases
Attaching the ScreenShot of the error getting during the github Action
Here is my workflow yaml file
name: Workflow_1
on:
pull_request:
branches: [ dev ]
types:
- closed
jobs:
build:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
# checking out the code to the environment
- uses: actions/checkout#v2
- name: set up Java
uses: actions/setup-java#v2
with:
distribution: 'adopt'
java-version: 11
- name: Make gradlew executable
run: chmod +x ./gradlew
# - name: Build debug APK
# run: ./gradlew assembleDebug
# - name: Upload Debug APKs
# uses: actions/upload-artifact#v2
# with:
# name: Test-App
# path: app/build/outputs/
- name: Firebase App Distribution3
uses: hasretsariyer/firebase-app-distribution-github-action#v1.0
with:
app_id: "${{ secrets.FIREBASE_APP_ID }}"
firebase_token: "${{ secrets.FIREBASE_TOKEN }}"
app_file: app-debug.apk
tester_groups: demoGRP
I searched the web not getting this issue linked with github Action.
Update:
By guessing from the comments below, I edited the checkout part in yaml file
- uses: actions/checkout#v2
with:
set-safe-directory: '*'
which lead to another issues: (Screenshot below)

Cron scheduling not working for Github Action yml file

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?

Github on-schedule action workflow executes in delay or not at all

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.

How to make npm run build on local machine with help of github actions?

I have a Nuxt Js application on my ubuntu server. I use my terminal to enter server and run commands on my server. I can't run command "npm run build" because my VPS has low memory and build command freezes.So I decided to make build on my PC, copy built folder to VPS and then run application.
What should I write in github actions to perform those steps?
-npm run build
-copy built folder from my machine to VPS using ssh(or password, it doesn't matter) to specific folder on VPS
What I did last time and it worked for me:
name: Deployment Setup
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
job-one:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Testing VPS connection and deploy project
uses: appleboy/ssh-action#master
with:
host: 114.12.587.105
port: 1234
username: new-user
key: ${{ secrets.PRIVATE_KEY}}
uses: appleboy/ssh-action#master
script: |
cd /home/kentforth/webapps/myapp
git pull
npm install --production
quasar build
sudo service nginx restart
EDIT:
Here is my deploy.yml file:
name: 'test my project'
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- run: npm run build
Here is What I get in github actions:
It seems github actions try to find path /home/runner/work/my project name
But I do not have such directory
How can specify I the folder on my local machine where "npm run build" command should run?

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

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

Resources