feat: testing conditional on failure #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Project | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
- fix-* | |
- feat/** | |
paths-ignore: | |
- wofkflows/* | |
env: | |
MONGODB_DB_NAME: gha-demo | |
jobs: | |
lint: | |
# Notice: ubuntu runner comes with node.js pre installed | |
runs-on: ubuntu-latest | |
steps: | |
# download the code from the repository | |
- name: Get code | |
uses: actions/checkout@v4 | |
# To fix a node.js version | |
- name: Install NodeJS | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "18" | |
- name: info on kind of event triggering the workflow | |
run: echo '${{ toJSON(github.event) }}' | |
- name: Cache Dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.npm # npm cache folder path | |
# key to retrieve the cache | |
key: deps-node-module-${{ hashFiles('**/package-lock.json') }} | |
- name: install dependencies | |
run: npm ci # to ensure you don't install some breaking versions | |
- name: run linter | |
run: npm run lint | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
# download the code from the repository | |
- name: Get code | |
uses: actions/checkout@v4 | |
# To fix a node.js version | |
- name: Install NodeJS | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "18" | |
- name: Cache Dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.npm # npm cache folder path | |
# key to retrieve the cache | |
key: deps-node-module-${{ hashFiles('**/package-lock.json') }} | |
- name: install dependencies | |
run: npm ci # to ensure you don't install some breaking versions | |
- name: Run tests | |
id: run-test | |
run: npm test | |
- name: Upload test report | |
# we'd like to upload the test report only when the test fails | |
if: failure() && steps.run-test.outcome == 'failure' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: test-report | |
path: test.json | |
build: | |
needs: [lint, test] | |
runs-on: ubuntu-latest | |
outputs: | |
script_file: ${{ steps.publish_js_filename.outputs.script_file }} | |
steps: | |
# download the code from the repository | |
- name: Get code | |
uses: actions/checkout@v4 | |
# To fix a node.js version | |
- name: Install NodeJS | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "18" | |
- name: Cache Dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.npm # npm cache folder path | |
# key to retrieve the cache | |
key: deps-node-module-${{ hashFiles('**/package-lock.json') }} | |
- name: install dependencies | |
run: npm ci # to ensure you don't install some breaking versions | |
- name: Build code | |
run: npm run build | |
- name: Publish JS filename | |
id: publish_js_filename | |
run: | | |
find dist/assets/*.js -type f -exec echo 'script_file={}' >> $GITHUB_OUTPUT ';' | |
cat "$GITHUB_OUTPUT" | |
# soon deprecated | |
# run: find dist/assets/*.js -type f -execdir echo '::set-output name=script-file::{}' ';' | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: built-artifacts | |
path: dist | |
deploy: | |
# to run the deployment only when the test job is successful... | |
needs: [lint, test, build] # array of jobs to wait for | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get build artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
name: built-artifacts | |
- name: Output built files | |
run: ls -la | |
- name: Output file name | |
# needs object contains all output of jobs | |
run: | | |
echo "${{ needs.build.outputs.script_file }}" | |
- name: Deploy | |
run: echo "Deploying project..." |