Skip to content

Instantly share code, notes, and snippets.

@localheinz
Last active September 8, 2019 09:42
Show Gist options
  • Save localheinz/57a6ad564251207a430bee50c87c289d to your computer and use it in GitHub Desktop.
Save localheinz/57a6ad564251207a430bee50c87c289d to your computer and use it in GitHub Desktop.
GitHub Actions Feature Request: Run job only on `push` to configured branch
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
on:
push:
branch: master
name: Docker
jobs:
docker:
name: "Docker"
runs-on: ubuntu-latest
strategy:
matrix:
image:
- php-7.2
- php-7.3
- php-7.3-with-xdebug
steps:
- name: Checkout
uses: actions/checkout@master
- name: Docker Login
uses: actions/docker/login@master
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD : ${{ secrets.DOCKER_PASSWORD }}
- name: "Build and tag Docker image"
uses: actions/docker/cli@master
with:
args: build --tag localheinz/php-library-template-${{ matrix.image }} .docker/${{ matrix.image }}
- name: "Push Docker image"
uses: actions/docker/cli@master
with:
args: push localheinz/php-library-template-${{ matrix.image }}
- name: Docker Logout
uses: actions/docker/cli@master
env:
args: logout
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
on:
- push
name: Docker
jobs:
docker:
name: "Docker"
runs-on: ubuntu-latest
strategy:
matrix:
image:
- php-7.2
- php-7.3
- php-7.3-with-xdebug
steps:
- name: Checkout
if: github.ref == 'refs/heads/master'
uses: actions/checkout@master
- name: Docker Login
if: github.ref == 'refs/heads/master'
uses: actions/docker/login@master
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD : ${{ secrets.DOCKER_PASSWORD }}
- name: "Build and tag Docker image"
if: github.ref == 'refs/heads/master'
uses: actions/docker/cli@master
with:
args: build --tag localheinz/php-library-template-${{ matrix.image }} .docker/${{ matrix.image }}
- name: "Push Docker image"
if: github.ref == 'refs/heads/master'
uses: actions/docker/cli@master
with:
args: push localheinz/php-library-template-${{ matrix.image }}
- name: Docker Logout
if: github.ref == 'refs/heads/master'
uses: actions/docker/cli@master
env:
args: logout
@localheinz
Copy link
Author

localheinz commented Sep 8, 2019

It would be very nice if it were possible to further configure specifics of a push event that trigger a job.

Here's a diff:

--- docker-before.yml	2019-09-08 09:23:48.000000000 +0200
+++ docker-after.yml	2019-09-08 09:24:00.000000000 +0200
@@ -1,7 +1,8 @@
 # https://help.github.com/en/categories/automating-your-workflow-with-github-actions

 on:
-  - push
+  push:
+    branch: master

 name: Docker

@@ -20,30 +21,25 @@

     steps:
       - name: Checkout
-        if: github.ref == 'refs/heads/master'
         uses: actions/checkout@master

       - name: Docker Login
-        if: github.ref == 'refs/heads/master'
         uses: actions/docker/login@master
         env:
           DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
           DOCKER_PASSWORD : ${{ secrets.DOCKER_PASSWORD }}

       - name: "Build and tag Docker image"
-        if: github.ref == 'refs/heads/master'
         uses: actions/docker/cli@master
         with:
           args: build --tag localheinz/php-library-template-${{ matrix.image }} .docker/${{ matrix.image }}

       - name: "Push  Docker image"
-        if: github.ref == 'refs/heads/master'
         uses: actions/docker/cli@master
         with:
           args: push localheinz/php-library-template-${{ matrix.image }}

       - name: Docker Logout
-        if: github.ref == 'refs/heads/master'
         uses: actions/docker/cli@master
         env:
           args: logout

Currently, the job described in docker-before.yml will run on every push event, and even though we use if conditions to skip steps in the job

  • the job is triggered
  • required actions (here: actions/checkout and actions/docker/cli) are build

We could do better and save resources if the job were only triggered when we push to master (or any other specified branch).

What do you think, @nat?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment