Github Actions outcome is not retrieved - github

I'm attempting to run the following .yml with GitHub Actions, but the outcome from the steps is not retrieved or null.
The documentation for outcome is here.
name: run test for outcome
on:
workflow_dispatch:
jobs:
run-test-for-outcome:
runs-on: ubuntu-latest
steps:
- name: step1
id: step1
continue-on-error: true
run: |
echo step 1
- name: test
run : echo ${{ steps.step1.outcome }}
However, if I run the following:
name: run test for outcome
on:
workflow_dispatch:
jobs:
run-test-for-outcome:
runs-on: ubuntu-latest
steps:
- name: step1
id: step1
continue-on-error: true
run: |
echo step 1
- name: test
run : echo ${{ steps.step1.success }}
the value returned is true.
Why is that? Why isn't outcome working as intended?

I actually found the problem
I was running the workflow locally using https://github.com/nektos/act and that appears to be the problem.
When I did test it under Github, the outcome and every step context is working as intended.
Thank you.

Related

git steps command to get output from action

I would like to get the success/failure output of this action aws-actions/configure-aws-credentials#v1 in my next job. As per the action-output the output is aws-account-id.
hence I created below workflow, doesn't matter what I do, I don't seems to catch the output. My question is how to catch the error/success message for this action.
Right now I'm getting empty even if the job success/failed. Is there any way I can get the output of the steps?
jobs:
deploy_infra:
name: Deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
outputs:
output1: ${{ steps.cac.outputs.aws-account-id }}
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: AWS Role
id: cac
uses: aws-actions/configure-aws-credentials#v1
with:
role-to-assume: arn:aws:iam::${{ github.event.inputs.account_id }}:role/assume-role
aws-region: ${{ github.event.inputs.aws_region }}
mask-aws-account-id: false
alert_job:
if: always()
needs: deploy_infra
name: Testing the status
runs-on: ubuntu-latest
steps:
- run: echo ${{needs.job1.outputs.output1}}

GitHub Actions using output from a previous parallel matrix job in a new one

I have configured 2 jobs that I want to run. The first job does the actual building of my software on multiple configurations. On my second job I want to wait until the first set of jobs complete, and then post to a slack channel the summary of my job.
I found a method by using needs to accomplish this task however I am stuck when it comes to actually obtaining the status of each individual task and not just a global status.
For example I have something as follows
name: Build and test
jobs:
build-and-test:
strategy:
matrix:
config:
- name: 'Ubuntu 18.04'
runner: 'ubuntu-18.04'
id: 'u18'
- name: 'Ubuntu 20.04'
runner: 'ubuntu-20.04'
id: 'u20'
fail-fast: false
runs-on: ${{ matrix.config.runner }}
steps:
- name: Step 1
id: step1
run: |
echo "To be filled in"
- name: Step 2
id: step2
run: |
echo "To be filled in"
webhook-update:
needs: build-and-test
if: always()
runs-on: [ubuntu-20.04]
steps:
- name: Send webhook update for all jobs
run: |
# In the future will push to slash, but for now using echo
echo ${{ needs.build-and-test.results }}
# Ideally I want to also have access to something like u18.results and u20.results

How to set and change GitHub workflow output

I'm trying to figure out how to set an output, change the output, then use it it the following job. I've tried making this as simple as possible yet the last echo returns true. The documentation doesn't provide examples so any insight or advice would be great.
job-1:
runs-on: ubuntu-latest
outputs:
compare: true
steps:
- name: set output
run: |
echo "::set-output name=compare::false"
job-2:
runs-on: ubuntu-latest
# require the first job to have ran
needs: compare
steps:
- name: echo job-1 compare output
run: |
echo ${{ needs.job-1.outputs.compare }}
I learned you have to declare the value of compare in run and use an id
job-1:
runs-on: ubuntu-latest
outputs:
compare: ${{ steps.job_id.outputs.compare}}
steps:
- name: set output to false
id: job_id
run: |
echo "::set-output name=compare::false"
job-2:
runs-on: ubuntu-latest
# require the first job to have ran
needs: compare
steps:
- name: echo job-1 compare output
run: |
echo ${{ needs.job-1.outputs.compare }}

How do i run GithHub actions .yaml files in certain order?

I have two .yaml files for my GitHub actions. I need the second file to be executed only after first. How can I achieve this if the jobs are both in other files?
You could use the workflow_run syntax for Github Actions workflows.
In the example below, a workflow with the following trigger will only run when the workflow named Workflow Tester is completed (you could also started them in sequence using the requested type).
on:
workflow_run:
workflows: ["Workflow Tester"]
types: [completed] #requested
Note that when using the trigger that way (with the completed type) you can also check the previous workflow, and perform different jobs depending on the workflow conclusion.
Example
jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
[...]
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
[...]
I've tested this syntax in this workflow if you want to have a look and check the workflow runs in the repo Actions tab.
There is a feature called Reusing Workflows which can be used.
Example:
workflow1.yaml
name: Job1
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Run a one-line script
run: echo Job1 Executed!
workflow2.yaml
name: Job2
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Run a one-line script
run: echo Job2 Executed!
demo1.yaml(Calling Workflow)
name: Demo1
on:
push:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
call-workflow1:
uses: ./.github/workflows/workflow1.yaml
call-workflow2:
if: ${{ always() }} #This will make your workflow2 executed even if workflow1 fails, remove this, if you want to run this only on success of workflow1
needs: call-workflow1
uses: ./.github/workflows/workflow2.yaml
Sample
Reference -
Job Link
Repo

How do to run the next github action step even if the previous step failed, while still failing the job?

This question is similar to How to run a github-actions step, even if the previous step fails, while still failing the job but the accepted answer does not help me because the it creates an additional job.
What i am trying to accomplish below is
When the test-app (step2) passes; the test-clean step should run and the github action workflow returns success.
When the test-app (step2) fails; the test-clean, action-slack and fail-action steps should run. The github action workflow returns fails.
How do I fix the below code to make it happen?
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: test-app
run: ./gradlew test
- name: test-clean
run: some cleanup that should run always
- name: action-slack
if: ${{ step2.result != 'success' }}
uses: 8398a7/action-slack#v3
with:
status: ${{ step2.result }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: fail-action
run: |
if ${{ step2.result != 'success' }}; then
exit 1
fi
You can use the status check functions to know the status of previous steps. If you don't include such a function, if: success() && ... is implied. This means that a job will not run when previous jobs failed unless you use always() or failure() in the if clause.
To address the result of previous steps, you can use the steps context like steps.<id>.outcome (before continue-on-error is applied) or steps.<id>.conclusion (after continue-on-error is applied).
Here is a working example combining everything:
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
# Mock to test workflow
- name: Test app
id: test-app # will be referenced later
run: |
echo "Testing app (randomly fails)"
if [[ $(($RANDOM % 2)) == 0 ]]; then exit 0; else exit 1; fi
# runs always
- name: test-clean
if: always()
run: echo "Cleanup after tests"
# runs if previous jobs failed and test-app was not successful (failure/cancelled)
- name: action-slack
if: failure() && steps.test-app.outcome != 'success'
run: |
echo "Run action-slack"
echo "Result of test-app was '${{ steps.test-app.outcome }}'"
PS: The answer in the other question does not add an additional job but includes an example on how to apply it across jobs. However, that answer does not address your exact use case but it could have helped you by giving some pointers.