Answers: <p>This is guaranteed to always fail:</p>
  <pre class="lang-yaml prettyprint-override"><code>      run: |
          [ &quot;$GITHUB_REF_NAME&quot; = Cypress-reconfigure ] &amp;&amp; echo $STAGING_ENV_FILE | base64 --decode &gt; .env
          [ &quot;$GITHUB_REF_NAME&quot; = staging ] &amp;&amp; echo $PRODUCTION_ENV_FILE | base64 --decode &gt; .env
  </code></pre>
  <p>Why? Depending on the value of <code>$GITHUB_REF_NAME</code> only one of the lines will run. But the other line will return a non-zero return code:</p>
  <pre><code>$ [ &quot;a&quot; = &quot;b&quot; ] &amp;&amp; true
  
  $ echo $?
  1
  </code></pre>
  <p>And GHA is configured to abort on the first command that doesn't return a zero exit code (that's the successful exit code).</p>
  <p>To fix, you should use an if-then, rather than the shell one-liner:</p>
  <pre class="lang-yaml prettyprint-override"><code>      run: |
          if [ &quot;$GITHUB_REF_NAME&quot; = Cypress-reconfigure ]; then echo $STAGING_ENV_FILE | base64 --decode &gt; .env; fi
          if [ &quot;$GITHUB_REF_NAME&quot; = staging ]; then echo $PRODUCTION_ENV_FILE | base64 --decode &gt; .env; fi
  </code></pre>
  <hr />
  <p>Note that I didn't include a different workaround in the shell command of ignoring errors with a <code>|| true</code>:</p>
  <pre><code>[ &quot;$GITHUB_REF_NAME&quot; = Cypress-reconfigure ] &amp;&amp; echo $STAGING_ENV_FILE | base64 --decode &gt; .env || true
  </code></pre>
  <p>because it results in false positives if the command (e.g. <code>base64 --decode</code>) fails.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-23T20:16:26.930Z
Answers: Awesome thanks BMitch works like a charm
Answers: uneeb meer
Answers: CC BY-SA 4.0
Answers: 2022-01-23T20:02:07.070Z
Answers: 2022-01-25T16:00:13.707Z
Answers: 2022-01-25T16:00:13.707Z
Answers: BMitch
Body: <p>I am setting up a pipeline to run Cypress automated tests using github actions/workflows on my code anytime there is a push to the repo. I came into a scenario where I want to generate dyanmic .env files depending on the branch , so instead of hard coding variables i created a base64 string and saved that as an secret and accessed that secret inside the code, however when i try to decode i run into issues, let me show you my code</p>
  <p><strong>ci.yml</strong></p>
  <pre><code>name: Nuxt CI Pipeline
      on:
        push:
          branches: [ Cypress-reconfigure ]
        # pull_request:
        #   branches: [ master ]
      
  jobs:
    build:
  
      runs-on: ubuntu-latest
  
      strategy:
        matrix:
          node-version: [ 14.x ]
          # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
  
      steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - name: Generating .env files
        env:
          STAGING_ENV_FILE: ${{ secrets.STAGING_ENV_FILE }}
          PRODUCTION_ENV_FILE: ${{ secrets.PRODUCTION_ENV_FILE }}
        run: |
          [ &quot;$GITHUB_REF_NAME&quot; = Cypress-reconfigure ] &amp;&amp; echo $STAGING_ENV_FILE | base64 --decode &gt; .env
          [ &quot;$GITHUB_REF_NAME&quot; = staging ] &amp;&amp; echo $PRODUCTION_ENV_FILE | base64 --decode &gt; .env
      - run: cat .env 
      - run: npm ci
      - run: npm run cy:ci
  </code></pre>
  <p><strong>Screenshot</strong></p>
  <p><a href="https://i.sstatic.net/tAwfK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tAwfK.png" alt="Pipeline error" /></a></p>
  <p>I followed this answer which was best suited for my scenario <a href="https://stackoverflow.com/questions/60176044/how-do-i-use-an-env-file-with-github-actions#answer-64452700">Stack Overflow post</a></p>
  <p>As you can see, the error doesnt say anything it just exists! I have very limited knowledge of Devops can someone help me out with what I am doing wrong?</p>
  
