ISPEAKCODE.COM

TALK · HACKTOBERFEST 2019 · MICROSOFT, BENGALURU · 26 OCT 2019

GitHub Actions

An official Hacktoberfest 2019 event at Microsoft Bengaluru, two weeks before GitHub Actions left beta. CI/CD that lives next to your code: workflows, jobs and steps, a matrix build, and a Docker image built and pushed to Docker Hub on every push.

OFFICIAL HACKTOBERFEST 2019 EVENT 31 SLIDES RUN A MATRIX BUILD
Jump to the matrix build

The original slides didn’t survive. This deck is rebuilt from the talk’s demo repo and my October 2019 articles, and shows GitHub Actions as it was that month.

Nabil at the lectern in a Microsoft training room in Bengaluru; the screen behind him shows the 'Github Actions, 26th Oct 2019' title slide with the Microsoft and MayaData logos, and attendees sit at desks with laptops open.
On stage at Microsoft, Bengaluru
Group photo of about sixty attendees standing at the front of the room, in front of a screen showing MayaData's Hacktoberfest 2019 page.
Everyone, after the sessions

THE MEETUP

An official Hacktoberfest 2019 event

When
Sat 26 Oct 2019
Where
Microsoft, Bengaluru
Event
Hacktoberfest 2019, the month-long open source celebration
01

HACKTOBERFEST 2019 · MICROSOFT, BENGALURU

GitHub Actions

02

PART 1

Why automate?

03

Every push should be tested, built and shipped the same way.

04

CI/CD usually means another service, another login and another config file.

05

What if it lived right next to your code?

06

PART 2

What is GitHub Actions?

07

Automation built into GitHub: when something happens in your repo, run a workflow.

08

How we got here

  1. OCT 2018Announced at GitHub Universe, in limited beta. Workflows were written in HCL, in a main.workflow file.
  2. 8 AUG 2019CI/CD arrives: YAML workflows, Linux, macOS and Windows runners, matrix builds and live logs.
  3. 30 SEP 2019HCL workflows stop running. Everything is YAML now.
  4. 13 NOV 2019General availability at GitHub Universe. Until then it’s in beta, and free for everyone.
09

Free for public repositories.

Private repositories get included minutes on every plan, then pay as you go. Self-hosted runners are free.

10

PART 3

The building blocks

11
  • WORKFLOWA YAML file in .github/workflows/. A repo can have several: build, release, and so on.
  • EVENTWhat starts it: a push, a pull_request, a new issue, or a schedule.
  • JOBA set of steps that runs on one machine: ubuntu-latest, windows-latest or macos-latest.
  • STEPEither a shell command (run) or an action (uses).
  • ACTIONA reusable piece of a workflow, written in JavaScript or as a Docker container. Find them in the Marketplace.
12

Hello, Hacktoberfest

.GITHUB/WORKFLOWS/HELLO.YML
name: Hello
on: push

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - run: echo "Hello, Hacktoberfest!"

Save it as .github/workflows/hello.yml, push, and open the Actions tab.

13

Each job runs in a fresh instance of a virtual environment.

Nothing leaks from one run to the next. Jobs run in parallel unless you say needs.

14

PART 4

CI: test every push

15

A Node.js matrix

.GITHUB/WORKFLOWS/NODEJS.YML
name: Node CI
on: [push]

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node_version: [8, 10, 12]
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node_version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node_version }}
    - name: npm install, build and test
      run: |
        npm install
        npm run build --if-present
        npm test

One job definition, nine jobs: three Node.js versions on three operating systems.

16

Push a commit

The matrix above, run for you. Every combination gets its own fresh machine, and they all run at once. A simulation; no real runners are involved.

9 jobs waiting for a push.
17

Live logs, streamed while the job runs.

And every line has a link you can paste into a pull request.

18

PART 5

CD: ship it

19

What we’re shipping

DOCKERFILE
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python
ADD hello.py /home/hello.py
CMD ["/home/hello.py"]
ENTRYPOINT ["python"]

# hello.py
print ("Hello World");

The smallest image that proves the pipeline works: Python on Ubuntu, saying hello.

20

Build and push it to Docker Hub

DOCKER-GITHUBACTION · MAIN.YML
name: Build and Publish Docker
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Publish to Docker Repository
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: ispeakcode/docker-githubaction
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

The demo repo, docker-githubaction, exactly as it was on talk day. A community action from the Marketplace does the build and the push.

21

Secrets stay in Settings, not in YAML.

Add DOCKER_USERNAME and DOCKER_PASSWORD under the repo’s Settings → Secrets, then read them as ${{ secrets.DOCKER_PASSWORD }}.

22

Every push, a fresh image

TERMINAL
$ docker run ispeakcode/docker-githubaction
Hello World

Anyone can pull it. No laptop, no manual docker push.

23

The same idea ships mobile apps.

Test a Flutter app, build the APK and attach it to a GitHub Release, all from one workflow.

24

PART 6

Not just CI/CD

25
  • TRIAGELabel new issues and greet first-time contributors.
  • RELEASESTag a version and let a workflow build and publish it.
  • SCHEDULESNightly builds and reports with on: schedule and cron syntax.
  • PACKAGESPublish to GitHub Package Registry, also in beta.
26

PART 7

Get started

27
  1. 01Open the Actions tab. GitHub suggests a workflow for your project’s language.
  2. 02Commit it to .github/workflows/.
  3. 03Push, and watch it run.
28

A CI workflow for a project you use is a great Hacktoberfest pull request.

29

Demo

Push to the repo, watch the workflow build the image, then pull it from Docker Hub.

30

Questions?

31

Thank you!