diff --git a/.github/workflows/delete-preview.yml b/.github/workflows/delete-preview.yml new file mode 100644 index 0000000..afd658a --- /dev/null +++ b/.github/workflows/delete-preview.yml @@ -0,0 +1,27 @@ +# Candace Savonen Apr 2021 + +name: Delete Preview + +on: + pull_request: + types: [closed] + +jobs: + delete-preview: + runs-on: ubuntu-latest + steps: + + # This is because if a PR is closed before a render finishes it won't find it. + - name: Sleep for 5 minutes + run: sleep 300s + shell: bash + + # Check out current repository + - name: checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + # Delete the branch! + - name: Delete branch locally and remotely + run: git push origin --delete preview-${{ github.event.pull_request.number }} || echo "No branch to delete" diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..bc1c850 --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,155 @@ +# Candace Savonen Dec 2021 + +name: Pull Request + +on: + pull_request: + branches: [ main, staging ] + +jobs: + + create-branch: + name: Create Branch + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Make branch if it doesn't exist + run: | + git config --local user.email "itcrtrainingnetwork@gmail.com" + git config --local user.name "jhudsl-robot" + + branch_name='preview-${{ github.event.pull_request.number }}' + exists=$(git ls-remote https://${{ secrets.GH_PAT }}@github.com/$GITHUB_REPOSITORY $branch_name | wc -l | xargs) + if [[ $exists == 0 ]];then + echo branch doesnt exist + git checkout -b $branch_name || echo branch exists + git push --set-upstream origin $branch_name + else + echo branch does exist + fi + shell: bash + +########################## Make the error reports ############################## + spell-check: + name: Check spelling + needs: yaml-check + uses: jhudsl/ottr-reports/.github/workflows/report-maker.yml@main + with: + check_type: spelling + error_min: 3 + gh_pat: secrets.GH_PAT + + url-check: + name: Check URLs + needs: yaml-check + uses: jhudsl/ottr-reports/.github/workflows/report-maker.yml@main + with: + check_type: urls + error_min: 0 + gh_pat: secrets.GH_PAT + + render-preview: + name: Render preview + needs: yaml-check + runs-on: ubuntu-latest + container: + image: jhudsl/base_ottr:main + + steps: + - name: Checkout files + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + # Set up git checkout + - name: Set up git checkout + run: | + git config --local user.email "itcrtrainingnetwork@gmail.com" + git config --local user.name "jhudsl-robot" + + branch_name='preview-${{ github.event.pull_request.number }}' + git fetch --all + git checkout $branch_name + git merge -s recursive --strategy-option=theirs origin/${{ github.head_ref }} --allow-unrelated-histories + shell: bash + + # We want a fresh run of the renders each time - so first delete old html files + - name: Delete old *.html + run: Rscript -e "rmarkdown::clean_site(preview = FALSE)" + + # Now we want to render all the html files from the Rmd files + - name: Run render html + id: site + run: Rscript -e "rmarkdown::render_site()" + + # We may have html files that we want to render to add as links on the pages -these will be stored in a dir called subdir_html + - name: Render subdir_html + id: subdir_html + run: Rscript -e "for (i in list.files(path = './subdir_html', pattern = 'Rmd$', recursive = TRUE, full.names = TRUE)){rmarkdown::render(i)}" + + # This checks on the steps before it and makes sure that they completed. + # If the renders didn't complete we don't want to commit the file changes + - name: Check on render steps + if: steps.site.outcome != 'success' + run: | + echo site status ${{steps.site.outcome}} + exit 1 + + # Commit the rendered website files + - name: Commit rendered website files to preview branch + id: commit + run: | + branch_name='preview-${{ github.event.pull_request.number }}' + git diff origin/main -- '*.html' >/dev/null && changes=true || changes=false + echo ::set-output name=changes::$changes + git add . --force + git commit -m 'Render preview' || echo "No changes to commit" + git pull --set-upstream origin $branch_name --allow-unrelated-histories --strategy-option=ours + git push --force || echo "No changes to commit" + shell: bash + + - name: Find Comment + uses: peter-evans/find-comment@v1 + id: fc + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: latest commit + + - name: Build components of the comment + id: build-components + run: | + course_name=$(head -n 1 _website.yml | cut -d'"' -f 2| tr " " "-") + website_link=$(echo "https://htmlpreview.github.io/?https://raw.githubusercontent.com/$GITHUB_REPOSITORY/preview-${{ github.event.pull_request.number }}/index.html") + echo ::set-output name=website_link::$website_link + echo ::set-output name=time::$(date +'%Y-%m-%d') + echo ::set-output name=commit_id::$GITHUB_SHA + echo ${{steps.commit.outputs.changes}} + + - name: Create or update comment + if: steps.commit.outputs.changes == 'true' + uses: peter-evans/create-or-update-comment@v1 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + Re-rendered previews from the latest commit: See [preview of website here](${{ steps.build-components.outputs.website_link }}) + + _Updated at ${{ steps.build-components.outputs.time }} with changes from ${{ steps.build-components.outputs.commit_id }}_ + edit-mode: replace + + - name: No comment if no changes + if: steps.commit.outputs.changes == 'false' + uses: peter-evans/create-or-update-comment@v1 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + The latest commit did not produce rendering changes. + + _Updated at ${{ steps.build-components.outputs.time }} with changes from ${{ steps.build-components.outputs.commit_id }}_ + edit-mode: replace diff --git a/.github/workflows/render-site.yml b/.github/workflows/render-site.yml new file mode 100644 index 0000000..1e87727 --- /dev/null +++ b/.github/workflows/render-site.yml @@ -0,0 +1,88 @@ + +# It was adapted by Candace Savonen for the OTTR_template https://github.com/jhudsl/OTTR_Template. + +# This code was adapted by Carrie Wright to enable automated rendering of the site associated with this repository + +name: Render html + +# Controls when the action will run. Triggers the workflow on push +# events only for the master branch +on: + workflow_dispatch: + push: + branches: [ main ] + paths: + - '**.Rmd' + - styles.css + - _site.yml + - subdir_html/*.Rmd + - resources/images/*.png + - resources/images/favicon.ico + - site_libs/* + +jobs: + yaml-check: + name: Load user automation choices + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + # Use the yaml-env-action action. + - name: Load environment from YAML + uses: doughepi/yaml-env-action@v1.0.0 + with: + files: config_automation.yml # Pass a space-separated list of configuration files. Rightmost files take precedence. + + outputs: + rendering_docker_image: "${{ env.RENDERING_DOCKER_IMAGE }}" + + # This workflow contains a single job called "build-all" + render-main: + needs: yaml-check + runs-on: ubuntu-latest + container: + image: ${{needs.yaml-check.outputs.rendering_docker_image}} + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: checkout + uses: actions/checkout@v3 + with: + # get the full repo + fetch-depth: 0 + # use github PAT + token: ${{ secrets.GH_PAT }} + + # We want a fresh run of the renders each time - so first delete old html files + - name: Delete old *.html + run: Rscript -e "rmarkdown::clean_site(preview = FALSE)" + + # Now we want to render all the html files from the Rmd files + - name: Run render html + id: site + run: Rscript -e "rmarkdown::render_site()" + + # We may have html files that we want to render to add as links on the pages -these will be stored in a dir called subdir_html + - name: Render subdir_html + id: subdir_html + run: Rscript -e "for (i in list.files(path = './subdir_html', pattern = 'Rmd$', recursive = TRUE, full.names = TRUE)){rmarkdown::render(i)}" + + # This checks on the steps before it and makes sure that they completed. + # If the renders didn't complete we don't want to commit the file changes + - name: Check on render steps + if: steps.site.outcome != 'success' + run: | + echo site status ${{steps.site.outcome}} + exit 1 + +# Commit the rendered site files - html files and site_libs files + - name: Commit rendered site files + run: | + git config --local user.email "actions@github.com" + git config --local user.name "GitHub Actions" + git add --force *.html + git add --force subdir_html/*.html + git add --force site_libs/* + git commit -m 'Render site' || echo "No changes to commit" + git push origin main || echo "No changes to push"