Set Github action env variable based on target branch - continuous-integration

I am trying to set different env variables according to the target branch of the pull request.
For example, I am trying to have separate env variable for main and develop, but the ci is keeping such step and not running the actions
- name: Make envfile for develop
uses: SpicyPizza/create-envfile#v1.3
if: ${{ github.ref == 'refs/heads/develop'}}
with:
- name: Make envfile for main
uses: SpicyPizza/create-envfile#v1.3
if: ${{ github.ref == 'refs/heads/main'}}
with:

Related

How to run a reusable workflow conditionnaly?

I have the following workflow in my GitHub actions:
name: Tests e2e iOS App
on:
workflow_dispatch:
inputs:
skip:
type: boolean
required: true
default: false
jobs:
build-simu-ios-zip:
name: Build iOS simulator zip
uses: ./.github/workflows/reusable-e2e-buildsimuioszip.yml
secrets: inherit
with:
environment: ${{ inputs.environment }}
I would like to run the job build-simu-ios-zip conditionnaly, I add the following:
jobs:
build-simu-ios-zip:
name: Build iOS simulator zip
+ if: ${{ inputs.skip == 'false' }}
uses: ./.github/workflows/reusable-e2e-buildsimuioszip.yml
secrets: inherit
with:
environment: ${{ inputs.environment }}
But the job automatically get skipped.
I also tried to pass an input to the reusable workflow and make it conditionnaly it from there, but it also skip.
How can I make a conditionnal reusable workflow in GitHub action?
I made some tests here and using if: ${{ inputs.skip == 'false' }} with single quotes ' doesn't work as you are comparing a boolean type with a string.
However, I found 2 options that worked:
if: ${{ inputs.skip == false }} (no quote)
if: ${{ ! inputs.skip }} (as it's a boolean input, but with !)
Note: I used this workflow for tests.

Env variables set in GitHub Actions workflow not accessible in AWS lambda project

I have a Chalice (AWS lambda Python framework) project the following CI/CD GitHub Action workflow:
name: Production Workflow
on:
push:
branches:
- "main"
env:
REPO: ${{ github.repository }}
GITHUB_REF_NAME: ${{ github.ref_name }}
GITHUB_SHA: ${{ github.sha }}
jobs:
production:
name: Deploy production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Set up Python
uses: actions/setup-python#v1
with:
python-version: "3.9"
- name: Install requirements
run: pip3 install -r requirements.txt
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials#v1
with:
aws-region: ${{ secrets.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Check branch
run: echo "${{ env.GITHUB_REF_NAME }}"
- name: Check branch
run: echo "${{ env.GITHUB_SHA }}"
- name: Run tests
run: python3 -m unittest discover -s tests
- name: Deploy with Chalice
run: chalice deploy --stage=production
However, from inside the project, the env variables REPO, GITHUB_REF_NAME and GITHUB_SHA are not accessible (i.e. os.environ.get("GITHUB_REF_NAME", None)). Why?
I also tried setting the env variables not globally, but in the "Deploy with Chalice" step only, with the same result. Also, I can successfully see the branch and commit ID written in GitHub Actions by the "Check branch" and "Check branch" steps.
Other env variables that are set in the Chalice config file .chalice/config.json are accessible.
You need to set up the environment and explicitly list all ENV variables you want to use, like this:
- name: Deploy with Chalice
run: chalice deploy --stage=production
env:
GITHUB_REF_NAME: ${{ secrets.GITHUB_REF_NAME }}
GITHUB_SHA: ${{ secrets.GITHUB_SHA }}

How to assign environment variables from a file in GitHub actions?

I'm currently working on a Django project. I have create a GitHub action to run python manage.py test command everytime I push the code to main branch.
The problem here is, I have many environment variables in my project. I can't set env variables as GitHub secrets for each variables.
I have a env.dev file in my repository. What I need to do is, everytime I push the code, it needs to assign environment variables by reading it from the env.dev file.
Is there any way to do this?
This is my django.yml file used for GitHub actions.
name: Django CI
on:
pull_request:
branches: [ "main"]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.9]
steps:
- uses: actions/checkout#v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python#v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
As noted in "How do I use an env file with GitHub Actions?", you do have actions like Dotenv Action which do read a .env file from the root of this repo and provides environment variables to build steps.
But those would not be GitHub secret though.
There is an API to create secrets, which means, if your .env content does not change too often, you can script the secret creation step.

Can you have multiple working directories with github actions?

So I have a repo with multiple directories for multiple go projects. Is there a way to run github actions on multiple working directories so I don't have to have redundant yaml with github actions? To automate an error check with golang, I currently have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: ./app1
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
But I'd like to have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: [./app1, ./app2]
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
In order to run something in more than one working directory, I believe you have two options:
Option 1: Matrix
Use GitHub Action's jobs.<job_id>.strategy.matrix option. This will create multiple jobs, each with its own matrix (directory) value.
Here is a sample workflow:
name: Test
on:
push: { branches: master }
jobs:
test:
name: Matrix test
runs-on: ubuntu-latest
strategy:
matrix: { dir: ['some-dir', 'other-dir'] }
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Do something with the matrix value
working-directory: ${{ matrix.dir }}
run: pwd
Running this will create two jobs:
Option 2: Custom Shell Script
If the matrix option is not suitable for your needs, a simple shell script that loops through and tests all your nested applications (directories) might be appropriate. In this case, you ignore the working-direcoty directive in the workflow YAML, and let the script cd to each of them.
For example:
#!/usr/bin/env bash
dirs=( some-dir other-dir )
for dir in "${dirs[#]}"; do
pushd "$dir"
pwd # Do something more significant here
popd
done

Fallback in expression syntax

I have a github workflow that runs on two different events: pull_request and manually. It looks like this:
on:
pull_request:
branches: [master]
workflow_dispatch: {}
env:
API_URL: https://example.com/
What I want is to be able to set an alternate API_URL when I trigger the workflow manually. But I want the workflow to run the same when it runs automatically.
So, first, I add “inputs” to the workflow_dispatch:
on:
pull_request:
branches: [master]
workflow_dispatch:
inputs:
API_URL:
description:
required: true
default: https://example.com
env:
API_URL: ${{ github.event.API_URL }}
From what I understand, this way the API_URL env variable will only be set when the workflow is run manually. But when it’s run automatically, there’s no github.event.API_URL value.
It seems to me that a conditional fallback here is needed.
Will something like this work?
env:
API_URL: ${{ github.event.API_URL || 'https://example.com/' }}
But from the docs the || operator will evaluate to 1 or 0.
Is there a good way to solve this? Thanks!
This worked for me:
env:
BRANCH: ${{ github.base_ref || 'devel' }}
Unfortunately (at the time of writing this), it's not possible that way. But you can achieve the same effect by setting the environment variable (API_URL) to the default value dynamically only when the job is not run manually (eg. the job is triggered by the PR event). You can do that by creating a conditional job step that will run only if API_URL has not been set yet:
on:
pull_request:
branches:
- master
workflow_dispatch:
inputs:
API_URL:
required: true
default: https://example.com
jobs:
build:
env:
API_URL: ${{ github.event.inputs.API_URL }}
runs-on: ubuntu-latest
steps:
- name: Set API_URL environment variable
if: env.API_URL == null
run: echo "API_URL=https://example.com" >> $GITHUB_ENV
- name: Test API_URL environment variable
run: echo "API_URL=$API_URL"
This way we will have API_URL environment variable set regardless of the way the workflow was triggered (either manually or automatically) with the ability to override it with a custom value when run manually.

Resources