Run particular jobs while creating pull request in githubactions - github

I have implemented ci/cd using GitHub actions. In ci/cd I have three jobs are there when I want to release a tag I want to build these three jobs and when I raise a pull request to a particular branch only two jobs should be executed for health check purposes. for example, I have a feature branch I want to merge this feature branch to the devel branch. when I raise a PR should be run only two jobs. how can I achieve this? below is my sample code.
name: CI
on:
pull_request:
branches:
- master
- devel
push:
tags:
- '*'
jobs:
build:
name: build
runs-on: self-hosted
steps:
--------------
deploy:
name: deploy
runs-on: self-hosted
steps:
------------
automation-test:
name: test
runs-on: self-hosted
steps:
------------
here when I raise a PR I want to run build and automation-test jobs.

You have two options here:
Have separate workflow files that run for the right branches.
Use conditionals in your job
The first option is likely the one you want. The only problem here is if the output from one job is used in another, but it doesn't sound like that's the case for you. I would recommend you simply break out your yaml workflows into two separate workflows:
name: CI
on:
pull_request:
branches:
- master
push:
tags:
- '*'
jobs:
build:
name: build
runs-on: self-hosted
steps:
--------------
deploy:
name: deploy
runs-on: self-hosted
steps:
------------
automation-test:
name: test
runs-on: self-hosted
steps:
------------
name: PR Builder
on:
pull_request:
branches:
- devel
jobs:
whatever_testing_jobs_you_like:
The second option might look something like this:
name: CI
on:
pull_request:
branches:
- master
push:
tags:
- '*'
jobs:
build:
name: build
runs-on: self-hosted
steps:
--------------
deploy:
if: "github.ref != devel" # you might tweak the condition based on your needs
name: deploy
runs-on: self-hosted
steps:
------------
automation-test:
name: test
runs-on: self-hosted
steps:
------------
These context values / conditions are well documented

Related

Github action runs on pr unexpectedly

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.

Dependencies Between Workflows on Github Actions not working

I have two workflows and i do from this manual - https://docs.github.com/en/github-ae#latest/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_runbranchesbranches-ignore but dependent workflow not starting..
name: Tests
on:
push:
branches:
- '**' # matches every branch
- '!main' # excludes master
jobs:
setup-tests-job:
runs-on: ubuntu-latest
and dependent workflow
name: Build
on:
workflow_run:
workflows:
- 'Tests'
branches:
- '**'
types:
- completed
jobs:
setup-tests-job:
runs-on: ubuntu-latest
Maybe i do smthg wrong?

Invalid workflow file for GitHub actions

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!

How to run a job based on a tag-name

I have the following condition:
name: Build Image
on:
push:
pull_request:
workflow_dispatch:
jobs:
build-image: (This should be only if the tag contains "azure")
name: Build Image Job
runs-on: [ self-hosted ]
steps:
- uses: actions/checkout#v2
- name: Run Build Step
run: |
ANSIBLE_VERSION=$(cat VERSION)
docker images
jobs:
build-image: (This will be for everything else)
name: Build Image Job
runs-on: [ self-hosted ]
steps:
- uses: actions/checkout#v2
- name: Run Build Step
run: |
ANSIBLE_VERSION=$(cat VERSION)
docker images
How can that be achieved? How do you reference the tags inside a job?
How do you the variable and make an if rule out of it?
You can use regex in your on config to trigger a workflow based on a tag name. For example:
on:
push:
tags:
- '*azure*'
At the level of a job or a step, you can use a contains() expression:
jobs:
build-for-azure-tag:
name: build-for-azure-tag
runs-on: ubuntu-latest
if: github.ref_type == 'tag' && contains(github.ref_name, 'azure')

workflow_run only on push

I have the following workflow:
name: ci
on:
push:
branches: [main, develop]
pull_request:
jobs:
....
name: deploy
on:
release:
types: [released]
workflow_run:
workflows: ["ci"]
branches: [develop]
types: [completed]
The problem I have is that when I create a PR from the develop to the main branch it also runs the deploy workflow. Is there any way to prevent this? and just run the deploy workflow on push after the CI finishes?
Thanks