Github action pointing to a different directory than the current directory - github

When running an action during a pull request the path emitted by mkdir while trying to create a folder was different than the current directory set by a previous step. This also continues to the next steps as follows..
name: Publish demo
on:
push:
branches:
- 'develop'
pull_request:
branches:
- 'develop'
jobs:
web-deploy:
name: Deploy
runs-on: windows-latest
steps:
- name: Get latest code
uses: actions/checkout#v3
- name: Setup MSBuild
uses: microsoft/setup-msbuild#v1
- name: Setup NuGet
uses: NuGet/setup-nuget#v1.1.1
- name: Navigate to Workspace
run: cd ${{ github.workspace }}/demo_project
- name: Create Build Directory
run: mkdir _build
- name: Restore Packages
run: nuget restore demo_project.csproj
- name: Build Solution
run: |
msbuild.exe demo_project.csproj /nologo /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=True /p:platform="Any CPU" /p:configuration="Release" /p:PublishUrl="_build"
- name: Sync files
uses: SamKirkland/FTP-Deploy-Action#4.3.3
with:
local-dir: "_build"
server: <server>
username: <username>
password: ${{ secrets.password }}
So, in this line..
run: mkdir _build
the _build folder should be created in demo_project but instead it gets created in ${{ github.workspace }} which i think means that setting the current directory here..
run: cd ${{ github.workspace }}/demo_project
which in turn prints out this in job view..
Run cd D:\a\demo_solution\demo_solution/demo_project
and..
Input file does not exist: D:\demo_project.csproj.
did not take effect. So, what am i missing?

you could specify a different working directory for a job:
You can provide default shell and working-directory options for all run steps in a job
jobs:
web-deploy:
name: Deploy
runs-on: windows-latest
defaults:
run:
shell: bash
working-directory: ${{ github.workspace }}/demo_project
steps:
- name: Get latest code
uses: actions/checkout#v3

Related

Github actions: Re-usable workflows

So what I am trying to do is to organize my ci workflow. I have a self-hosted runner that is my personal computer. Since the workflow files is getting too large I am thinking about reducing the size by having different workflow files instead.
so this is my files so far:
name: "Pipeline"
run-name: ${{ github.actor }} just pushed.
on:
schedule:
- cron: "0 2 * * *"
push:
branches-ignore:
- "docs/*"
tags:
- "v*"
workflow_dispatch:
inputs:
cc_platform:
description: "Input of branch name to build metalayers."
required: true
default: "dev"
jobs:
build:
runs-on: self-hosted
name: Build job.
steps:
- name: Checking out the repository.
uses: actions/checkout#v3
- name: Settings Credentials.
uses: webfactory/ssh-agent#v0.6.0
with:
ssh-private-key: ${{ secrets.SSH_KEY_CC }}
- name: Creating config file.
run:
touch conf
- name: Removing old folders & running build script.
run: |
if [[ -d "my_folder" ]]; then rm -rf my_folder; fi
./build
- name: Generate a zip file of artifacts.
uses: actions/upload-artifact#v3
with:
name: art_${{ github.sha }}
path: |
.*.rootfs.wic.xz
.*.rootfs.manifest
if-no-files-found: error
so what I want to do it to have the artifacts and the build step in their own workflow file. But it does give me som concersn.
Will each workflow file get run in their own runner?
Do they share memory?

Github action execute an action that calls other actions upon its completion

I have to do the following, every time a commit is done (so it can also be done by editing the file from the browser on Github), a Github action is called.
The Github action has to do the following:
Run the command found in the package.json or just run the ncc build command
What such a thing:
"build": "ncc build"
To then commit the build files.
After committing with the push, the 4 Github action test must be run.
How do you advise me to do?
I thought of such a thing:
on:
push:
branches:
- master
name: Build
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
name: Check out current commit
- name: Install
run: npm install
- name: Build
run: npm run build
- name: Commit
run: |
git config --local user.email "41898282+github-actions[bot]#users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .
git commit -m "Build" -a
- name: Push
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
At the moment the test is like this for example, how can I do?
Test.yml
on:
push:
branches:
- master
name: "Testing"
jobs:
test_the_action:
name: Test the action
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout#v2
- uses: suisei-cn/actions-download-file#master
id: downloadfile
name: Download a file
with:
url: "[API Endpoint](https://api.github.com/repos/suisei-cn/actions-download-file)"
target: public/
auto-match: true
- name: Display the file
run: head -n8 public/actions-download-file
There are two options. You can add jobs for each test in your main yml with the needs keyword or call your test yml with the workflow run event as trigger.
Option 1 with needs keyword:
on:
push:
branches:
- master
name: Build
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- <your-build-steps>
test1:
name: Test
runs-on: ubuntu-latest
needs: build
steps:
- <your-test-steps>
Option 2 with workflow run as trigger:
on:
workflow_run:
workflows: ["<name-of-your-main-workflow>"]
types:
- completed
name: "Testing"
jobs:
test_the_action:
This option works only on default branch.

