Github Action how to deal with standalone config file

2.4k views Asked by At

We are using Github Action to deploy our code. On push, the source code will be pushed and we were able to build the code and deploy successfully if the config file is also tracked by the repository. However, we are encountering a problem with a config file in .gitignore.

Our app has different versions, controlled by this config file, and also this file is different from testing to production. Therefore, this file is standalone and not tracked by the git repository. However, for Github actions to build the project correctly, this file is necessary and has to be placed on a certain path of the project, e.g., /configs/env_configs.json.

This seems like a very common use case but I find very little information in Github action's document.

Is there a good way to work this out?

1

There are 1 answers

0
Abhishek On

There is concept of environments available in GitActions. You can create multiple such environments for each of your app versions. And there will be a variable(with same name) in each of these environments holding config data needed for your app to work. In your git actions you identify the branch on which your workflow is triggered and refer that environment name. The config data for that environment(basically your app version) is automatically referred. You can then redirect that variable output to a file in specific build path and can continue with further steps.

Check docs here: Git Actions Environments

Sample Git actions code might look like this

name: Deployment

on:
  push:
    branches: #Your app versions are your branches
      - v1
      - v2
      - v3


jobs:
  config:
    runs-on: ubuntu-latest
    steps:
      - name: find_environment
      - run: |
            if [ ${{ github.ref_name }} == 'v1' ] || [ ${{ github.ref_name }} == 'v1' ]; then
                echo "Building for v1 version of app"
                echo "DEPLOYMENT_ENV=v1" >> $GITHUB_OUTPUT
            else [ ${{ github.ref_name }} == 'v2' ] || [ ${{ github.ref_name }} == 'v2' ]; then
                echo "Building for v2 version of app"
                echo "DEPLOYMENT_ENV=v2" >> $GITHUB_OUTPUT

    outputs:
      DEPLOYMENT_ENV: ${{ steps.deployment_type.outputs.DEPLOYMENT_ENV }}
  
  deployment:
    runs-on: ubuntu-latest
    needs: [config]
    environment:
        name: ${{needs.config.outputs.DEPLOYMENT_ENV}}
    steps:
      - name: deploy
      - run: |
            echo ${{vars.config}} >> /path/to/your/config/file
            # Run other deployment related commands