Use bash variables as parameters to a GitHub Action - linux

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 }}"

Related

Use boolean variable as lowercase string in Azure Devops YML pipeline script

I have the following variable in my pipeline script:
variables:
is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
And I want to use it like this:
- script: npm run build -- --source-map=$(is_main)
Unfortunately that leads to an error because "True" is not allowed:
> ng build "--source-map=True"
Cannot parse arguments. See below for the reasons.
Argument --source-map could not be parsed using value "True".Valid type(s) is: boolean
##[error]Bash exited with code '1'.
Finishing: Angular build
I need to lowercase the boolean, and I want to do so in the script step to keep the variable in the pipeline as a true boolean (as opposed to a string) until the last possible moment. I've tried several options. To summarize:
Option 1 $(is_main) leads to:
Argument --source-map could not be parsed using value "True".Valid type(s) is: boolean
Option 2 $[lower($(is_main))] leads to:
lower(True): syntax error in expression (error token is "(True)")
Option 3 $[lower(variables('is_main'))] leads to:
lower(variables('is_main')): syntax error in expression (error token is "(variables('is_main'))")
Option 4 $[lower(variables['is_main'])] leads to:
lower(variables['is_main']): syntax error in expression (error token is "(variables['is_main'])")
Option 5 ${{ lower(variables.is_main) }} (which should not work as far as I could tell from documenation, because it is not used for runtime evaluation, but hey....) leads to:
eq(variables['build.sourcebranch'], 'refs/heads/main'): syntax error in expression (error token is "(variables['build.sourcebranch'], 'refs/heads/main')")
I've read through the "Expressions" documentation on Azure Pipelines MSDN pages and my above attempts are based on it already.
What is the proper way to convert a boolean variable in an Azure Devops pipeline to a lowercase string to be used inside a script step?
Footnote:
For now I use this workaround:
variables:
is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
generate_source_maps: $[lower(variables['is_main'])] # script step requires lowercase boolean
And use it like this:
- script: npm run build -- --source-map=$(generate_source_maps)
Quite suboptimal because generate_source_maps is a string whereas the name suggests it's a boolean, and the conversion to lowercase happens very far from the place in the yml where it is relevant and obvious that this is needed. But it works.
My team and I encountered a similar problem just now [A], and convertToJson worked for us as a way to convert a Pipeline boolean value to lowercase string literal.
Since your initial value is a non-static variable, I don't think you can inline this expression into your script line though [B] - you probably still need an extra variable.
Perhaps this would work for you?
variables:
is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
is_main_str: $[convertToJson(variables['is_main'])]
- script: npm run build -- --source-map=$(is_main_str)
[A]: Specifically, we wanted to pass a Pipeline parameter value of type boolean to an ARM template parameter via overrideParameters on an AzureResourceManagerTemplateDeployment#3 task.
[B]: According to the docs, Compile time expressions can be used anywhere; runtime expressions can be used in variables and conditions., and only runtime expressions can access non-static variables. Looks like the $() macro syntax cannot evaluate an expression.
Use boolean variable as lowercase string in Azure Devops YML pipeline script
You could try to below scripts:
variables:
is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
- script: npm run build -- --source-map=$(echo "$(is_main)" | tr "[:upper:]" "[:lower:]")
If above not work for you, please try to use below scripts:
variables:
Branch: $[eq(variables['Build.SourceBranch'], 'refs/heads/Dev_Test5')]
is_main: $(echo "$(Branch)" | tr "[:upper:]" "[:lower:]")
- script: npm run build -- --source-map=$(is_main)

Azure DevOps pipeline not recognizing pipeline variable in Bash script (not using a YAML file)

The Azure DevOps pipeline has this variable:
Name: pat
Value: Git repo authentication token
The pipeline has a Bash script task. It is set to filepath. Filepath is set to script.sh. script.sh begins with:
git clone https://username:$(PAT)#dev.azure.com/company/project/_git/repo-name
Errors in pipeline logs:
PAT: command not found
Cloning into 'repo-name'...
fatal: Authentication failed for 'https://dev.azure.com/healthcatalyst/CAP/_git/docs-template/'
To validate that the authentication token and repo URL are accurate, I can verify this works when run as inline code:
git clone https://username:$(pat)#dev.azure.com/company/project/_git/repo-name
script.sh file is in repo-name.
However, environment variables work. Both of the following return the accurate value within the script. Note that one has no quotes and the other does.
echo $BUILD_REPOSITORY_NAME
repo-name
echo "$BUILD_REPOSITORY_NAME"
repo-name
Based on documentation I've seen (I am having difficulty with Microsoft's docs because I am not using a YAML file), I've tried unsuccessfully:
$pat
$PAT
$(PAT)
"$(PAT)"
gitToken=<backtick - Markdown is not allowing me to show a backtick here>echo $PAT<backtick>
Does anyone know what I'm doing wrong? Thank you for any tips.
Is your PAT variable a secret variable ?
If so, then it's not directly accessible in script files
As you can see in the documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables
Unlike a normal variable, they are not automatically decrypted into environment variables for scripts. You need to explicitly map secret variables.
Example:
...
env:
MY_MAPPED_ENV_VAR: $(mySecret) # the recommended way to map to an env variable
Or if you are using the visual editor, like that:
Use System.AccessToken instead of personal PAT:
git clone https://$SYSTEM_ACCESSTOKEN#dev.azure.com/company/project/_git/repo-name
To enable $SYSTEM_ACCESSTOKEN: go to release page in ADO > Tasks > Agent job > check Allow scripts to access the OAuth token

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 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

How to interpolate environment variables within netlify.toml config

I want to proxy to a different api depending on the environment - I've tried a few variations on the following theme without any luck. What's the correct way of doing this, if its even possible?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
This does not work.
Although the above config works when I hardcode a URI into the to field, it just fails when I try to interpolate an env var.
It's not supported, but Netlify suggest a work-around in their documentation (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your
netlify.toml file is not supported. However, the following workflow
can be used to substitute values based on environment variables in the
file, assuming you are only trying to change headers or redirects. The
rest of the file is read BEFORE your build - but those sections are
read AFTER the build process.
Add a placeholder like
API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers
sections.
Create an Build Environment Variable, for example API_KEY,
with the desired value. You can do this in the toml file or in our UI
in the Build and Deploy Settings section of your configuration. You
might use the latter to keep sensitive values out of your repository.
Add a command like this one to your build command: sed -i
s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
Answering my own question - it's not supported, you have to manually interpolate env vars yourself as part of the build on Netlify.
Yes. It's possible. Here is the detailed docs: https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
In my case, I need to set a REACT_APP_API_URL separate for production and all other branches. Here is what I use:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"

Resources