Github Actions reusable workflow access repo issues

I moved some common ci work into its own repo for linting, static checks etc. Multiple repos will then use this to avoid duplication. Issue I am having is, obviously the checks need to be carried out on the repo that invokes the workflow. How is this made possible? When the common workflow is executed it has no access to the contents of the initial repo. It only checks out itself.
Example source repo:
name: Perform Pre Build Check
on:
push:
workflow_dispatch:
jobs:
checks:
uses: <org>/<common-repo>/.github/workflows/checks.yml#main
Common workflow:
name: Perform Pre-Build Checks
on:
workflow_call:
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Run linting check
run: xxxxxx
- name: Install cppcheck
run: sudo apt-get -y install cppcheck
- name: Run cppcheck
run: xxxxx
continue-on-error: true
This is what I ended up doing. Not sure if this was right approach but it worked. Basically just passing in the repo & ref name from current repo that triggered the workflow,
Example source repo:
name: Perform Pre Build Check
on:
push:
workflow_dispatch:
jobs:
checks:
uses: <org>/<common-repo>/.github/workflows/checks.yml#main
with:
repo-name: ${{ github.GITHUB_REPOSITORY }}
ref-name: ${{ github.GITHUB_REF_NAME }}
Common workflow:
name: Perform Pre-Build Checks
on:
workflow_call:
inputs:
repo-name:
required: true
type: string
ref-name:
required: true
type: string
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
repository: ${{inputs.repo-name}}
ref: ${{inputs.ref-name}}
- name: Run linting check
run: xxxxxx
- name: Install cppcheck
run: sudo apt-get -y install cppcheck
- name: Run cppcheck
run: xxxxx
continue-on-error: true

GitHub Action "set-env" result in "No such file or directory"

Trying to set an env var based on the current commit SHA that will later be used as a filename. I've tried numerous variations of "set-env", with a dynamic value or not and each time the build fails with:
Setup Additional Environment Variables0s
##[error]No such file or directory
Run echo ::set-env name=TEST_FILE::test-${GITHUB_SHA}
echo ::set-env name=TEST_FILE::test-${GITHUB_SHA}
shell: /bin/bash -e {0}
##[error]No such file or directory
I am referencing from the Workflow docs: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node }}
- name: Setup Additional Environment Variables
run: echo ::set-env name=TEST_FILE::test-${GITHUB_SHA}
You can use github context to get sha value. Here is the configuration that works for me:
.github/workflows/test.yml
name: Test
on: push
jobs:
test:
name: Test env var
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2.1.0
- name: Set env var
run: |
echo "::set-env name=TEST_FILE_NAME::test-${{ github.sha }}"
- name: Create file
run: |
mkdir temp
cd temp
touch $TEST_FILE_NAME
ls -la
Result:

Running actions in another directory

I've just started exploring Github actions however I've found myself placing a command in multiple places.
I have a PHP project where the composer.json is not in the root, my structure looks like:
my-project:
readme.md
app:
composer.json
Obviously there is more to it and there is a reason why, but my composer.json sits in a subdirectory called 'app'. As a result in my workflow, I have to cd into that folder every time to run a command:
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
run: |
cd app
cp .env.dev .env
- name: Install Composer Dependencies
run: |
cd app
composer install --prefer-dist
- name: Run Tests
run: |
cd app
php bin/phpunit
How can I remove the cd app in every stage?
Update: It's now possible to set a working-directory default for a job. See this answer.
There is an option to set a working-directory on a step, but not for multiple steps or a whole job. I'm fairly sure this option only works for script steps, not action steps with uses.
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Using working-directory, your workflow would look like this. It's still quite verbose but maybe a bit cleaner.
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
working-directory: ./app
run: cp .env.dev .env
- name: Install Composer Dependencies
working-directory: ./app
run: composer install --prefer-dist
- name: Run Tests
working-directory: ./app
run: php bin/phpunit
Alternatively, you can run it all in one step so that you only need to specify working-directory once.
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup and run tests
working-directory: ./app
run: |
cp .env.dev .env
composer install --prefer-dist
php bin/phpunit
You can now add a default working directory for all steps in a job: docs
For the example here, this would be:
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./app
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
run: .env.dev .env
- name: Install Composer Dependencies
run: composer install --prefer-dist
- name: Run Tests
run: php bin/phpunit
Caveat: this only applies to run steps; eg you'll still need to add the subdirectory to with parameters of uses steps, if required.
Hope this will help somebody
name: CI
on:
...
defaults:
run:
working-directory: ./app
jobs:
...