I want to create a GitHub Action with some conditional jobs. I want to run these conditional jobs paralell, but after all finished I want to continue with another job what needs to wait finish all conditional jobs.
This is my workflow what I want to implement:
- Detect file changes in template folders
- Run
npm run prodin every template folder what changed - Wait until all template build finished
- Coninue the deploy process
Here is my deploy.yml file:
name: Deploy
on:
push:
branches:
- deploy
jobs:
check-theme-changes:
name: Check template changes
outputs:
run_job: ${{ steps.check_files.outputs.run_job }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2
- name: check modified files
id: check_files
run: |
echo "=============== list modified files ==============="
git diff --name-only HEAD^ HEAD
echo "========== check paths of modified files =========="
git diff --name-only HEAD^ HEAD > files.txt
while IFS= read -r file
do
echo $file
if [[ $file != laravel/resources/themes/project_a/* ]]; then
echo "::set-output name=run_project_a_build::false"
break
else
echo "::set-output name=run_project_a_build::true"
break
fi
done < files.txt
while IFS= read -r file
do
if [[ $file != laravel/resources/themes/project_b/* ]]; then
echo "::set-output name=run_project_b_build::false"
break
else
echo "::set-output name=run_project_b_build::true"
break
fi
done < files.txt
build-theme-project-a:
name: Build project_a theme
needs: check-theme-changes
if: needs.check-theme-changes.outputs.run_project_a_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Read current version
id: CURRENT
run: echo "::set-output name=VERSION::$(cat /var/www/latest)"
- name: Install dependencies
run: |
cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
npm ci
- name: npm run prod --theme=project_a
run: |
cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
npm run prod --theme=project_a
- name: Publish build
run: # some git command here
build-theme-project-b:
name: Build project_b theme
needs: check-theme-changes
if: needs.check-theme-changes.outputs.run_project_b_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Read current version
id: CURRENT
run: echo "::set-output name=VERSION::$(cat /var/www/latest)"
- name: Install dependencies
run: |
cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
npm ci
- name: npm run prod --theme=project_b
run: |
cd /var/www/${{steps.CURRENT.outputs.VERSION}}/laravel
npm run prod --theme=project_b
- name: Publish build
run: # some git command here
prepare:
name: Prepare to clone a new version
needs: [
build,
build-theme-project-a,
build-theme-project-b
]
runs-on: self-hosted
steps:
- name: Save current date
run: echo "$(date '%s')" > /var/www/latest
The problem is if a theme isn't changed the variable will be false and then the prepare job's need will also skip the proceduring and all next step too, because they expect previous job finish.
How can I run job after a skipped step, but wait for it to finish?
CodePudding user response:
The answer is to use:
if: always()
on your prepare job.
You can then combine it with checking results, like this:
prepare:
name: Prepare to clone a new version
needs: [
build,
build-theme-project-a,
build-theme-project-b
]
if: ${{ always() && !cancelled() && needs.build.result == 'success' }}
To avoid running it in case job was cancelled.
