Github action test if a commit containing a specific word was previously made - github

I need to make sure to test with github action, if a commit has previously been made that contains the word build.
If the commit does not contain the word build then tests with github action should not be run.
Can you give me some advice?
Test:
name: "Testing"
on:
push:
branches:
- master
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

From a push event, it's possible to extract the commit message by using github.event.commit.message
Here is an example of the github context for a push event.
Note that if there are several commit messages:
commit[0] contains the oldest commit
${{ github.event.commits[0].message }}
head_commit contains the youngest commit
${{ github.event.head_commit.message }}
Then, you can check in your job if the commit message contains or not the word you want, for example by using:
if: "!contains(github.event.head_commit.message, 'build')"
Therefore, your workflow could look like this if you don't want the job to be run if the commit message contains the 'build' word:
name: "Testing"
on:
push:
branches:
- master
jobs:
test_the_action:
if: "!contains(github.event.head_commit.message, 'build')"
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
Finally, you now also have the option to skip ci workflows with key words in the commit messages.

Related

"Can't find 'action.yml'" in reusable workflow

I have below structure:
.github
└── workflows
└── main.yml
└── send_alerts.yml
Now in main, I am using
jobs:
main:
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: some job
run: |
......
send_alerts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- uses: ./.github/workflows/send_alerts.yml#feature/workflow1
with:
provision_status: "Success"
in my send_alerts.yml
name: Creating and Sending Alerts/Status
on:
workflow_call:
provision_status:
required: true
type: string
jobs:
create_send_alerts:
runs-on: ubuntu-latest
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: Some other jobs
run: |
.....
so this thorws me error:
Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/git-repo/.github/workflows/send_alerts.yml#feature/workflow1'. Did you forget to run actions/checkout before running your local action?
So my question is why it is complaining about action in send_alerts for main? while in the main.yml same actions/checkout#v3 works just fine?
I tried both actions/checkout#v2 and v3 in all cases I have same error
There are at least three things wrong:
You're calling a reusable workflow like an action. The call to a reusable workflow replaces the entire job, not just one step.
When you reference a workflow (or an action) that's in the same repository, with a relative path, you must not add an #version suffix. If you look at the error message, the runner is interpreting that as a directory name.
Both together result in something like this:
send_alerts:
uses: ./.github/workflows/send_alerts.yml
with:
provision_status: "Success"
You have to declare the parameter in an inputs object in the reusable workflow:
on:
workflow_call:
inputs:
provision_status:
required: true
type: string

Run github actions if branch has updated files from a specific directory

