In codeship, what is the syntax to use env variables in steps? - codeship

In codeship - I am trying to use the env variables. My setup looks like this:
codeship-services.yml
environment:
- ENV=my-var
codeship-steps.yml
type: parallel
steps:
- command: echo $ENV
I does not work, it just prints $ENV.

Environment variables are only available when you the command is invoked in the context of a shell. By default that's not the case (similar to how docker run operates as well).
To get access to environment variables, either extract the command to a shell script and call the script instead, or explicitly invoke a shell
- service: app
command: sh -c "echo $ENV"

Related

Difference between '-- /bin/sh -c ls' vs 'ls' when setting a command in kubectl?

I am bit confused with commands in kubectl. I am not sure when I can use the commands directly like
command: ["command"] or -- some_command
vs
command: [/bin/sh, -c, "command"] or -- /bin/sh -c some_command
I am bit confused with commands in kubectl. I am not sure when I can use the commands directly
Thankfully the distinction is easy(?): every command: is fed into the exec system call (or its golang equivalent); so if your container contains a binary that the kernel can successfully execute, you are welcome to use it in command:; if it is a shell built-in, shell alias, or otherwise requires sh (or python or whatever) to execute, then you must be explicit to the container runtime about that distinction
If it helps any, the command: syntax of kubernetes container:s are the equivalent of ENTRYPOINT ["",""] line of Dockerfile, not CMD ["", ""] and for sure not ENTRYPOINT echo this is fed to /bin/sh for you.
At a low level, every (Unix/Linux) command is invoked as a series of "words". If you type a command into your shell, the shell does some preprocessing and then creates the "words" and runs the command. In Kubernetes command: (and args:) there isn't a shell involved, unless you explicitly supply one.
I would default to using the list form unless you specifically need shell features.
command: # overrides Docker ENTRYPOINT
- the_command
- --an-argument
- --another
- value
If you use list form, you must explicitly list out each word. You may use either YAML block list syntax as above or flow list syntax [command, arg1, arg2]. If there are embedded spaces in a single item [command, --option value] then those spaces are included in a single command-line option as if you quoted it, which frequently confuses programs.
You can explicitly invoke a shell if you need to:
command:
- sh
- -c
- the_command --an-argument --another value
This command is in exactly three words, sh, the option -c, and the shell command. The shell will process this command in the usual way and execute it.
You need the shell form only if you're doing something more complicated than running a simple command with fixed arguments. Running multiple sequential commands c1 && c2 or environment variable expansion c1 "$OPTION" are probably the most common ones, but any standard Bourne shell syntax would be acceptable here (redirects, pipelines, ...).

How to use environment variable in Dockerfile RUN

I've read several tutorials about this and it seems like this code should work. I want to use RUN in my Dockerfile to write environment variables to a config file. The Dockerfile is simply:
FROM php:7.4-apache
RUN echo "I am ${USERNAME}" > /home/dockerfile.log
I am starting it with this docker-compose.yml
version: "3"
services:
web:
build:
context: .
environment:
- USERNAME=larry
After starting with docker-compose up -d the log file just says I Am so the environment variable is empty. What's wrong here? If I run docker-compose config I can see the variable shown correctly there.
The Run command is executed at build time (I.e. when building the image ) while the environment variable in your docker-compose is added to the container at run time. So when the Run command is executed the environment variable has not been populated yet. If you want to pass a parameter at build time, I would suggest using a build argument instead. In your compose file use arg instead of environment

current directory variable substitution in docker-compose for windows 10

