Caching artifacts in GitHub actions using runner controller

Viewed 207

I want to set up self-hosted runners on a k8s cluster using actions-runner-controller.

My question is, given that as per the official docs, persistent runners are not recommended

Although not generally recommended, it’s possible to disable the passing of the --ephemeral flag by explicitly setting ephemeral: false in the RunnerDeployment or RunnerSet spec. When disabled, your runner becomes “persistent”.

how can one leverage artifact caching when using this controller?

Where will the cache content will be stored in the k8s cluster, given that containers are ephemeral?

1 Answers

If you are not using the enterprise version, the caches will be handled by Github itself. I came across some similar problems at my self-hosted runner to create a cache for nodeJs, VueJs, and Java. Here's what I did:

VueJs (moving dist folder) (note the actions/upload-artifact@v3)

name: CI
on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]
  workflow_dispatch:
jobs:
  build-web:
    runs-on: self-hosted
    container:
      image: node:14
    steps:
      - uses: actions/checkout@v3
      - name: Build shc-web
        run: |
          yarn config set cache-folder .yarn
          yarn
          yarn run build
      - uses: actions/upload-artifact@v3
        with:
          name: dist-folder
          path: dist/
  registry-web:
    runs-on: self-hosted
    needs: ['build-web']
    steps:
      - uses: actions/checkout@v3
      - uses: actions/download-artifact@v3
        with:
          name: dist-folder
          path: dist/
      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Registry on AWS repository
        env:
          REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          REPOSITORY: shccp
        run: |
          docker build -t $REGISTRY/$REPOSITORY:3.1.x-$GITHUB_RUN_ID .
          docker push $REGISTRY/$REPOSITORY:3.1.x-$GITHUB_RUN_ID

Also, I used two different jobs to handle the build. It could be done in only one so there was no need to upload/download the dist. Actually, that was precisely what I had to do in the NodeJs action. The node_modules is just too big to be uploaded.

NodeJS:

name: CI
on:
  push:
    branches: [ "stage" ]
  pull_request:
    branches: [ "stage" ]

  workflow_dispatch:

jobs:
  ci-api:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 14
      - name: Build api
        run: npm install
      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Registry on AWS repository
        id: registry-aws
        env:
          REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          REPOSITORY: shcapi
        run: |
          docker build -t $REGISTRY/$REPOSITORY:3.1.x-$GITHUB_RUN_ID .
          docker push $REGISTRY/$REPOSITORY:3.1.x-$GITHUB_RUN_ID
          echo "::set-output name=image-tag::$REGISTRY/$REPOSITORY:3.1.x-$GITHUB_RUN_ID"

No cache is needed once it is done in a single job. That is a pretty nice feature of Github actions btw.

The Java cache, on the other hand, is handled by the following action:

name: CI
on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

  workflow_dispatch:

jobs:
  ci-etlv4:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: adopt-openj9
          java-version: 8
          cache: 'maven'
      - uses: stCarolas/setup-maven@v4.4
        with:
          maven-version: 3.8.2
      - name: Build ETLv4
        run: |
          echo ${{ secrets.SETTINGS_BASE64 }} | base64 -d > settings.xml
          mvn --settings settings.xml --global-settings settings.xml clean package -DskipTests=true
      - uses: docker/login-action@v2
        with:
          registry: "iad.ocir.io"
          username: ${{ secrets.OCI_REGISTRY_USER }}
          password: ${{ secrets.OCI_REGISTRY_PASSWORD }}
      - uses: docker/setup-qemu-action@v2
      - uses: docker/setup-buildx-action@v2
        with:
          driver: docker
      - uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          tags: XXXXX

The actions/setup-java@v3 can deal with the maven/gradle caches.

Hope it helps.

Related