How to remove special characters from a branch name on github actions - github

My Branch name IS AKA-2120
i using this as a job to get the branch name.
extract_branch_name:
runs-on: ubuntu-latest
steps:
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
outputs:
branch: ${{ steps.extract_branch.outputs.branch }}
but what i actually need to my output is aka2120
theres a way to remove special characters and lower the branch name?

There are several ways to solve your problem.
One way is to use existing actions in Marketplace:
- uses: mad9000/actions-find-and-replace-string#2
id: findandreplace
with:
source: ${{ github.ref }}
find: '-'
replace: ''
- uses: ASzc/change-string-case-action#v2
id: lowercase
with:
string: ${{ steps.findandreplace.outputs.value }}
- name: Get the above output
run: echo "The replaced value is ${{ steps.lowercase.outputs.lowercase }}"
If you want just bash formula, that will work:
echo ${GITHUB_REF#refs/heads/} | tr "[:upper:]" "[:lower:]" | sed -e 's/-//g'

Related

How to trigger workflow when all files in PR are .csv

I'd like to create a workflow in GitHub that triggers when all the changed files are .csv. I've been looking at GitHub's workflow syntax and I can only find instances where workflows are triggered when at least 1 certain file/directory is excluded or included.
My initial approach was
on:
push:
paths:
- '**.csv'
But this workflow will trigger as long as 1 file ends in .csv
What I ended up doing was running a workflow that tirggers when .csv files are added in the PR. Then a job starts that collects all the names of the files changed. Last it loops through the files changed and returns 'false' if any file doesn't end in .csv.
You can then add another job that uses the value of needs.compare.outputs.compare
name: csv-check
on:
pull_request:
branches:
- '**'
paths:
- '**.csv'
jobs:
changed_files:
runs-on: ubuntu-latest
outputs:
all: ${{ steps.changes.outputs.all }}
steps:
- name: Checkout repository
uses: actions/checkout#v2
with:
fetch-depth: 2
- name: Get changed files
id: changes
run: |
echo "::set-output name=all::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)"
compare:
runs-on: ubuntu-latest
needs: changed_files
outputs:
compare: ${{ steps.all_csv.outputs.compare }}
if: ${{ needs.changed_files.outputs.all }}
steps:
- name: echo changed files
id: all_csv
run: |
echo "::set-output name=compare::true"
for file in ${{ needs.changed_files.outputs.all }}; do
if [[ $file != *.csv ]]; then
echo "::set-output name=compare::false"
fi
done
I suggest to review https://github.com/dorny/paths-filter - "GitHub Action that enables conditional execution of workflow steps and jobs, based on the files modified by pull request, on a feature branch, or by the recently pushed commits".

Github Action needs context is not available in container.image

I'm trying to parametrize the jobs.myjob.container.image field. The documentation says the needs context is available there:
Contexts documentation
Specifically this:
Workflow key
Context
jobs.<job_id>.container
github, needs, strategy, matrix, env, secrets, inputs
But it doesn't work. My job output is an empty string, causing an error.
get_image:
name: get_image
runs-on: self-hosted
outputs:
image: ${{ steps.jq.image }}
needs:
- ...
steps:
- name: Checkout code
uses: actions/checkout#v3
- name: jq
id: jq
run: |
set -x
export TAG=$(jq -r '.${{ github.event.inputs.cluster }} | .tag' data.json)
echo "::set-output name=image::registry.com/mycontainer:$TAG"
job2:
name: job2
runs-on: self-hosted
needs:
- get_image
container:
image: ${{ needs.get_image.outputs.image }} <--- this is an empty string
credentials:
...
steps:
...
The error I'm getting is Error: The template is not valid. ...: Unexpected value ''.
Is the documentation lying to me or am I just reading it wrong?
Other questions lead me to think that the thing I want to do is not allowed.
parametrize container.image
github community discussion
You should use outputs here image: ${{ steps.jq.outputs.image }}.

Github Actions steps id, how to call stdout of it? [duplicate]

This question already has answers here:
How do I get the output of a specific step in GitHub Actions?
(4 answers)
Closed last year.
So I have issue because I want to store value of my branch prefix as id but I stumbled across... how to call it in other step?
I have something like this, so far I tried steps.branch-prefix.output.stdout and steps,branch-prefix.output.branch-prefix (first one was my instict because... what I do there returns all in stdout...)
Workflow sample:
name: PR Semver
on: [push]
jobs:
update-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Get current prefix
id: branch-prefix
run: echo $GITHUB_REF | sed -E 's/^refs\/heads\/(.*)\/.*/\1/'
- name: Check if branch prefix is valid (major, minor, patch)
run: |
echo "Checking branch prefix..."
echo "branch prefix: ${{ steps.branch-prefix.output.stdout }}"
if [[ ${{ steps.branch-prefix.output.stdout }} != "major" && ${{ steps.branch-prefix.output.stdout }} != "minor" && ${{ steps.branch-prefix.output.stdout }} != "patch" ]]; then
echo "Branch prefix is not valid, exiting..."
exit 1
fi
It seems like you need to use the set-output command
I think it would be something like
- name: Get current prefix
id: branch-prefix
run: |
prefix=$(echo $GITHUB_REF | sed -E 's/^refs\/heads\/(.*)\/.*/\1/')
echo "::set-output name=prefix::$prefix"
And getting it with ${{ steps.branch-prefix.output.prefix }}

Retrieving list of modified files in GitHub action

I'm new to GitHub actions and am currently using https://github.com/foo-software/lighthouse-check-action to have audits done automatically. But since the urls have to be hard-coded in, it doesn't prove that useful when wanting to audit only the modified pages in a commit and failing based off of those.
In the case that I am totally missing something, is there a way to achieve the above? I was looking at some actions like https://github.com/marketplace/actions/get-changed-files but I can't get it to work. I also looked at the GitHub events and references docs and was unable to figure out something with those. Would someone point me in the right direction?
Thank you in advance for your help!
lots0logs/gh-action-get-changed-files action is broken atm due to this bug. Take a look at jitterbit/get-changed-files action. It works perfectly for me:
.github/workflows/test.yml
name: Test
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2.1.0
- uses: jitterbit/get-changed-files#v1
id: abc
with:
format: space-delimited
token: ${{ secrets.GITHUB_TOKEN }}
- name: Printing
run: |
echo "All:"
echo "${{ steps.abc.outputs.all }}"
echo "Added:"
echo "${{ steps.abc.outputs.added }}"
echo "Removed:"
echo "${{ steps.abc.outputs.removed }}"
echo "Renamed:"
echo "${{ steps.abc.outputs.renamed }}"
echo "Modified:"
echo "${{ steps.abc.outputs.modified }}"
echo "Added+Modified:"
echo "${{ steps.abc.outputs.added_modified }}"
Logs output:
2020-05-15T13:47:15.5267496Z All:
2020-05-15T13:47:15.5268424Z .github/workflows/test.yml .tidy-renamed2 Test.ts hello.py
2020-05-15T13:47:15.5268537Z Added:
2020-05-15T13:47:15.5268609Z hello.py
2020-05-15T13:47:15.5268697Z Removed:
2020-05-15T13:47:15.5268787Z Test.ts
2020-05-15T13:47:15.5268880Z Renamed:
2020-05-15T13:47:15.5269260Z .tidy-renamed2
2020-05-15T13:47:15.5269357Z Modified:
2020-05-15T13:47:15.5269450Z .github/workflows/test.yml
2020-05-15T13:47:15.5269547Z Added+Modified:
2020-05-15T13:47:15.5269625Z .github/workflows/test.yml hello.py
2020-05-15T13:47:15.5306656Z Post job cleanup.
After trying unsuccessfully with both plugins above and some more I have resorted to the following:
- uses: actions/checkout#v2
with:
fetch-depth: 0
- name: (CI) Dependencies update check
run: |
current_commit=`git log -n 1 --pretty=format:%H`
echo $current_commit
last_deps_mod_commit=`git log -n 1 --pretty=format:%H -- composer.json`
echo $last_deps_mod_commit
if [ $current_commit == $last_deps_mod_commit ]; then echo USE_LOCK=0 > ci.conf; else echo USE_LOCK=1 > ci.conf; fi
Observe that it must be a full checkout (depth 0) not a flat one otherwise it will always return true.

How do I get the output of a specific step in GitHub Actions?

I have this GitHub Actions workflow which runs tests, but now I am integrating slack notification in it. I want to get the output of the Run tests step and send it as a message in the slack step.
- name: Run tests
run: |
mix compile --warnings-as-errors
mix format --check-formatted
mix ecto.create
mix ecto.migrate
mix test
env:
MIX_ENV: test
PGHOST: localhost
PGUSER: postgres
- name: Slack Notification
uses: rtCamp/action-slack-notify#master
env:
SLACK_MESSAGE: Run tests output
SLACK_TITLE: CI Test Suite
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
You need to do 3 things:
Add an id to the step you want the output from
Create the outputs using the GITHUB_OUTPUT environment variable
Use the id and the output name in another step to get the outputs and then join them into one message for slack
- name: Run tests
run: |
echo "mix-compile--warnings-as-errors=$(mix compile --warnings-as-errors)\n" >> $GITHUB_OUTPUT
echo "mix-format--check-formatted=$(mix format --check-formatted)\n" >> $GITHUB_OUTPUT
echo "mix-ecto_create=$(mix ecto.create)\n" >> $GITHUB_OUTPUT
echo "mix-ecto_migrate=$(mix ecto.migrate)\n" >> $GITHUB_OUTPUT
echo "mix-test=$(mix test)\n" >> $GITHUB_OUTPUT
id: run_tests
env:
MIX_ENV: test
PGHOST: localhost
PGUSER: postgres
- name: Slack Notification
uses: rtCamp/action-slack-notify#v2
env:
SLACK_MESSAGE: ${{join(steps.run_tests.outputs.*, '\n')}}
SLACK_TITLE: CI Test Suite
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
See Metadata Syntax for outputs name description
The problem with the current accepted answer is that the result for the step will always be success since the test execution result is being masked by the echo command.
This modification to the last line should work in preserving the original exit status:
mix test 2>&1 | tee test.log
result_code=${PIPESTATUS[0]}
echo "::set-output name=mix-test::$(cat test.log)"
exit $result_code
I made an action with the same interface as run that stores stdout and stderr in output variables to maybe simplify some cases like this:
- name: Run tests
uses: mathiasvr/command-output#v1
id: tests
with:
run: |
mix compile --warnings-as-errors
mix format --check-formatted
mix ecto.create
mix ecto.migrate
mix test
- name: Slack Notification
uses: rtCamp/action-slack-notify#master
env:
SLACK_MESSAGE: ${{ steps.tests.outputs.stdout }}
SLACK_TITLE: CI Test Suite
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
I just wanted to add #smac89's solution was helpful but didn't quite work for me. I'm using a different Slack action (pullreminders/slack-action) to build more specific content. I found that I was getting single-quotes where each newline was, and my leading spaces on each line were also being truncated. After reading https://github.com/actions/toolkit/issues/403 and playing around, I found that in my case, I needed newlines to actually be escaped in the output (a literal \n), so I replaced \n characters with \\n. Then, I replaced regular space characters with a Unicode 'En Space' character.
Here's what worked:
Bash Run Step:
Tools/get-changed-fields.sh src/objects origin/${{ env.DIFF_BRANCH }} > changed-fields.out
output="$(cat changed-fields.out)"
output="${output//$'\n'/\\n}"
output="${output// / }" # replace regular space with 'En Space'
echo "::set-output name=changed-fields-output::$output"
Slack Notification Step:
- name: Changed Fields Slack Notification
if: ${{ success() && steps.summarize-changed-fields.outputs.changed-fields-output != '' && steps.changed-fields-cache.outputs.cache-hit != 'true' }}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
uses: pullreminders/slack-action#master
with:
args: '{\"channel\":\"${{ env.SUCCESS_SLACK_CHANNEL }}\",\"attachments\":[{\"color\":\"#36a64f\",\"title\":\"Changed Fields Report:\",\"author_name\":\"${{ github.workflow }} #${{ github.run_number }}: ${{ env.BRANCH }} -> ${{ env.TARGET_ORG }} (by: ${{ github.actor }})\",\"author_link\":\"${{ github.server_url }}/${{ github.repository }}/runs/${{ github.run_id }}\",\"text\":\"```\n${{ steps.summarize-changed-fields.outputs.changed-fields-output }}\n```\"}]}'