I am trying to create absolute paths in my docker-compose using variable substitution for the path up to the current directory. I pass these paths as environment variables in my container.
More precisely I try to pass it via the cd command and not via the PWD env var. It is supposed to be possible declaring the env var in the .env (https://github.com/docker/compose/issues/4081).
Unfortunately I cannot get the substitution to work.
I tried the different approaches:
with the declaration of the var as a environment variable in a .env OR directly in the environment list
Running with powershell and command prompt
using %cd% and $pwd with both shells
escape $ and %, doubling them down with extra $ or %
Relevant piece of the docker-compose:
version: "3.2"
services:
jupyter:
environment:
- WORKING_DIR=%cd% # OR $(pwd) OR $VAR
and when I use the .env:
VAR=%cd% or ${pwd}
When I 'set' my environment variables in my container, it just shows the command I used (%cd%, etc) as if they were strings.
I do not quite understand where is the docker-compose formated/executed: in the shell I use to call docker-compose, in the linux image where my docker daemon runs, etc ?
EDIT:
I tried on a MacOS version and I still have the problem ! (using adequate bash variable substitution)
Not a complete solution, but perhaps something you can script:
If you specify DOCUMENT_ROOT as a variable in the docker compose file
environment:
- WORKING_DIR=${DOCUMENT_ROOT}
And have an .env file in your working directory that contains
DOCUMENT_ROOT=./
, then you can use the local .env file by running
docker-compose -f /path/to/docker-compose.yml up
https://docs.docker.com/compose/compose-file/#env_file

How to pass multiple arguments to Command line in VSTS

I am using Command line in VSTS in the Release pipeline. Unfortunately not sure how to pass multiple arguments like "username", "password" and "siteurl" to the command line
If you are using Command Line task, you can use the format $(variablename) to pass argument.
Such as below example:
For passing multiple variables in the Command Line task, the format is still $(variablename).
Such as, the command line script can be:
echo "Please enter your user name: $(username). Please enter to you password: $(password). Please enter the site url: $(siteurl)"
As per the documentation, and using YAML code, this can be achieved by the env argument.
# Command line
# Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
- task: CmdLine#2
inputs:
script: 'echo Example of a command line task'
env:
var1: someValue
var2: $(someVariable)
...
Note that:
Secret variables are not automatically mapped. If you have a secret
variable called Foo, you can map it in as shown in the above example
Also, do not forget that you have to declare the variables you are using. For groups of variables, which on Azure DevOps you can declare in the Pipelines Menu, on the Library sub-menu. In the image below, I am declaring a group variable named Tailspin Space Game - Release
I can then reference the group variables in the YAML code as follows:
variables:
- group: 'Timeline CV - Release'

docker compose environment variable for command

I am having troubles in passing environment variables to my custom image via the compose command option:
My compose file:
---
version: '2'
services:
myservice:
image: mycustomimage_lms
environment:
CONF_HOME: /opt/apps-java/
APP_ENV: dev
UUID: me1
command: -Dconfig.home=${CONF_HOME} -Dcomponent.name=LMS -Denv=${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=${UUID} -jar /opt/apps-java/my.jar
ports:
- "9060"
volumes:
- ./:/opt/apps-java/
- ./:/var/logs/apps-logs/
- ./:/tmp/data
My image is just a custom jre image which has an entrypoint set to a shell script that accepts jvm arguments. My run.sh that is called from enrtypoint
#!/bin/sh
export JAVA_HOME="/usr/java/latest/"
exec $JAVA_HOME/bin/java $#
I need to pass values to command at runtime since I can then use my image for a lot of other jars and just changing parameters to my image.
This is what i get:
$> docker-compose up
WARNING: The CONF_HOME variable is not set. Defaulting to a blank string.
WARNING: The APP_ENV variable is not set. Defaulting to a blank string.
WARNING: The UUID variable is not set. Defaulting to a blank string.
I have also
gone through couple of answers such as :
Docker Compose - Command using Container Environment Variable
and
Docker-compose environment variables
but could not get it working. Any directions please?
The variables are being read by Compose when the file is parsed. But setting environment only provides values to the container, not to the file parsing.
If you're trying to pass those variables into the container, you need to escape them in the command using an extra $
-Dconfig.home=$${CONF_HOME} -Dcomponent.name=LMS -Denv=$${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=$${UUID
If you're just trying to use variables in the Compose file, you need to put those variables into an .env file.
See https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution for the full documentation
Escaping the $ so the variable is not substituted immediately but later in the container environment is the trick, as the accepted answer does in the the docker-compose.yml.
I just wanted to add that in case you pass a command to the docker-compose call, you need to escape the character in the shell, i.e. with \
docker-compose run --rm myservice "-Dconfig.home=\${CONF_HOME} -Dcomponent.name=LMS -Denv=\${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=\${UUID} -jar /opt/apps-java/my.jar"