access script stored in .github directory in github action - github

I have a my_script.sh file in .github/my_script.sh in my repo.
Below is my YAML file:
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
path: master
- name: Set sync script in env
run: |
myscript=$(cat .github/my_script.sh)
echo "::set-env name=MY_SCRIPT::$myscript"
But I got this error:
cat: .github/my_script.sh: No such file
Any clue why?

According to https://github.com/actions/checkout, path is the "relative path under $GITHUB_WORKSPACE to place the repository". Note that does not change the working directory.
This means that using path: master will put your repo in a folder named master. I suspect that you meant to check out the master branch instead. Checkout will automatically checkout the branch the workflow was ran on so most of the time, specifying it specifically is not required.
You either want to remove the path argument or change your code to use the correct path: myscript=$(cat master/.github/my_script.sh)

I think specifying working directory will work.
jobs:
main:
runs-on: ubuntu-latest
defaults:
run:
working-directory: .github
steps:
- uses: actions/checkout#v3
- name: step sync script in env
run: cat my_script.sh
I have checked it and it works.

Related

Github repository is empty when rsync is used

I have added workflow to github actions where i want to rsync files from repository to remote server
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Deploy with rsync
run: rsync -avuz --delete . ${{ secrets.USER }}#${{ secrets.HOST }}:~/App/MainPage/
rsync sends nothing because my repository in source folder is empty, even though there are files in repo.
i get this message
I know that rsync works, because if i change dir (eg ../../) files are being transferred to remote server and i can see them there.
I forgot to add checkout action to workflow .yaml file
...
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
- name: Install SSH Key
uses: shimataro/ssh-key-action#v2
....

Github Actions to see changed files

I am trying to use actions to post a comment on a PR if a file has changed but my action is unable to see the changes.
jobs:
check:
runs-on: self-hosted
permissions:
pull-requests: write
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 2
- name: Get all changed files and echo alert
env:
GITHUB_TOKEN: *******************
run: |
git diff --name-status
or if I change it to git status it outputs a message saying no changes working tree is clean, but I know one file has changed.
I tried using v1 instead of v2 but that doesn't work either.
Does anyone have any ideas on what i am doing wrong or how I can get this working?
Checkout is just "checking out" clean repository state for a given commit or PR.
If you expect to get changes files from PR, you can do it by using external actions, for examples:
- name: Get changed files using defaults
id: changed-files
uses: tj-actions/changed-files#v32
- name: List all added files
run: |
for file in ${{ steps.changed-files.outputs. modified_files }}; do
echo "$file was modified."
done
Use Github contexts: Learn GitHub Actions Contexts
Here is the command you probably need:
git diff --name-only ${{ github.event.after }} ${{ github.event.before }}
Also, you could affect the whole action by setting paths at the beginning of an action:
name: Action #1
on:
push:
branches:
- main
paths:
- <folder>/** // action will be triggered by push to the main branch AND when there are changed files in <folder>
pull_request:
branches:
- main
paths:
- <folder>/*.js // action will be triggered only on pull-requests AND when any .js files in <folder> have been changed

Missing files that have not yet been commited and pushed

Hello I'm trying to test my actions locally using https://github.com/nektos/act
But the problem is when I want to use some of the files that have not yet been committed and pushed to the remote, act says that these files don't exist.
How to solve it? Below is my action config.
As you can seen in first step I'm doing a checkout to actions#v3. Without this, the next step shows nothing, but when I do checkout then it seems that my local sources are not get into consideration.
name: Main
on: [push, workflow_dispatch]
jobs:
terffere:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v3
- id: ls
run: ls -la ./.github/workflows
shell: bash

How to use github-secret in github action workflow?

so I have this code:
name: run-script
on: push
jobs:
run_tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout#v2
- name: Run script file
run: |
echo {here should be the secret} > ~/id_rsa
shell: bash
On my git action, where {here should be the secret} I want to put the variable, which is a secret token saved as a repo secret.
How can this be done?
Thank you for your help.
Assuming you have a secret named TOKEN, you can use it like so:
name: run-script
on: push
jobs:
run_tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout#v3
- name: Run script file
run: |
echo ${{ secrets.TOKEN }} > ~/id_rsa
shell: bash
Unrelated to how to use secrets, please note that > will override the contents of ~/id_rsa.
Secondly, if you want to do something with your private key (which is my guess based on the filename), the correct file would be in ~/.ssh/id_rsa.
And lastly, note that I have changed the checkout action to v3 as that's the latest available version.

GitHub Action checkout from specific directory

I am trying to upload a repo to server via ftp on push to master branch. I have it set up and working. However in the repo there is a folder /public. I only want to upload the files in this folder to the server. Not other files or the folder itself. I have tried to set up a working directory for the job but this doesn't seem to do the trick.. any ideas?
on:
push:
branches:
- master
name: 🚀 Deploy website on push
jobs:
ftp-web-deploy:
name: 🎉 Deploy
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./public
steps:
- name: 🚚 Get latest code
uses: actions/checkout#v2.4.0
working-directory: ./public
with:
fetch-depth: 2
- name: 📂 Sync files
uses: SamKirkland/FTP-Deploy-Action#4.2.0
with:
server: ****
username: ****
password: ${{ secrets.prod_ftp_password }}
server-dir: public_html/
Checking out only one directory is not possible, but has been requested in the actions/checkout repository before: https://github.com/actions/checkout/issues/483
There's an action to check out specific files, but I haven't tried it and I'm not sure if it does what you want: https://github.com/marketplace/actions/checkout-files
You might want to ask yourself why you're trying to limit the number of files transferred. Is it because you're concerned about traffic? Or because of the input expected in the subsequent action?
If it's the latter, you could also manually "fix" the structure by running some mv and rm commands.