Search code examples
ryamlgithub-actions

How to change GitHub Actions OS from macOS to Windows or Ubuntu?


I'm currently using the macOS os version for my Github Actions but it's very consuming on my minutes thresholds as it has a 10x cost on the monthly limit

I would like to switch to Ubuntu or Windows but I'm not quite sure about how to change my YML file.

Here is my actual YML file of one of my workflow:

# Hourly scraping
name: amazonR Polite Price Checker

# Controls when the action will run.
on:
  schedule:
    - cron: '0 0 * * 1,4'

jobs:
  autoscrape:
    # The type of runner that the job will run on
    runs-on: macos-latest

    # Load repo and install R
    steps:
    - uses: actions/checkout@master
    - uses: r-lib/actions/setup-r@master

    # Set-up R
    - name: Install packages
      run: |
        R -e 'install.packages("tidyverse")'
        R -e 'install.packages("tibble")'
        R -e 'install.packages("openxlsx")'
        R -e 'install.packages("gdata")'
        R -e 'install.packages("lubridate")'
        R -e 'install.packages("rvest")'
        R -e 'install.packages("stringr")'
        R -e 'install.packages("dplyr")'
        R -e 'install.packages("purrr")'
        R -e 'install.packages("plyr")'
        R -e 'install.packages("polite")'
        R -e 'install.packages("xml2")'
        R -e 'install.packages("gt")'
        R -e 'install.packages("blastula")'
            
    # Run R script
    - name: Scrape with the polite package
      env:
          EMAIL_SENDER: ${{ secrets.EMAIL_SENDER }}
          EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
          EMAIL_RECIPIENT: ${{ secrets.EMAIL_RECIPIENT }}
          EMAIL_CC_1: ${{ secrets.EMAIL_CC_1 }}
          EMAIL_CC_2: ${{ secrets.EMAIL_CC_2 }}
          EMAIL_CC_3: ${{ secrets.EMAIL_CC_3 }}
      
      run: Rscript polite_price_checker.R

    # Add new files in data folder, commit along with other modified files, push
    - name: Commit files
      run: |
        git config --local user.name github-actions
        git config --local user.email "[email protected]"
        git add polite_price/*
        git commit -am "GH ACTION Autorun POLITE PRICE $(date)"
        git push origin main --force
      env:
        REPO_KEY: ${{secrets.GITHUB_TOKEN}}
        username: github-actions

Solution

  • The runner is configured by the runs-on for each job.

    It can be a GitHub hosted runner or a self-hosted runner according to your context.

    Examples with GitHub hosted runner:

    jobs:
      job1:
        runs-on: ubuntu-latest
        steps:
          [ ... ]
      job2:
        runs-on: macos-latest
        steps:
          [ ... ]
      job3:
        runs-on: windows-latest
        steps:
          [ ... ]
    

    The available GitHub runner images (with the pre-installed tools) can be found here for more details.

    Be aware that each runner use a specific shell by default, for example ubuntu and macos use bash, where windows use powershell.

    Therefore, in your case, you just need to update the runs-on: macos-latest to runs-on: ubuntu-latest or runs-on: windows-latest (supposing you want to use the latest, and not a specific version).

    Note that you can also use matrix to run the same job on different runners if necessary (without duplicating code).