Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a config_automation.yml #435

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
3a056ae
Neatening up and trying to control gha triggers
cansavvy Feb 8, 2022
9d154da
Add some config mechs
cansavvy Feb 8, 2022
964e16d
One strategy for yamls
cansavvy Feb 8, 2022
bdf4d65
Fix trigger syntax
cansavvy Feb 8, 2022
a853cf7
Try to fix syntax part 2
cansavvy Feb 8, 2022
0d84ced
Fix syntax part 3
cansavvy Feb 8, 2022
43266db
Add TRIGGER-START
cansavvy Feb 8, 2022
996e502
This is mostly working
cansavvy Feb 8, 2022
a85d3fd
Set up gha
cansavvy Feb 8, 2022
579c801
This seems to work
cansavvy Feb 8, 2022
a56d5ba
Updates to script
cansavvy Feb 8, 2022
6d6ca3b
Updates to script 2
cansavvy Feb 8, 2022
975148f
Nope
cansavvy Feb 8, 2022
35e1d60
Don't delete last line
cansavvy Feb 8, 2022
a0d2c02
script edit
cansavvy Feb 8, 2022
6d36644
Added some extra lines
cansavvy Feb 8, 2022
b200741
Don't add extra spaces
cansavvy Feb 8, 2022
e3e658c
Get rid of packages.bib
cansavvy Feb 8, 2022
a2f97a6
No packages.bib
cansavvy Feb 8, 2022
fa7b618
Add OTTR Website
cansavvy Feb 8, 2022
a398c81
Update sync
cansavvy Feb 8, 2022
cc6c6e4
Get rid of git_repo_check.R too
cansavvy Feb 8, 2022
9c1382c
Make sure ulrs are running on pull request
cansavvy Feb 8, 2022
ccee843
Update all
cansavvy Feb 8, 2022
01cba8a
Fix manage-gha
cansavvy Feb 8, 2022
c192ddd
Switch what image we use
cansavvy Feb 8, 2022
f69702a
Put extract indices after the file is found
cansavvy Feb 8, 2022
ce90b94
If push fails echo
cansavvy Feb 8, 2022
4ecf644
Add docker tester to the file of what's deleted
jhudsl-robot Feb 8, 2022
4433b36
Make render coursera its own thing
jhudsl-robot Feb 8, 2022
aae872e
Add blank lines
jhudsl-robot Feb 8, 2022
f7cf9c8
Merge remote-tracking branch 'origin/staging' into cansavvy/main-gha
jhudsl-robot Feb 8, 2022
ef0d1e6
Add render-coursera.yml to sync.yml
jhudsl-robot Feb 9, 2022
955b491
Polishing
jhudsl-robot Feb 9, 2022
60e27ae
Polishing up comments and etc
jhudsl-robot Feb 9, 2022
28fa844
No spaces
jhudsl-robot Feb 9, 2022
6c31f07
Update trigger handling
cansavvy Feb 9, 2022
1274e9e
Fix GHA branch handling
cansavvy Feb 9, 2022
5a429cc
Echo if no changes to commit
cansavvy Feb 9, 2022
28c0098
Only add github/workflows folder
cansavvy Feb 9, 2022
c25a044
force push
cansavvy Feb 9, 2022
2fbb6b7
Try to fix manage GHA trigger
cansavvy Feb 9, 2022
62afbf7
Fix commenting
cansavvy Feb 9, 2022
70cd861
Merge branch 'staging' into cansavvy/main-gha
cansavvy Feb 9, 2022
97eee77
Fix file changes
cansavvy Feb 9, 2022
01fab8e
Merge remote-tracking branch 'origin/cansavvy/main-gha' into cansavvy…
cansavvy Feb 9, 2022
96ad996
Merge branch 'staging' into cansavvy/main-gha
cansavvy Feb 9, 2022
559d556
Merge branch 'staging' into cansavvy/main-gha
cansavvy Feb 9, 2022
9e2d930
Add a gha-triggers.yml file
cansavvy Feb 9, 2022
cba55f8
Update workflow name
cansavvy Feb 9, 2022
316072d
Move yml and fix script
cansavvy Feb 9, 2022
0e55b55
Don't need store-triggers.R anymore!
cansavvy Feb 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions .github/gha-triggers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
check-quizzes.yml:
status: on
on-trigger:
workflow_dispatch:
pull_request:
branches: [ main, staging ]
paths:
- 'quizzes/*'
off-trigger:
workflow_dispatch:
delete-preview.yml:
status: on
on-trigger:
pull_request:
types: [closed]
off-trigger:
workflow_dispatch:
dependent_on: render-preview.yml
docker-build-test.yml:
status: on
on-trigger:
release:
types:
- created
pull_request:
branches: [ main, staging ]
paths: [ docker/Dockerfile, docker/github_package_list.tsv ]
workflow_dispatch:
inputs:
dockerhubpush:
description: 'Push to Dockerhub?'
required: true
default: 'false'
off-trigger:
workflow_dispatch:
inputs:
dockerhubpush:
description: 'Push to Dockerhub?'
required: true
default: 'false'
release-notes.yml:
status: on
on-trigger:
pull_request:
branches: [ main, staging ]
types: [ opened ]
off-trigger:
render-bookdown.yml:
status: on
on-trigger:
workflow_dispatch:
push:
branches: [ main, staging ]
paths:
- '**.Rmd'
- assets/*
off-trigger:
workflow_dispatch:
render-coursera.yml:
status: on
on-trigger:
workflow_dispatch:
workflow_run:
workflows: [ "Render Bookdown" ]
branches: [ main, staging ]
types:
- completed
off-trigger:
workflow_dispatch:
dependent_on: render-bookdown.yml
render-leanpub.yml:
status: on
on-trigger:
workflow_dispatch:
workflow_run:
workflows: [ "Render Coursera" ]
branches: [ main, staging ]
types:
- completed
off-trigger:
workflow_dispatch:
dependent_on: render-coursera.yml
render-preview.yml:
status: on
on-trigger:
pull_request:
branches: [ main, staging ]
off-trigger:
workflow_dispatch:
send-updates.yml:
status: on
on-trigger:
release:
types:
- published
workflow_dispatch:
inputs:
prtag:
description: 'Tag to use?'
required: true
default: 'null'
off-trigger:
workflow_dispatch:
inputs:
prtag:
description: 'Tag to use?'
required: true
default: 'null'
starting-course.yml:
status: on
on-trigger:
push:
branches: [ main, staging ]
workflow_dispatch:
off-trigger:
workflow_dispatch:
style-and-sp-check.yml:
status: on
on-trigger:
pull_request:
branches: [ main, staging ]
off-trigger:
workflow_dispatch:
test-send-updates.yml:
status: on
on-trigger:
off-trigger:
workflow_dispatch:
inputs:
repo:
description: 'What repo to test on e.g. jhudsl/OTTR_Template_Test'
required: true
default: 'jhudsl/OTTR_Template_Test'
transfer-rendered-files.yml:
status: on
on-trigger:
workflow_dispatch:
workflow_run:
workflows: [ "Render Bookdown" ]
branches: [ main, staging ]
types:
- completed
off-trigger:
workflow_dispatch:
dependent_on: render-bookdown.yml
url-checker.yml:
status: on
on-trigger:
workflow_dispatch:
pull_request:
branches: [ main, staging ]
off-trigger:
workflow_dispatch:
Binary file added .github/on-triggers.rds
Binary file not shown.
22 changes: 22 additions & 0 deletions .github/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,25 @@ group:
dest: scripts/spell-check.R
repos: |
jhudsl/OTTR_Template_Website

repos: |
jhudsl/OTTR_Template_Website
- source: .github/workflows/style-and-sp-check.yml
dest: .github/workflows/style-and-sp-check.yml
- source: .github/workflows/url-checker.yml
dest: .github/workflows/url-checker.yml
- source: scripts/spell-check.R
dest: scripts/spell-check.R
###### TO ADD NEW REPOSITORY USE THIS FORMAT; Feel free to add/drop files that
# you specifically would like synced (or not).
#
# # Repositories to receive changes
# repos: |
# org/repo_name
# # Files which should be updated
# - source: .github/workflows/
# dest: .github/workflows/
# - source: scripts/
# dest: scripts/
# - source: docker/
# dest: docker/
34 changes: 34 additions & 0 deletions .github/workflows/manage-gha.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Candace Savonen Feb 2022

name: Update Github Actions Switches

on:
push:
paths:
- 'config_automation.yml'

jobs:
update-triggers:
runs-on: ubuntu-latest
container:
image: jhudsl/course_template

steps:
- name: checkout repo
uses: actions/checkout@v2

- name: Login as jhudsl-robot
run: |
git config --local user.email "itcrtrainingnetwork@gmail.com"
git config --local user.name "jhudsl-robot"

- name: Run GHA switches script
run: Rscript --vanilla scripts/employ-gha-switches.R

- name: Commit updated workflows to current branch
id: commit_it
run: |
branch_name=$(git rev-parse --abbrev-ref HEAD)
git add .github/workflows --force
git commit -m 'Update GHA switches' || echo "No changes to commit"
git push origin || echo "No changes to commit"
3 changes: 1 addition & 2 deletions .github/workflows/transfer-rendered-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ name: Bookdown to Leanpub repo copy over
#---TRIGGER-START---#
on:
workflow_dispatch:
# Only run after the render finishes running
workflow_run:
workflows: [ "Render Bookdown and Coursera" ]
workflows: [ "Render Bookdown" ]
branches: [ main, staging ]
types:
- completed
Expand Down
13 changes: 13 additions & 0 deletions config_automation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# PR Checks
check-quizzes: yes
url-checker: yes
render-preview: yes
style-and-sp-check: yes

# Rendering
render-bookdown: yes
render-leanpub: yes
render-coursera: yes

# Docker specific
docker-build-test: yes
125 changes: 125 additions & 0 deletions scripts/employ-gha-switches.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env Rscript

library(magrittr)

option_list <- list(
optparse::make_option(
c("--overwrite"),
action = "store_true",
default = FALSE,
help = "TRUE or FALSE overwrite the trigger no matter what",
)
)

# Read the arguments passed
opt_parser <- optparse::OptionParser(option_list = option_list)
opt <- optparse::parse_args(opt_parser)


# Find .git root directory
root_dir <- rprojroot::find_root(rprojroot::has_dir(".github"))

######## Load in triggers
# Each github action has its own on and off trigger versions which are stored
# in this file -- if triggers need to be altered, they must be altered in this file to take effect
trigger_config <- yaml::yaml.load_file(file.path(root_dir, ".github", "gha-triggers.yml"))

######## Get the github actions file paths
# Get github actions directory
github_actions_dir <- file.path(root_dir, ".github", "workflows")

# Get list of github actions files
github_actions_files <- list.files(github_actions_dir,
pattern = "\\.yml$",
full.names = TRUE
)

######## Set up dataframe with information about the GHA
gha_df <- data.frame(gha_files = names(trigger_config))

# Read in the configuration file with the specs
config_df <-
yaml::read_yaml(file.path(root_dir, "config_automation.yml")) %>%
data.frame() %>%
t() %>%
data.frame() %>%
tibble::rownames_to_column("gha_files") %>%
dplyr::rename(new_status = '.') %>%
dplyr::mutate(gha_files = paste0(gsub("\\.", "-", gha_files), ".yml"))

# Extract the "dependent on" information
dependent_on_df <- data.frame(dependent_on = unlist(trigger_config)[grep("dependent_on", names(unlist(trigger_config)))],
gha_files = gsub("\\.dependent_on$", "", grep("dependent_on", names(unlist(trigger_config)), value = TRUE)))

# For GHA dependent on other ones, switch their status based on what the new status is
dependent_on_df$recoded_status <- config_df$new_status[match(dependent_on_df$dependent_on, config_df$gha_files)]

# Put it all together
gha_df <- gha_df %>%
dplyr::left_join(config_df) %>%
dplyr::left_join(dependent_on_df) %>%
dplyr::mutate(new_status = dplyr::case_when(
!is.na(recoded_status) ~ recoded_status,
TRUE ~ new_status)
)

config_gha <- function(gha_file) {

# Read in this GHA yaml
yaml_contents <- readLines(gha_file)

# Get info about this particular GHA
trigger_info <- trigger_config[[basename(gha_file)]]

# Extract what the status should be
new_status <- gha_df %>%
dplyr::filter(basename(gha_file) == gha_files) %>%
dplyr::pull("new_status")

# If status specified in the config_automation.yml continue
if (!is.na(new_status) && length(new_status) == 1) {

# If current status doesn't match specified status
# and it wasn't specified to overwrite anyway...
if ((new_status != trigger_info$status) || opt$overwrite) {

# Output message
message(paste("Switching trigger for", basename(gha_file), "to", new_status))

# Get trigger for whether this should be on or off
trigger <- ifelse(new_status, trigger_info[["on-trigger"]], trigger_info[["off-trigger"]])

# Make sure it has `on:` at the beginning
trigger$on <- trigger
trigger <- trigger[-1]

# Temporarily write to yaml so we can read in as plain text
yaml::write_yaml(trigger, "tmp-trigger.yml")

# Find out where this trigger is supposed to go
trigger_indices <- (grep("TRIGGER-START", yaml_contents) + 1):(grep("TRIGGER-END", yaml_contents) - 1)

# Remove current trigger
yaml_contents <- yaml_contents[-trigger_indices]

# Set up new yaml trigger
trigger_text <- gsub("\\'on\\':", "on:", readLines("tmp-trigger.yml"))

# Put new trigger in
yaml_contents <- append(
yaml_contents,
trigger_text,
after = trigger_indices[1] - 1
)

# Remove temporary file
file.remove("tmp-trigger.yml")

# Write lines to file
writeLines(yaml_contents, gha_file)
}
}
}

# Read in all files
all_gha <- lapply(github_actions_files, config_gha)