Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 3

Concatenate multiple environment variable

Solved! Go to Solution.

Hello,

 

The ORM I use ( Sequelize ) needs to get my database connection URL into a env variable DATABASE_URL 

For example : 

 

const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname') // Example for postgres

 

Now, I would like to have this kind of  with my action  : How can I do that ?

 

name: Source Code CI/CD

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
ci:
runs-on: ubuntu-latest
container:
image: node
services:
# More explanation about that here :
# https://github.com/actions/example-services/blob/master/.github/workflows/postgres-service.yml
postgres:
image: postgres:12-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: jy95
POSTGRES_DB: sourcecode
ports: ["5432:5432"]
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v1
- name: Install & Tests
env:
# use postgres for the host here because we have specified a container for the job.
# If we were running the job on the VM this would be localhost
POSTGRES_HOST: postgres
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
# TODO How to concatenate multiple env variables in Github Actions ?
DATABASE_URL: ???
run: |
npm install
npm test
cd:
runs-on: ubuntu-latest
needs: ci

steps:
- uses: actions/checkout@v1
- name: Docker login
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}
- name: Build
run: docker build -t sourcecode_api .
- name: Tags
run: |
docker tag back ${{ secrets.DOCKER_USER }}/sourcecode_api:${{ github.sha }}
docker tag back ${{ secrets.DOCKER_USER }}/sourcecode_api:latest
- name: Push
run: |
docker push ${{ secrets.DOCKER_USER }}/sourcecode_api:${{ github.sha }}
docker push ${{ secrets.DOCKER_USER }}/sourcecode_api:latest


 

Thanks in advanced

 

2 Replies
Highlighted
Solution
GitHub Partner
Message 2 of 3

Re: Concatenate multiple environment variable

You could set those env which need to use in DATABASE_URL as job environment variables. 

For POSTGRES_PORT, we could use another step to set it as an env. 

Then to get those env value, you can use ${{ env.POSTGRES_***}} syntax to concatenate multiple env variables. 

DATABASE_URL: '${{ env.POSTGRES_HOST }}://${{ env.POSTGRES_USER}}:${{env.POSTGRES_PASSWORD}}@example.com:${{env.POSTGRES_PORT}}/${{env.POSTGRES_DB}}'

Please see my example yml content: 

env in other env.png

 Here is the result: 

result.png

Highlighted
Copilot Lvl 3
Message 3 of 3

Re: Concatenate multiple environment variable

@Yanjingzhu Thanks for the help.

The concatenation works but I still cann't access the database ( https://stackoverflow.com/questions/60321175/github-actions-unhandled-rejection-sequelizeconnectione... )