Search code examples
rgithubshinygithub-actions

Using GitHub Actions to create a .env file in the workflow


I recently created this post trying to figure out how to reference GitHub Secrets in a GitHub action. I believe I got that solved & figured out and I'm onto a different issue.

Below is a sample of the workflow code as of right now, the issue I need help with is the Create and populate .Renviron file part.

on: [push, pull_request]
name: CI-CD
jobs:
  CI-CD:
    runs-on: ${{ matrix.config.os }}

    name: ${{ matrix.config.os }} (${{ matrix.config.r }})

    strategy:
      # we keep a matrix for convenience, but we would typically just run on one
      # single OS and R version, aligned with the target deployment environment
      matrix:
        config:
          - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

    env:
      # Enable RStudio Package Manager to speed up package installation
      RSPM: ${{ matrix.config.rspm }}
      # Access token for GitHub
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:

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

      - name: Setup R
        uses: r-lib/actions/setup-r@v1
        with:
          r-version: ${{ matrix.config.r }}

      - name: Install system dependencies
        run: |
          while read -r cmd
          do
            eval sudo $cmd
          done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
      - name: Install R dependencies
        run: |
          remotes::install_deps(dependencies = TRUE)
          remotes::install_cran("rcmdcheck")
        shell: Rscript {0}

      - name: Create and populate .Renviron file
        env:
          AWS_HOST: ${{ secrets.AWS_HOST }}
          AWS_PORT: ${{ secrets.AWS_PORT }}
          AWS_PW: ${{ secrets.AWS_PW }}
          AWS_USER: ${{ secrets.AWS_USER }}
          DBNAME: ${{ secrets.DBNAME }}
        run: |
          touch .Renviron
          echo aws_host="$AWS_HOST" >> .Renviron
          echo aws_port="$AWS_PORT" >> .Renviron
          echo aws_pw="$AWS_PW" >> .Renviron
          echo aws_user="$AWS_USER" >> .Renviron
          echo dbname="$DBNAME" >> .Renviron
          ls ${{ github.workspace }}
        shell: bash

      - name: Deploy to shinyapps.io
        # continuous deployment only for pushes to the main / master branch
        if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
        env:
          SHINYAPPS_ACCOUNT: ${{ secrets.SHINYAPPS_ACCOUNT }}
          SHINYAPPS_TOKEN: ${{ secrets.SHINYAPPS_TOKEN }}
          SHINYAPPS_SECRET: ${{ secrets.SHINYAPPS_SECRET }}
        run: Rscript deploy/deploy-shinyapps.R

I believe this .Renviron file is getting created, but I don't know where, and it certainly doesn't look like it's where the rest of the files are. I've tried a number of file path destinations, .Renviron, ~/.Renviron, $github.workspace/.Renviron, /home/runner/work/NBA-Dashboard/NBA-Dashboard/.Renviron, none of them work. After I create the file I list all of the contents of the workspace directory (which is where I want the file to be) and it's never listed there.

enter image description here

I need that .Renvrion file to be created & listed in that specific directory along with all of these other files so when I continue to the next step and use the rsconnect package to build & deploy my Shiny app it's able to include that file to retrieve environment variables correctly when someone uses the app.

I thought maybe there was some issue with .gitignore so I deleted .Renviron off of that ? But it didn't fix the issue. But ya if anyone has any ideas I'd appreciate it!


Solution

  • The file is there where you expect to be

          - name: Create and populate .Renviron file
            env:
              AWS_HOST: ${{ secrets.AWS_HOST }}
              AWS_PORT: ${{ secrets.AWS_PORT }}
              AWS_PW: ${{ secrets.AWS_PW }}
              AWS_USER: ${{ secrets.AWS_USER }}
              DBNAME: ${{ secrets.DBNAME }}
            run: |
              touch .Renviron
              echo aws_host="$AWS_HOST" >> .Renviron
              echo aws_port="$AWS_PORT" >> .Renviron
              echo aws_pw="$AWS_PW" >> .Renviron
              echo aws_user="$AWS_USER" >> .Renviron
              echo dbname="$DBNAME" >> .Renviron
    
              echo "cat .Renviron"
              cat .Renviron
    
              echo "ls -a ."
              ls -a .
    
              echo "ls -a ${{ github.workspace }}"
              ls -a ${{ github.workspace }}
            shell: bash
    

    you need to run ls -a to show hidden files.

    Run touch .Renviron
      touch .Renviron
      echo aws_host="$AWS_HOST" >> .Renviron
      echo aws_port="$AWS_PORT" >> .Renviron
      echo aws_pw="$AWS_PW" >> .Renviron
      echo aws_user="$AWS_USER" >> .Renviron
      echo dbname="$DBNAME" >> .Renviron
      
      echo "cat .Renviron"
      cat .Renviron
      
      echo "ls -a ."
      ls -a .
      
      echo "ls -a /home/runner/work/github-actions-manual/github-actions-manual"
      ls -a /home/runner/work/github-actions-manual/github-actions-manual
      shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
      env:
        AWS_HOST: 
        AWS_PORT: 
        AWS_PW: 
        AWS_USER: 
        DBNAME: 
    cat .Renviron
    aws_host=
    aws_port=
    aws_pw=
    aws_user=
    dbname=
    ls -a .
    .
    ..
    .Renviron
    .git
    .github
    .gitignore
    LICENSE
    README.md
    change-workflow.ps1
    commit-new-workflow.ps1
    dist
    public
    test.ps1
    test2.ps1
    ls -a /home/runner/work/github-actions-manual/github-actions-manual
    .
    ..
    .Renviron
    .git
    .github
    .gitignore
    LICENSE
    README.md
    change-workflow.ps1
    commit-new-workflow.ps1
    dist
    public
    test.ps1
    test2.ps1