File structure:
apps
-- app-1
-- app-2
libs
-- lib-1
-- lib-2
We have tests that should run only in case if files were changed in lib-2.
I have tried to do
on:
push:
paths:
- 'libs/lib-2/**'
But it runs tests only when files from lib-2 were pushed in a commit but not running if some others were pushed after that.
Imagine tests are failed for lib-2, then developer have submitted files from lib-1 in the next commit and tests just wouldn't run for previous changes and github will consider checks as a success.
Is there a way to run actions if files from a certain directory were changed in a branch no matter in what commit?
I have designed a solution thanks to #guifalourd.
name: 'UI-kit Tests'
on:
pull_request:
branches:
- proto
- develop
- staging
- master
jobs:
filter-ui-kit:
runs-on: ubuntu-latest
name: Filter Ui kit
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 0
- name: Get changed files in the docs folder
id: changed-files-specific
uses: tj-actions/changed-files#v34
with:
files: libs/ui-kit/**
- name: Run step if any file(s) in the docs folder change
if: steps.changed-files-specific.outputs.any_changed == 'true'
run: echo UI-kit is affected
- name: Prevent from running
if: steps.changed-files-specific.outputs.any_changed != 'true'
run: exit 1
test:
timeout-minutes: 60
runs-on: ubuntu-latest
needs: [filter-ui-kit]
steps:
... test actions goes there

GitHub Actions get from API to create pull request

Is there a way to tell GitHub to automatically create a pull request from an API providing JSON content and merge it into my project?
I want to:
Edit files on a platform (I control the platform) using my own production editors/tools.
Have GitHub request it (REST), then create a PR or a commit, so people can collaborate on it with forks/GitHub project management.
Push from GitHub back to the platform for publishing.
3 is no problem, but 2 I can't find documentation for if it's even possible.
name: Manual workflow
on:
workflow_dispatch:
jobs:
makefiles:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout#v2
- name: Getting
uses: fjogeleit/http-request-action#master
id: myRequest
with:
url: 'https://domain/api/file'
method: 'GET'
- name: Show File
run: echo ${{ steps.myRequest.outputs.response }}
- name: Create A File
uses: 1arp/create-a-file-action#0.2
with:
path: 'src'
file: 'foo.bar'
content: ${{steps.myRequest.outputs.response}}
- name: final commit
uses: zwaldowski/git-commit-action#v1
id: git_commit
- name: show
run: echo "${{ steps.git_commit.outputs.sha }}"

How to trigger a new workflow from another workflow based on a path filter?

I have one workflow (that I want to be triggered by any commit with no path filter)
name: workflow1
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
...
that triggers another workflow
name: workflow2
on:
workflow_run:
workflows: ["workflow1"]
types:
- completed
jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
...
which is working fine with the workflow_run event. Now is it possible somehow to restrict workflow2 from being triggered even further (like with a path filter)? E.g. I make a commit to folder folder1 -> workflow1 triggered -> workflow2 triggered and if it's not a commit to the folder folder1, e.g. folder2 -> workflow1 triggered -> workflow2 not triggered. Is there something like the following I can add to workflow2?
name: workflow2
on:
paths:
- 'folder1/**'
workflow_run:
workflows: ["workflow1"]
types:
- completed
jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
...
Or another way to achieve that?
Thanks in advance for any help.
First option: Did you consider using the action verify-changed-files in your workflow run to execute the jobs only if specific files are updated?
Second option: It's more verbose, but you could save the path from the first workflow in an artifact to download in the second workflow.
Using the variable GITHUB_EVENT_PATH that returns the path of the file with the complete webhook event payload. For example, /github/workflow/event.json
It would look like this
In the FIRST workflow, you extract the path, then you save that number into a file and upload it as an artifact.
- name: Save the PATH in an artifact
shell: bash
env:
- PATH: {{ github.event.path}} ## If it's not specific enough, you can extract the $PATH variable on a previous step using the shell.
run: echo $PATH > path.txt
- name: Upload the PATH
uses: actions/upload-artifact#v2
with:
name: path
path: ./path.txt
In the SECOND workflow, you get the artifact and the path from the FIRST workflow, using the following GitHub Apps:
- name: Download workflow artifact
uses: dawidd6/action-download-artifact#v2.11.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: <first_workflow_name>.yml
run_id: ${{ github.event.workflow_run.id }}
- name: Read the path.txt file
id: path_reader
uses: juliangruber/read-file-action#v1.0.0
with:
path: ./path/path.txt
- name: Step to check the path output from the step above with an if condition to perform an operation (or not)
[...]
This link can also help to understand how to extract the PATH depending on the event.

How to read tag name using workflow_run

I'm using Github actions with two workflows: CI and CD. The CI workflow is triggered for new tags like v1.1.1 and pull requests to develop and hotfix branches.
name: CI
on:
push:
tags: v[1-9]+.[0-9]+.[0-9]+
pull_request:
branches: [develop, hotfix*]
The CD workflow is triggered when the previous workflow (CI) is completed.
name: CD
on:
workflow_run:
workflows: ['CI']
push:
tags: v[1-9]+.[0-9]+.[0-9]+
types:
- completed
Currently, my goal is to generate packages (Docker images) based on the name of the new tag. I'm trying to read the new tag name in the CD workflow using the action dawidd6/action-get-tag#v1:
- name: Get tag
id: tag
uses: dawidd6/action-get-tag#v1
- name: Use tag
run: echo ${{steps.tag.outputs.tag}}
But I'm getting the following error:
Run dawidd6/action-get-tag#v1
env:
IMAGE_NAME: open-tuna-api
Error: Not a tag ref (refs/heads/master)
My question is: how to read the tag name in my CD workflow that is triggering after the CI workflow?
First of all, you can get the tag without using an action, with ${GITHUB_REF##*/}.
Sample test workflow:
name: Experiment
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Debug
run: echo "Works. Tag is ${GITHUB_REF##*/}"
As for the chained workflows you mention - I am not sure it is possible to get the tag of the ancestor workflow, since the documentation mentions it is triggered on the default branch, and the last commit on that branch.