Hands on!

Author

Beatriz Milz

First, we need to create a repository: https://github.com/new

Exploring jobs!

1 - Writing a simple workflow

01-hello-gha.yml

  on:
  workflow_dispatch:

name: 01-hello-gha

jobs:
  write-message:
    runs-on: ubuntu-latest
    steps:
      - name: print-message
        run: echo "Hello GitHub Actions! 🎉"

3 - Run code from R script (only base packages)

03-run-r-script.yml

  on:
  workflow_dispatch:

name: 03-run-r-script

jobs:
  run-r-script:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: r-lib/actions/setup-r@v2
      - name: run-script
        run: Rscript scripts/03-r-script.R
        

scripts/03-r-script.R



print("Sampling 10 numbers between 1 and 100: ")
sample(1:100, 10)

print("Print date: ")
Sys.Date()

4 - Run code using other packages

04-run-r-script-with-pkgs.yml

  on:
  workflow_dispatch:

name: 04-run-r-script-with-pkgs

jobs:
  run-r-script:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: r-lib/actions/setup-r@v2
        with: 
          use-public-rspm: true
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          cache-version: 2
          packages:
            any::httr
            any::fs

      - name: run-script
        run: Rscript scripts/04-r-script.R        

scripts/04-r-script.R



library(httr)
library(fs)

print("Checking which files we have:")
dir_ls()

#  the URL 
url_download = "https://app.anm.gov.br/SIGBM/Publico/ClassificacaoNacionalDaBarragem/ExportarExcel"

# file name to save
file_name = paste0("sigbm_download_", Sys.Date(), ".xlsx")


print("Making a POST request and writing file on disk:")
POST(url_download, write_disk(file_name, overwrite = TRUE))


print("Checking which files we have:")
dir_ls()

5 - Save results from code in the repository

05-saving-results.yml

  on:
  workflow_dispatch:

name: 05-saving-results

jobs:
  run-r-script:
    runs-on: ubuntu-latest
    env: 
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}    
    steps:
      - uses: actions/checkout@v3
      - uses: r-lib/actions/setup-r@v2
        with: 
          use-public-rspm: true
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          cache-version: 2
          packages:
            any::httr
            any::fs

      - name: run-script
        run: Rscript scripts/04-r-script.R        
        
      - name: commit files
        run: |
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add -A
          git commit -m "Saving the results" || echo "no changes to commit"
          git push origin || echo "no changes to commit"

Exploring events!

6 - Schedule

Tip: CronTab Guru.

06-schedule.yml

  on:
  workflow_dispatch:
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron:  "0/5 * * * *"
  
name: 06-event-schedule

jobs:
  run-r-script:
    runs-on: ubuntu-latest
    env: 
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}    
    steps:
      - uses: actions/checkout@v3
      - uses: r-lib/actions/setup-r@v2
        with: 
          use-public-rspm: true
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          cache-version: 2
          packages:
            any::httr
            any::fs

      - name: run-script
        run: Rscript scripts/04-r-script.R        
        
      - name: commit files
        run: |
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add -A
          git commit -m "Saving the results" || echo "no changes to commit"
          git push origin || echo "no changes to commit"

7 - Changes on the code

The workflow run is triggered every time someone pushes a change to the repository or merges a pull request.

07-on-push.yml

  on:
  workflow_dispatch:
  push:
    branches:
      - main
      - master

name: 07-saving-results

jobs:
  run-r-script:
    runs-on: ubuntu-latest
    env: 
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}    
    steps:
      - uses: actions/checkout@v3
      - uses: r-lib/actions/setup-r@v2
        with: 
          use-public-rspm: true
      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          cache-version: 2
          packages:
            any::httr
            any::fs

      - name: run-script
        run: Rscript scripts/04-r-script.R        
        
      - name: commit files
        run: |
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add -A
          git commit -m "Saving the results" || echo "no changes to commit"
          git push origin || echo "no changes to commit"