Running actions in another directory - github

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:
...

Related

Github action pointing to a different directory than the current directory

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

PNPM Github actions flow

I just moved my project from npm and lerna to pnpm but now when using GitHub actions I get the following error
"line 1: pnpm: command not found"
can someone suggest how the .yml file should be, I've posted the current version below?
name: Lint & Unit Test
on: [pull_request]
jobs:
run-linters:
name: Run linter and Unit tests
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout#v3
- name: ACTIONS_ALLOW_UNSECURE_COMMANDS
run: echo 'ACTIONS_ALLOW_UNSECURE_COMMANDS=true' >> $GITHUB_ENV
- name: Set up Node.js
uses: actions/setup-node#v3
with:
node-version: 16.18.1
- name: Portal Install Node.js dependencies
working-directory: ./portals
run: |
pnpm install
- name: Portals Lint & tests
working-directory: ./portals
run: |
cat .env.example > .env
pnpm run build:tailwind
pnpm run lint
pnpm test
- name: Services Install Node.js dependencies
working-directory: ./services
run: |
pnpm install
- name: Services Lint & tests
working-directory: ./services
run: |
pnpm run lint
pnpm test
You might need to use first the pnpm/action-setup action, in order to install pnpm.
on:
- push
- pull_request
jobs:
install:
runs-on: ubuntu-latest
steps:
- uses: pnpm/action-setup#v2
with:
version: 6.0.2

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

How to cache npm dependencies in GitHub action?

Below is my dockerfile. Is there a way to cache npm in GitHub action?
FROM node
WORKDIR /app
ADD package*.json ./
RUN npm ci
ENV PATH /app/node_modules/.bin:$PATH
My GitHub actions:
name: NPM buid
on:
push:
branches:
- main
jobs:
build-npm-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Build and Tag Node image
id: build-ui-image
run: |
export DOCKER_BUILDKIT=1
docker build -t ui -f ./ui/Dockerfile .
P.S I don't want to cache docker image. Above docker file is used just for example. objection is cache npm dependencies
This can be achieved with actions/setup-node#v3
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: current
cache: npm
- name: Build and Tag Node image
id: build-ui-image
run: |
export DOCKER_BUILDKIT=1
docker build -t ui -f ./ui/Dockerfile .
Did you check below action ?
actions/cache#v2
So your Actions yaml would look like below,
name: NPM buid
on:
push:
branches:
- main
jobs:
build-npm-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- uses: actions/setup-node#v2
with:
node-version: '14'
- name: Caching node modules
uses: actions/cache#v2
with:
path: "app/node_modules"
key: node-modules-${{ hashFiles('app/package.json') }}
- name: Build and Tag Node image
id: build-ui-image
run: |
export DOCKER_BUILDKIT=1
docker build -t ui -f ./ui/Dockerfile .
Caching npm dependencies will not make a difference if you're building a docker image. You can use actions/setup-node for caching JS or docker/build-push-action for Docker.

Github CI/CD pipeline crashes with webpack

I'm trying to learn how CI/CD pipelines work.
I decided to use it with my portfolio page which should re-run itself on every push.
Here is my yaml config:
name: Build Bundle for Github Pages
on:
push:
branches:
- source
env:
NODE_ENV: production
PUBLIC_URL: http://crrmacarse.github.io/
GA_TRACKING_CODE: ${{ secrets.GA_TRACKING_CODE }}
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
with:
persist-credentials: false
- name: Build
run: |
npm install
npm run prod:pipeline
npm run sitemap
cp dist/index.html dist/404.html
cp google21029c74dc702d92.html dist/
cp robots.txt dist/
- name: Deploy
uses: JamesIves/github-pages-deploy-action#releases/v3
with:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BRANCH: source
FOLDER: dist
Here is the error:
Source code for the webpack config:
https://github.com/crrmacarse/crrmacarse.github.io/blob/source/compiler/production.pipeline.js
If the error message is cannot find module 'html-webpack-plugin', you could try, for testing, to install it.
See survivejs/webpack-book issue 100 as an example:
The solution was to run npm i html-webpack-plugin --save-dev before building with webpack
The OP has fixed the GitHub Action workflow with crrmacarse/crrmacarse.github.io commit 8a4397b
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2.0.0
- name: Use node 12
uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org
- name: install
run: npm install
- name: lint
run: npm run sitemap
- name: build
run: npm run prod
- name: copy
run: npm run copy
Then the module html-webpack-plugin is properly installed and available, as seen in this Actions CI run.
Use npm install before build your project which installs your npm library on docker where your project will be built.
Note: Don't forget to define html-webpack-plugin on packge.json file