Automatically Deployable Self-Hosted Github Actions Runner via Docker & Ansible

#ansible #github actions #self-hosted #ubuntu

 

I often work on Android projects, and find myself wanting to run through tests on real devices. I have a spare computer running Ubuntu 20.04 server and have a bunch of phones attached to it via USB. I also run a bunch of tests via github actions.

You can manually set all this up - but then if you clobber the server at any point, you have a bunch of manual work to do again. Instead you can setup ansible to deploy all this stuff automatically. You just need to setup a github API token: https://github.com/myoung34/docker-github-actions-runner. Also highly recommend if you're going to store the API token in a repo to use something like gpg to encrypt it first.

Ansible tasks:

# https://github.com/nickjj/ansible-docker
- name: Ensure docker is installed on the target system
  include_role:
    name: nickjj.docker
    apply:
      become: true
			
# https://github.com/myoung34/docker-github-actions-runner
- name: Github Actions Runner
  tags: gh-actions
  vars:
    ansible_python_interpreter: "/usr/bin/env python3-docker"
  docker_container:
    name: github-runner
    image: myoung34/github-runner:latest
    pull: true
    volumes:
      - "/dev/bus/usb:/dev/bus/usb"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/tmp/github-runner:/tmp/github-runner"
    restart_policy: unless-stopped
    privileged: true
    env:
      ACCESS_TOKEN: "{{ github_access_token }}"
      RUNNER_NAME: "{{ github_runner_name }}"
      RUNNER_WORKDIR: /tmp/github-runner
      RUNNER_GROUP: default
      REPO_URL: "{{ github_repo }}"
      LABELS: "self-hosted"
      ORG_RUNNER: "false"

All thats left is to setup the variables with your access token, whatever you want to call the runner, and what repo you want to use it for. Then in your Android project workflows you can do something like this (note the runs-on must match the LABELS from above:

android_checks:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - name: set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Setup Android SDK
        uses: android-actions/setup-android@v2.0.6
      - name: lint
        run: ./gradlew app:lint
      - name: app lint results
        uses: yutailang0119/action-android-lint@v1.1.0
        with:
          xml_path: app/build/reports/lint/report.xml
      - name: checkstyle
        run: ./gradlew app:checkstyle
      - name: junit5 instrumented tests (with jacoco coverage reports))
        run: ./gradlew app:connectedCheck && ./gradlew app:jacocoTestDebugUnitTestReport
      - uses: actions/upload-artifact@v2
        with:
          name: app-reports
          path: app/build/reports

Note, the connectedCheck is when the instrumented tests run on the devices themselves. You may have to manually approve on the devices the first time it runs so they trust the computer deploying the tests to them, and after that its good to run again and again.


Comments (0)


Leave a Comment

Log in to leave a comment.

Most Recent Post

Building a Better GitHub Stats Card

I've had a GitHub profile stats card for a while now, but I was never fully happy with it. The existing tools each had trade-offs that bugged me, so I ended up building my own by combining the best parts of three different projects. Here's how it went. The Problem There are a few popular projects fo...