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

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 }}

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".

How can I run a GitHub Actions job based on a complex condition (specific label removed)?

I have two reusable workflows to deploy and destroy GCP resources, which I call from one workflow based on different conditions.
One workflow creates infra and is triggered when the label preview is added to a PR:
on:
pull_request:
types: [opened, reopened, labeled]
jobs:
create-infrastructure:
if: ${{ contains( github.event.pull_request.labels.*.name, 'preview') }}
# Call to a reusable workflow here
The second workflow I need to trigger when the PR is closed or when a specific label is removed; I tried this:
on:
pull_request:
types: [ closed, unlabeled ]
jobs:
destroy_preview:
if: ${{ contains( github.event.pull_request.labels.*.name, 'preview') }}
uses: myrepo/.github/workflows/preview-app-destroy.yml#v0.3.6
with:
project_id: xxx
I don't know how to define unlabeled for a specific label. It would be great if someone has any idea.
The pull request webhook payload doesn't contain the removed label, as far as I can tell, but you can fetch the list of issue events (which work for pull requests, too), filter by unlabeled events, and then look at the label name of the last one.
Using the GitHub CLI in a run step, that might look something like this:
name: Preview removed workflow
on:
pull_request:
types:
- unlabeled
jobs:
destroy_preview:
runs-on: ubuntu-20.04
steps:
- name: Check if "preview" was removed
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
pr=${{ github.event.number }}
label=$(gh api "repos/$GITHUB_REPOSITORY/issues/$pr/events" \
--jq 'map(select(.event == "unlabeled"))[-1].label.name')
if [[ $label == 'preview' ]]; then
echo "The 'preview' label has been removed"
fi
where you'd replace the echo with your infrastructure commands.
Now, if you want to call a reusable workflow when that specific label is removed, you have to somehow find a way to add a condition to the job where the reusable workflow is called.
One option is to make two jobs, one to check the condition and setting the result as a job output. The other job is set up as depending on the first one, and its if condition checks if the output was set to true.
This would look something like this (omitting the name and trigger, as they are identical to above):
jobs:
checklabel:
runs-on: ubuntu-20.04
outputs:
waspreview: ${{ steps.check.outputs.preview }}
steps:
- name: Check if "preview" was removed
id: check
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
pr=${{ github.event.number }}
label=$(gh api "repos/$GITHUB_REPOSITORY/issues/$pr/events" \
--jq 'map(select(.event == "unlabeled"))[-1].label.name')
if [[ $label == 'preview' ]]; then
echo "::set-output name=preview::true"
fi
destroy_preview:
needs: checklabel
if: needs.checklabel.outputs.waspreview
uses: myrepo/.github/workflows/preview-app-destroy.yml#v0.3.6
with:
project_id: xxx

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

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'

How to compare a file changes since last action in github actions

In gitlab we have support for CI_COMMIT_BEFORE_SHA and CI_COMMIT_SHA
Whereas in github we only have support for GITHUB_REF which holds SHA of current commit that triggered this action.
My requirement is to find if a particular file changed since last Action execution
Found a way to solve this using event payload - see push event payload:
in .github/workflows/release.yaml
name: release
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: deploy
run: sh deploy.sh
env:
CI_COMMIT_BEFORE_SHA: ${{ github.event.before }}
CI_COMMIT_SHA: ${{ github.event.after }}
in bin/deploy.sh
HAS_DESIRED_CHANGES=`git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep -E 'path/to/file1|path/to/dir2'`
echo $HAS_DESIRED_CHANGES
HAS_DESIRED_CHANGES=`echo ${#HAS_DESIRED_CHANGES}`
if ! [ $HAS_DESIRED_CHANGES -eq 0 ]
then
echo "Has desired changes, proceed performing necessary actions"
else
echo "No desired changes. Skipping...."
fi

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.