Gitlab: Concat file based variables - gitlab

I need to replace a variable value inside a file based gitlab variable like below.
File based variable:
Name: app_service_dev_env
Value:
iam_role_name="xxxx"
lambda_s3_bucket_name = "xxxxx"
lambda_s3_key="xxxxx"
Variable:
Name: ENV
Value: dev
Below is what I am looking to implement
before_script:
-cat ${app_service_${ENV}_env} > dev.txt
Getting error: ERROR: Job failed: exit code 2
Could anyone please let me know how to resolve this?
How could I resolve this?

The file variable type creates a file. They are not environment variables. The key is the path to the file.
before_script:
- cat app_service_dev_env > dev.txt
Though, you could simply make the name of your file variable dev.txt
As for your ENV variable, with your current setup you can do something like this:
before_script:
- cat "app_service_${ENV}_env"

Related

How to read environment variables from YAML file in docker?

I'm having a .env file that contains variables
VALUE_A=10
NAME=SWARNA
I'm having an env.yml file.
Information:
value: $VALUE_A
name: $NAME
I'm having a python file envpy.py
from envyaml import EnvYAML
# read file env.yml and parse config
env = EnvYAML('env.yml')
print(env['Information']['value'])
print(env['Information']['name'])
Here is my Dockerfile
FROM python:3
ADD envpy.py /
ADD env.yml /
RUN pip install envyaml
CMD [ "python", "./envpy.py" ]
Expected output:
10
SWARNA
But I got :
VALUE_A
NAME
I'm using the commands to build the docker and run:
docker build -t python-env .
docker run python-env
How to print the values. Please correct me or suggest me where I'm doing wrong. Thank you.
.env is a docker-compose thing, which defines default values for environment variables to be interpolated into docker-compose.yml, and only there. They are not available anywhere else and certainly not inside your image.
You can make the values available as environment variables inside your image by copying .env into the image and in your Python code do
from dotenv import load_dotenv
load_dotenv()
(which requires you to install dotenv).
Also mind that there are probably better ways to achieve what you want:
If the values must be set at build time, you'd rather interpolate them into the resulting file at build time and copy the file with the hardcoded values into the image.
If the values should be overridable at runtime, just define them via ENV with a default value inside the Dockerfile.

Use bash variables as parameters to a GitHub Action

I am calling a GitHub action, and I want to pass it the parameter extra_build_args with the value --build-arg CURRENT_DD_VERSION={$VER} (not in string) where $VER is a shell variable that I set with a specific version. When I check what was passed in I see it took the literal value {$VER} instead of resolving the variable. I set $VER in a different (earlier) step of the Github action. How can pass in the content of the shell variable as a parameter?
- name: Get version
run: |
VER=$(cat ver.txt)
- name: Build docker image
uses: kciter/aws-ecr-action#v3
with:
//some more parameters
extra_build_args: "--build-arg CURRENT_DD_VERSION={$VER}"
Check first the syntax:
${VER}
# not
{$VER}
In your case:
extra_build_args: "--build-arg CURRENT_DD_VERSION=${VER}"
You also have the documentation "Environment variables"
To set custom environment variables, you need to specify the variables in the workflow file.
You can define environment variables for a step, job, or entire workflow using the jobs.<job_id>.steps[*].env, jobs.<job_id>.env, and env keywords.
The examples would use $VER
Or:
extra_build_args: "--build-arg CURRENT_DD_VERSION=${{ env.VER }}"

How to use gitlab CI environment variables in fastlane fastfile?

I am currently using a .env file to get environment variables in FASTFILE, but now I am trying to automate the fastlane using GitLab CI/CD.
Since the .env file which has all the keys can not be pushed to the branch I have to declare all the .env or the environment variables in the GitLab runner's environment variable.
I want to know how can I use the GitLab runners's environment variable in my fastfile.
lane :build_staging do |options|
environment_variable(set: { 'ENVFILE' => '.env.staging' }) // I want to use the GitLab environment variable
clean
gradle(task: options[:task], build_type: 'Staging', project_dir: 'android/')
end
In Settings > Variables, you can define the whole file as a variable with a specified scope :
In your gitlab-ci, you would use it by specifying the variable name (in my example $ENV_FILE) and the scope using stage keyword in your job :
build:
stage: staging
script:
# do your work here
You can find more info in the documentation for variable file type and scope.

how to get custom environment variable in ansible?

Hi I am trying to find out how to get custom environment variable in Ansible.
something that a simple shell command like this:
I created a custom Environment Variable and assigned a value
EXPORT SSH_STATUS=TRUE
who can I access the result from ansible
How can I assign an Ubuntu Environment Variable Value to Ansible Variable
Any clue ?
You can define it as variables or you can pass the variables as extra-vars while running the playbook.
vars:
SSH_STATUS=TRUE
or
--extra-vars "SSH_STATUS=TRUE"
To access the variable use "{{SSH_STATUS}}"
For environment variable use as below
- name: Install cobbler
command: < some command >
environment: "{{SSH_STATUS}}"
#Anish Varghese, From what I have understood, I have written the below code which can extract the value of a custom environment variable in ansible playbook, without even updating the ansible.
- name: show env value
debug:
msg: "{{ lookup('env', 'XV') }}"
Below is the verification of the above task,
Setting the custom environment variable named "XV",
export XV="TRUE"
Running the Ansible script,
TASK [show env value] ***********************************************************************************************************************
ok: [192.168.10.10] => {
"msg": "TRUE"
}
Now, updating the value of the env variable and running the ansible again,
export XV="FALSE"
TASK [show env value] ***********************************************************************************************************************
ok: [192.168.10.10] => {
"msg": "FALSE"
}
Here is the associated documentation.
https://docs.ansible.com/ansible/latest/plugins/lookup/env.html
Ansible has a built in facility for this called environment https://docs.ansible.com/ansible/latest/user_guide/playbooks_environment.html
You probably want set_fact.
- set_fact:
SSH_STATUS: TRUE
If what you need is the value of $somevar, if it gets set by .bashrc then register is your friend.
- name: Expose `$somevar`
shell: echo "$somevar" # must exist
register: tmpvar
- set_fact:
SSH_STATUS: "{{ tmpvar.stdout }}"
The issue is that you have to make sure the variable is set before you access it.

CircleCI: Execute local binary

I'd like to be able to access my local bin files on CircleCI. Simply exporting a new PATH variable doesn't seem to be working. The following works locally, but does not work on CircleCI. On CircleCI, the variable is unchanged.
circle.yml
machine:
pre:
- echo $PATH
- export PATH=$PATH:./bin
- echo $PATH
The answer seems to be interpolating in the machine:environment section.
machine:
environment:
PATH: $PATH:./bin

Resources