Comments: CC BY-SA 4.0
Comments: 2022-01-23T20:03:08.637Z
Comments: [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551)
Comments: BMitch
Tags: <continuous-integration><github><github-actions>
Title: decode base64 Github Secrets
UserDisplayName: uneeb meer
ThreadText: # decode base64 Github Secrets
  
  (Asked by: username_0 on 2022-01-23)
  
  I am setting up a pipeline to run Cypress automated tests using github actions/workflows on my code anytime there is a push to the repo. I came into a scenario where I want to generate dyanmic .env files depending on the branch , so instead of hard coding variables i created a base64 string and saved that as an secret and accessed that secret inside the code, however when i try to decode i run into issues, let me show you my code
  
  **ci.yml**
  
  ```
  name: Nuxt CI Pipeline
      on:
        push:
          branches: [ Cypress-reconfigure ]
        # pull_request:
        #   branches: [ master ]
      
  jobs:
    build:
  
      runs-on: ubuntu-latest
  
      strategy:
        matrix:
          node-version: [ 14.x ]
          # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
  
      steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - name: Generating .env files
        env:
          STAGING_ENV_FILE: ${{ secrets.STAGING_ENV_FILE }}
          PRODUCTION_ENV_FILE: ${{ secrets.PRODUCTION_ENV_FILE }}
        run: |
          [ "$GITHUB_REF_NAME" = Cypress-reconfigure ] && echo $STAGING_ENV_FILE | base64 --decode > .env
          [ "$GITHUB_REF_NAME" = staging ] && echo $PRODUCTION_ENV_FILE | base64 --decode > .env
      - run: cat .env 
      - run: npm ci
      - run: npm run cy:ci
  
  ```
  
  **Screenshot**
  
  [![Pipeline error](https://i.sstatic.net/tAwfK.png)](https://i.sstatic.net/tAwfK.png)
  
  I followed this answer which was best suited for my scenario [Stack Overflow post](https://stackoverflow.com/questions/60176044/how-do-i-use-an-env-file-with-github-actions#answer-64452700)
  
  As you can see, the error doesnt say anything it just exists! I have very limited knowledge of Devops can someone help me out with what I am doing wrong?
  
  **Tags:** continuous-integration, github, github-actions
  
  > **Comments:**
  > **username_1**: [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-23. Score: 2 | Accepted answer
  
  This is guaranteed to always fail:
  
  ```yaml
        run: |
          [ "$GITHUB_REF_NAME" = Cypress-reconfigure ] && echo $STAGING_ENV_FILE | base64 --decode > .env
          [ "$GITHUB_REF_NAME" = staging ] && echo $PRODUCTION_ENV_FILE | base64 --decode > .env
  
  ```
  
  Why? Depending on the value of `$GITHUB_REF_NAME` only one of the lines will run. But the other line will return a non-zero return code:
  
  ```
  $ [ "a" = "b" ] && true
  
  $ echo $?
  1
  
  ```
  
  And GHA is configured to abort on the first command that doesn't return a zero exit code (that's the successful exit code).
  
  To fix, you should use an if-then, rather than the shell one-liner:
  
  ```yaml
        run: |
          if [ "$GITHUB_REF_NAME" = Cypress-reconfigure ]; then echo $STAGING_ENV_FILE | base64 --decode > .env; fi
          if [ "$GITHUB_REF_NAME" = staging ]; then echo $PRODUCTION_ENV_FILE | base64 --decode > .env; fi
  
  ```
  
  ---
  
  Note that I didn't include a different workaround in the shell command of ignoring errors with a `|| true`:
  
  ```
  [ "$GITHUB_REF_NAME" = Cypress-reconfigure ] && echo $STAGING_ENV_FILE | base64 --decode > .env || true
  
  ```
  
  because it results in false positives if the command (e.g. `base64 --decode`) fails.
  
  > **Comments:**
  > **username_0**: Awesome thanks username_1 works like a charm  
  
  ---
  
  