How to use gitlab CI environment variables in fastlane fastfile? - gitlab

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.

Related

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 read contents from Dockerfile in azure-pipeline.yaml file?

I have environment variable set in Dockerfile which is in Azure Repos part of the project. I have to set up Docker based pipeline in Azure Pipeline. I'm trying to get the environment variable content in azure-pipelines.yaml file.
Is it possible to access Dockerfile contents in azure-pipelines.yaml file?
Can we pass argument value (Environment value) to azure-pipelines.yaml file?
Please guide!
If you want to use an environment variable both when building your docker image and elsewhere in the pipeline, I'd suggest restructuring:
add a build variable to your pipeline, defining your environment variable; this will make it accessible to every task in your pipeline
remove the environment variable from the dockerfile
replace it with an ARG value, so your docker build step can pass in a --build-arg parameter, specifying the build variable

Setting environmental variables for node from host machine for build server

I'm using bitbucket pipelines as a build server.
I need to pass environmental variables from a host machine into a .env file which will then set the var values to be used in the build.
For example, lets say an environmental variable in a docker container running the build is AWS_ACCESS_KEY_ID.
In my .env file I'd like something like the following:
ACCESS_KEY=${AWS_ACCESS_KEY_ID}
I would then run the build and the ACCESS_KEY var would have a value equal to the env var in the docker container.
My current idea for a solution right now involves replacing values with sed, but that feels pretty hacky. Example:
.env file contains the following line:
ACCESS_KEY=<_access_key_replace_me_>
sed "s/<_access_key_replace_me_>/${AWS_ACCESS_KEY_ID}/g" .env
Any better solution than this?

Not able to access Gitlab env variables either in node.js or react

I have two different projects checked in Gitlab, frontend and backend.
For both the projects i have a Dockerfile each.
I have set the env variables in gitlab ci/cd .
I am running docker container in kubernetes,
but i am not able to access the gitlab env variables either in my react or node.js (express) application.
I was thinking that those env variable would be available to me when i do process.env.variable_name, but i am not able to access them.
What’s the best way to access Gitlab env variables in kubernetes (deployment.yaml) env variables ?
UPDATE
I have found that we can specify env variables in kubernetes, deployment.yaml file (under env section). How can i pass gitlab env variables to deployment.yaml?
Docker containers require that you set environment variables when you run them:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
--env , -e Set environment variables
I was able to access environment variable in Node js application using Gitlab K8S_SECRET_ variable naming convention.
For example define a variable like K8S_SECRET_MY_TEST_API in gitlab.
In Node js, you can access this variable using process.env.MY_TEST_API.
Only issue I am facing is that those variables are not available in React app. Still trying to figure that out. I will update here once I resolve that issue.
To inject environment variables to a react app created with create-react-app you should add the prefix REACT_APP_ to every env var.
During the build, webpack will pick all the environment variables with that prefix and will add them to environment.

Environment variables are not found in Jenkins

I want to set quite a few variables in Jenkins. I have tried putting them in .bashrc, .bash_profile and .profile of the jenkins user but Jenkins cannot find them when a build is happening.
The only way that is working is to put all the env variables inside the Jenkinsfile like this:
env.INTERCOM_APP_ID = '12312'
env.INTERCOM_PERSONAL_ACCESS_TOKEN = '1231'
env.INTERCOM_IDENTITY_VERIFICATION_KEY='asadfas'
But I don't think this is a good way of doing it.
What is the correct way of setting env variables in Jenkins?
To me, it seems very normal. INTERCOM_PERSONAL_ACCESS_TOKEN and INTERCOM_IDENTITY_VERIFICATION_KEY should be considered as text credentials and you can use the environment directive to add environment variables.
stages {
stage('Example') {
environment {
INTERCOM_APP_ID = '12312'
INTERCOM_PERSONAL_ACCESS_TOKEN = credentials('TokenCrednetialsID')
INTERCOM_IDENTITY_VERIFICATION_KEY = credentials('VerificationCrednetialsID')
}
steps {
echo "Hello ${env.INTERCOM_APP_ID}"
}
}
}
If you need to keep environment variables separate from JenkinsFile you can create a groovy file which contains all of those and then load that file into Jenkinsfile using
load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
For more information take a look at following links
https://jenkins.io/doc/pipeline/steps/workflow-cps/
SO: Load file with environment variables ...
Jenkins resets environment variables to some defaults for their jobs. Best way to set them is in jenkins configuration. You can set global vars, local for project or local for node.
Now i do not remember if this feature is build in or provided by some plugin.

Resources