Github action reusable workflow - github-actions

I am calling a reusable workflow from another repository.
Basically; Build frontend -> reusable workflow with e2e testing
However, when I'm calling the reusable workflow, it says that "no tests found". The frontend repo calls the reusable workflow like this:
e2e_tests:
needs: publish_frontend
name: Run e2e tests
uses: org/repo/.github/workflows/deploy_tests.yml#master
secrets: inherit
And in my e2e-test, I checkout:
steps:
- name: Checkout
uses: actions/checkout#v2
with:
token: ${{ secrets.GH_PAT || github.token}}
repository: org/e2e-repo
ref: master
Is there anything I'm doing wrong here?

Related

Playwright report per Pull Request available on GitHub Page

I have a demo repo https://github.com/sh977218/Angular-with-Playwright to try to run playwright with Angular on GitHub's action and publish the result to GitHub Page. I'm able to achieve that, but I'd like to improve it by publish each Pull Request's playwright result to GitHub Page and they are all available at same time.
Can someone guide me on how to do this?
This is the github action yml file
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
jobs:
Playwright-tests:
timeout-minutes: 60
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
actions: write
checks: write
contents: write
deployments: write
id-token: write
issues: write
discussions: write
packages: write
pages: write
pull-requests: write
repository-projects: write
security-events: write
statuses: write
# Deploy to the github-pages environment
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact#v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
- name: Setup Pages
uses: actions/configure-pages#v2
- name: Upload artifact
uses: actions/upload-pages-artifact#v1.0.7
with:
path: playwright-report/
- name: Deploy Playwright result to Github Pages
id: deployment
uses: actions/deploy-pages#v1.2.4
Thanks!

What github action can i use in the event of push to get changes that were a part of the commit that was pushed?

Currently, my GitHub workflow looks as follows:
name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on:
push:
branches:
- main
jobs:
update-x:
runs-on: ubuntu-latest
steps:
- name: Git checkout
uses: actions/checkout#v2
with:
# fetch depth is needed since we will be taking git diff with HEAD^1
fetch-depth: 2
- name: Run script
run: scripts/x/x-version-bump.sh
Instead of using git diff in my x-version-bump.sh script I would like to get the changes of the commit pushed via a GitHub action and pass it on to the script. I cannot find a way currently in Github actions to do the same.

Github action to lint after commit in previous step

I'm setting up Github action to lint the OpenAPI Spec using Spectral. Before linting, I would like to generate the single file spec and commit it.
I have set up a workflow that will first build and then lint. But the problem is, the lint is not considering the commit made by Github action in the previous step. It always lint for the commit that triggered this action. Is there any way to lint with the commit made as part of Github action?
You can see from the above image that Github workflow didn't run for the commit made by Github action.
Workflow file:
name: Run Spectral
on:
- pull_request
jobs:
build:
name: Build Spec
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout#v2
- name: Set up Node.js
uses: actions/setup-node#v1
with:
node-version: 12.x
- name: Install dependencies
run: npm install
- name: Build spec file
run: npm run build
- name: Commit build changes
uses: EndBug/add-and-commit#v7
with:
default_author: github_actions
message: 'Compiled spec file'
add: '_build/oas.yaml'
lint:
name: Lint
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout Repository
uses: actions/checkout#v2
- name: Spectral Linting
uses: stoplightio/spectral-action#v0.7.3
with:
file_glob: '_build/oas.yaml'
That's because commits made using the standard GITHUB_TOKEN aren't triggering workflows; you have to use a personal access token for an automated workflow that's supposed to kick off another workflow.
Quoting from the docs (linked above):
When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs. [...]
If you would like to trigger a workflow from a workflow run, you can trigger the event using a personal access token. [...]

How to Add Badges to .readme with Github Actions for a React App

Let's say I have a basic github action that builds my application, then deploys it to firebase:
name: Firebase Deploy
on:
push:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#v2.3.2
- name: Install Dependencies
run: npm install
- name: Run Tests
env:
CI: true
run: npm test
- name: Build
run: npm run build
- name: Archive Production Artifact
uses: actions/upload-artifact#v2
with:
name: build
path: build
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#v2.3.2
- name: Download Artifact
uses: actions/download-artifact#v2
with:
name: build
path: build
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
How do I use this action to automatically update the "build" badge on my .readme? If the action is successful, I'd like to display a "build passing" badge, if it is not, a "build fail" badge. Where do you write the logic for the build badges?
Do I need to have the badge in my readme before I run the action?
Is the build badge a stable url, or is it changed by the action at runtime?
Is this something you can even do with actions or do you need to use travis-ci?
Generally looking for insight into how these badges work and how to use them with Github Actions.
Here is documentation for how to add a workflow status badge.
The status badge represents an image - .png this image is automatically updated by GitHub - e.g if your workflow is:
successful the badge will be "green" and the status "success"
failing the badge will be "red" and the status "failed"
The first part of the badge is the name of the workflow (either the name: attribute or the name of the file)
You can also use the UI to generate a badge. See more in this answer.
Q: Do I need to have the badge in my readme before I run the action?
A: No you can add it anytime, if you add it before the workflow it shows no information (gray coloring). Keep in mind that you may need to "force refresh" the browser to see the new image (browser caching images)
Q: Is the build badge a stable url, or is it changed by the action at runtime?
A: It is as stable as the name of the workflow - if you change the name the badge will stop working.
Q: Is this something you can even do with actions or do you need to use travis-ci?
A: It is GitHub Action functionality, you do not need Travis-CI

Only run GitHub Actions step if not a pull request

I have a workflow which needs to execute either on a push or a pull request with the exception of the last step which pushes a package to NuGet (I don't want this to occur on a pull request, even to master).
How can I prevent the Publish NuGet step from running if the workflow is triggered from a pull request?
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: Publish NuGet
uses: brandedoutcast/publish-nuget#v2.5.2
with:
PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
NUGET_KEY: ${{secrets.NUGET_KEY}}
You can inspect the github.event_name context variable which contains the name of the event that triggered the workflow run. (eg, pull_request or push)
In this case, you can run a step for all events whose name is not pull_request with a github.event_name != 'pull_request' conditional on your step.
For example:
- name: Publish NuGet
uses: brandedoutcast/publish-nuget#v2.5.2
with:
PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
NUGET_KEY: ${{secrets.NUGET_KEY}}
if: github.event_name != 'pull_request'
For future travellers, I found this action that worked quite well. I just needed to do an if: needs.pr-check.outputs.number != 'null' in order to filter by things being a PR or not.
https://github.com/8BitJonny/gh-get-current-pr
github.event_name != 'pull_request' did not work for me because the on.pull_request trigger doesn't exist for workflows that aren't launched by a PR.