01 HACKTOBERFEST 2019 · MICROSOFT, BENGALURU
GitHub Actions
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
OCT 2018 Announced at GitHub Universe, in limited beta. Workflows were written in HCL, in a main.workflow file.
8 AUG 2019 CI/CD arrives: YAML workflows, Linux, macOS and Windows runners, matrix builds and live logs.
30 SEP 2019 HCL workflows stop running. Everything is YAML now.
13 NOV 2019 General 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
WORKFLOW A YAML file in .github/workflows/. A repo can have several: build, release, and so on.
EVENT What starts it: a push, a pull_request, a new issue, or a schedule.
JOB A set of steps that runs on one machine: ubuntu-latest, windows-latest or macos-latest.
STEP Either a shell command (run) or an action (uses).
ACTION A 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.
git push 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.
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.
25
TRIAGE Label new issues and greet first-time contributors.
RELEASES Tag a version and let a workflow build and publish it.
SCHEDULES Nightly builds and reports with on: schedule and cron syntax.
PACKAGES Publish to GitHub Package Registry, also in beta.
27
01 Open the Actions tab. GitHub suggests a workflow for your project’s language.
02 Commit it to .github/workflows/.
03 Push, 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.