Need help with env GitHub Action and check conditions

Release APK

  - name: Build Signed APK
    run: ./gradlew assembleRelease
    
  - name: Sign Android release
    uses: r0adkll/sign-android-release@v1
    with:
      releaseDirectory: app/build/outputs/apk/release
      signingKeyBase64: ${{ secrets.SIGNING_KEY }}
      alias: ${{ secrets.ALIAS }}
      keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
      keyPassword: ${{ secrets.KEY_PASSWORD }}
      
  - name: Upload Release Artifact
    uses: actions/upload-artifact@v1
    with:
      name: app-release
      path: ${{ env.SIGNED_RELEASE_FILE }}
    env: 
      RELEASE_BUILD_SUCCESS: 'true' 
    
  - name: Send email indicate success
    if: ${DEBUG_BUILD_SUCCESS} == 'true' && ${RELEASE_BUILD_SUCCESS} == 'true'
    uses: dawidd6/action-send-mail@v2
    with:
      server_address: smtp.gmail.com
      server_port: 465
      username: ${{secrets.MAIL_USERNAME}}
      password: ${{secrets.MAIL_PASSWORD}}
      subject: Github Actions job result
      body: Build job of ${{github.repository}} completed. Generated Debug and Release artifacts!
      to: ${{secrets.MAIL_USERNAME}}
      from: ${{secrets.MAIL_USERNAME}}
      
  - name: Send email indicate failure
    if: ${DEBUG_BUILD_SUCCESS} != 'true' || ${RELEASE_BUILD_SUCCESS} != 'true'
    uses: dawidd6/action-send-mail@v2
    with:
      server_address: smtp.gmail.com
      server_port: 465
      username: ${{secrets.MAIL_USERNAME}}
      password: ${{secrets.MAIL_PASSWORD}}
      subject: Github Actions job result
      body: Build job of ${{github.repository}} is incomplete. 
      to: ${{secrets.MAIL_USERNAME}}
      from: ${{secrets.MAIL_USERNAME}}

I just started messing around with GitHub Action and I am feeling stuck. I have been trying to achieve something like if(artifact exists) send Success email, else send Failure email but failed.

Does GitHub Action have if else statement at all? Can anyone look at my latest script and guide me how to check if an artifact exists and send success email, send failure email otherwise. Thanks in advanced.

First of, there are syntax issues here. The if conditional is not a shell, so you have to use the GitHub ${{ ... }} syntax for variables, and reference environment variables through the env context:

    if: ${{ env.DEBUG_BUILD_SUCCESS }} == 'true' && ${{ env.RELEASE_BUILD_SUCCESS }} == 'true'

Secondly, I don’t see those environment variables getting set anywhere in the excerpt you posted. If they aren’t set in a way that isn’t obvious in the workflow you’ll have to do that. Depending on how you set them you might also want to use boolean literals (e.g. true) instead of strings (the current 'true') in the check.

Finally, there’s a small note in the documentation that success() will automatically be added to if conditionals that do not contain a status check function. Depending on what exactly you’re trying to do that may be relevant. :wink:

1 Like

Let me add something in addition.
Firstly, when you use expressions in an if conditional, you may omit the expression syntax ( ${{ }} ) because GitHub automatically evaluates the if conditional as an expression.
You could use :

if: env.DEBUG_BUILD_SUCCESS == 'true' && env.RELEASE_BUILD_SUCCESS == 'true'

Secondly, env context only include environment variables which is set in Job level env or set env command. For DEBUG_BUILD_SUCCESS and RELEASE_BUILD_SUCCESS , you need to set it to environment variable before you use it in if conditional.
I am not sure how you confirm whether artifact exist or not. I you want to execute the send email steps based on the build step failed or succeeded, you could use job status check function in if conditional .

1 Like

Thank you. I realized that the if syntax is wrong last night and made the changes. I have used the below line to create an environment variable. In order to achieve my requirements, I had to rewrite the code logic and no longer checking for the existence of the artifact.

run: echo ‘::set-env name=HAS_RELEASE_ARTIFACT::true’

2 Likes