So I am just trying to learn how to use github workflow actions and made this simple starter test:
name: Basic test push
on:
push:
branches:
- 'autoupdate'
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo Test One Worked!
However, I get an error:
Invalid workflow file
You have an error in your yaml syntax on line 10'
Anyone know why?
You're indenting the steps, but it should be on the same level as runs-on:
name: Basic test push
on:
push:
branches:
- 'autoupdate'
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo Test One Worked!
Related
I have a workflow with two jobs, tests and build. tests job I want to run when any branch gets pushed into but it should not get run when I do merge from any branch to another. Aim is to prevent long running tests job during merge because it already gets executed when we push code.
name: PR test
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Run tests
run: |
echo "tests run"
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Build
run: |
echo "Build"
When I add if condition under job tests:
github.ref != 'refs/heads/develop' && github.event.pull_request.merged == false
...it helps to run tests job when I push in any branch and prevents tests job when I merge into develop but it also prevents when I do direct change and push in develop branch while I want it to run on direct push.
Note: Answer should be with native github workflow job definition condition, it should not be under steps. Also answer shouldn't based on commiter name. You can use this repo for testing if you wish https://github.com/hk1313/prtest
It's possible to add an if statement that would check if it's a push or not, for example:
name: PR test
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
tests:
if: |
!(github.event_name == 'pull_request' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Run tests
run: |
echo "tests run"
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Build
run: |
echo "Build"
I have this deveopment.yml in .github/workflows:
name: Development
on:
push:
branches:
- dev
paths-ignore:
- '**.md'
jobs:
test:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: "Checkout code"
uses: actions/checkout#v2
- name: "Test Suite"
uses: ./.github/actions/test_suite
lint:
[...lint stuff]
db_migrate:
[...db migrate stuff]
[etc]
[etc]
I would expect this workflow to only fire when I push to the branch named dev
I also have a pull_request.yml workflow. It's similar:
on:
pull_request:
branches:
- '*'
paths-ignore:
- '**.md'
jobs:
test:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: "Checkout code"
uses: actions/checkout#v2
- name: "Test Suite"
uses: ./.github/actions/test_suite
lint:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: "Checkout code"
uses: actions/checkout#v2
- name: "Lint Suite"
uses: ./.github/actions/linting
I would expect this to run on any pull request to any branch, before it merges.
So far so good. If I make a PR from a feature branch into dev, it runs test and lint. When I merge, it runs the development.yml actions like db_migrate.
My question is why when I make a PR of dev into prod, it seems to run the development.yml actions. As soon as the PR is opened, it runs db_migrate. Why is that, and how do I make it not do that?
There is a production.yml but that's set to on release, so I don't think that's relevant here
on:
release:
types: [published]
paths-ignore:
- '**.md'
I've been staring at the docs but I'm having a slow brain morning. It's a little tricky to debug this because it's tightly coupled to github and two important branches.
I have two very simple GitHub workflows for testing proposes. deploy-test-first which runs always on pull_request and deploy-test-second which should run on completion of deploy-test-first
The deploy-test-second however never runs. What Am I missing? How do I make it work?
name: deploy-test-first
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
deploy-test-first:
runs-on: ubuntu-20.04
steps:
- name: deploy-test-first
run: echo 'deploy-test-first'
name: deploy-test-second
on:
workflow_run:
workflows: [deploy-test-first]
types: [completed]
branches: [develop]
jobs:
test-deploy-second:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-20.04
steps:
- name: test-deploy-second
run: echo 'deploy-test-second'
Both workflows are suppose to run on non-default branch develop in PR.
Workflow that call reusable one:
name: Build only workflow
on:
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- uses: ./.github/workflows/build_job
with:
TARGET: lol
secrets: inherit
./.github/workflows/build_job folder contain action.yml file:
name: Build job
on:
workflow_call:
inputs:
TARGET:
required: true
type: string
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: webfactory/ssh-agent#v0.5.4
with:
ssh-private-key: ${{secrets.SSH_KEY}}
- run: echo "hello"
Error: The workflow is not valid. .github/workflows/build_workflow.yml (Line: 16, Col: 9): Unexpected value 'secrets'
You're including the reusable workflow as a step, but a reusable workflow is an entire job not just a step.
Therefore, what you need is:
jobs:
my-job:
uses: ./.github/workflows/my-reusable-workflow.yaml
And then, since you can't do the checkout from outside anymore, you're going to have to add the checkout to your reusable workflow.
Also see this other answer of mine on the distinction between composite actions and reusable workflows.
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