Skip to content

Instantly share code, notes, and snippets.

@agarthetiger
Last active May 24, 2023 14:59
Show Gist options
  • Save agarthetiger/818d56771c66c013d34789723465eabe to your computer and use it in GitHub Desktop.
Save agarthetiger/818d56771c66c013d34789723465eabe to your computer and use it in GitHub Desktop.
Commands, env vars, quotes and more in Semaphore
version: v1.0
name: Commands in Semaphore
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
queue:
processing: parallel
blocks:
- name: Run a variety of commands
dependencies: []
task:
jobs:
- name: Dump all available environment variables
commands:
- env | sort
- name: Use the value of an environment variable
commands:
# Double quotes work with dollar sign before variable name
- echo "We're running in a $SEMAPHORE_AGENT_MACHINE_ENVIRONMENT_TYPE"
# Command logged: echo "We're running in a $SEMAPHORE_AGENT_MACHINE_ENVIRONMENT_TYPE"
# Command output: We're running in a VM
# Double quotes and dollar curly braces also works, helps when part of a longer string.
- echo "The current shell is ${SHELL}"
# This won't work, must use double-quotes for variable interpolation
- echo 'The current shell is $SHELL'
# Command logged: echo 'The current shell is $SHELL'
# Command output: The current shell is $SHELL
- name: Use quotes and a variable inside quoted command
commands:
# Use single quotes to wrap the command instead.
- 'echo "Current shell is ${SHELL}"'
# This also works.
- "echo \"Current shell is ${SHELL}\""
# But this does not work.
- "echo 'Current shell is ${SHELL}'"
- name: Single-line if statement - But why?
commands:
- if [ ! -d /tmp/coverage ]; then mkdir /tmp/coverage && echo "Created dir"; else echo "Skipping dir creation"; fi
- name: Single-line test
commands:
# Needs quotes or `- |` and a new line because of the first character being `[` which is yaml for
# the start of an array.
- "[[ ! -d /tmp/coverage ]] && mkdir /tmp/coverage && echo 'Created dir'"
- name: Multi-line command - Get IP address
commands:
# Dumb example to split across lines, but it's just an example. No line-continuation characters required.
- >-
curl
-X GET
"https://checkip.amazonaws.com"
- name: Multi-line if statement - Create /tmp/coverage directory
commands:
- |
if [ ! -d /tmp/coverage ]; then
mkdir /tmp/coverage
echo "Created /tmp/coverage inside a $SEMAPHORE_AGENT_MACHINE_ENVIRONMENT_TYPE"
fi
- echo "Multi-line if statement done."
- name: Multi-line if statement with multi-line commands
commands:
- |
if [ ! -d /tmp/coverage ]; then
curl \
-X GET \
"https://checkip.amazonaws.com"
mkdir /tmp/coverage
echo "Created /tmp/coverage inside a $SEMAPHORE_AGENT_MACHINE_ENVIRONMENT_TYPE"
fi
- echo "Multi-line if statement done."
- name: Multi-line with if condition, multi-line json, curl and check response code
commands:
- >-
if [ "$SOMETHING" == "passed" ]; then
echo "Skipping curl."
else
echo "Post event to somewhere, because SOMETHING is ${SOMETHING}"
json_data='{
"key": "value",
"moar_keys": "moar_better",
"tags": [
"some_tag_1:'${ENV_VAR_1}'",
"some_tag_2:'${ENV_VAR_2}'"
]
}'
echo "Posting the following JSON"
echo "$json_data"
response_code=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
"https://dummy.url" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "$json_data" )
echo "Response code from curl request was $response_code"
fi
- name: Create a new environment variable and reference it in the same job
commands:
# Use this for dynamic values. For static values set `env_vars:` on the job, task or pipeline.
- MY_IP=$(curl "https://checkip.amazonaws.com")
- echo "My apparent public IP address is ${MY_IP}"
- name: Export a value to use in another job - IP address and app version
commands:
# Use the sem-context cli as a key-value store to pass data between jobs, blocks and
# pipelines in the same workflow.
- sem-context put MY_IP=$(curl "https://checkip.amazonaws.com") --force
- sem-context put APP_VERSION=${SEMAPHORE_SCALA_VERSION}
- name: Run commands using the output from another task
dependencies: ["Run a variety of commands"]
task:
jobs:
- name: Echo variable set in previous job - IP address and app version
commands:
- RESTORED_IP_VAULE=$(sem-context get MY_IP)
- echo "The IP address from the previous task was recorded as ${RESTORED_IP_VAULE}"
- echo "Read directly from the key-value store, the IP address was $(sem-context get MY_IP)"
- echo "App Version is $(sem-context get APP_VERSION)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment