- Newest
- Most votes
- Most comments
To use environment variables in CodeCatalyst devfiles, you can't directly reference them in the way you've attempted with ${OLDPWD}. However, there are a few approaches you can take to achieve your goal of dynamically changing the working directory for files.
-
Use the /projects directory: In CodeCatalyst, the /projects directory is the root directory for your source repository. You can use this as a base path in your devfile commands, which will work regardless of the project name. For example:
commands: - id: setexecuteforsso exec: component: aws-runtime commandLine: "chmod +x setupSSO.sh" workingDir: /projects/scripts -
Use a startup script: You can create a startup script that determines the correct working directory and sets it as an environment variable. Then, use this variable in your devfile commands. For example:
Create a startup.sh script:
#!/bin/bash export PROJECT_ROOT=$(pwd)In your devfile:
commands: - id: setenvironment exec: component: aws-runtime commandLine: "source startup.sh" workingDir: /projects - id: setexecuteforsso exec: component: aws-runtime commandLine: "chmod +x $PROJECT_ROOT/scripts/setupSSO.sh" events: postStart: - setenvironment - setexecuteforsso -
Use devfile events: You can use the postStart event to run a command that sets up your environment variables. Then, subsequent commands can use these variables.
Remember that CodeCatalyst supports devfile version 2.0.0 and allows public container images. The /projects directory stores files from the source repository and the devfile, while the /home directory stores user-created files.
If you need to debug your devfile behavior, you can access the logs stored in /aws/mde/logs after starting your Dev Environment with a working devfile.
These approaches should allow you to create a more dynamic and flexible devfile that can be used across different projects, achieving your goal of consistency when using it in a GitHub repo template.
Sources
Configuring a devfile for a Dev Environment - Amazon CodeCatalyst
Devfile commands - Amazon CodeCatalyst
I was almost there.. It turns out in my dev file i'd not added setupsso in the postStart Events. :-(
answered 2 years ago
THe answers the bot did'nt work. Its written some junk. However some more work with Q, and giving it errors and refining the problem did help. I've worked around needing to use environment variables. However i have run into another issue, that i did not expect.
Here are the commands i now am running in the devfile. I am using find with an exec switch, which lets me change the permissions and execute the script. This runs as expected.
commands:
- id: setexecuteforsso
exec:
component: aws-runtime
commandLine: find /projects -name 'setupSSO.sh' -exec chmod +x {} \;
workingDir: /projects
- id: setexefornpm
exec:
component: aws-runtime
commandLine: find /projects -name 'setnpm.sh' -exec chmod +x {} \;
workingDir: /projects
- id: setupsso
exec:
component: aws-runtime
commandLine: find /projects -name 'depcon.conf' -exec cp {} depcon.conf \; && find /projects -name 'setupSSO.sh' -exec sh {} \;
workingDir: /projects
the bash shell setupSSO.sh is as follows;
#!/bin/bash
# only do this if the sso config does not exisit already
if [ ! -f "/home/mde-user/.aws/config" ]; then
DIRECTORY=$(find . -maxdepth 1 -type d | head -n 2 | tail -n 1)
cd $DIRECTORY
USERNAME=`git config --get user.name`
cd ..
find /projects -name 'depcon.conf' -exec cp {} . \;
#sed "s/-USERNAME-/$USERNAME/g" depcon.conf > /home/mde-user/.aws/config
rm /projects/depcon.conf
fi
If i execute this shell script manually, it will create a config file for sso, and place it in /home/mde-user/.aws/config as expected.
depcon.conf looks like this
[profile codecatalyst]
region=us-west-2
sso_session=codecatalyst
[sso-session codecatalyst]
sso_region=ap-southeast-2
sso_start_url=https://d-123456789e.awsapps.com/start
sso_registration_scopes=codecatalyst:read_write,sso:account:access
[sso-session -USERNAME-]
sso_start_url = https://depcon.awsapps.com/start
sso_region = ap-southeast-2
sso_registration_scopes = sso:account:access
[profile getToken]
sso_session = -USERNAME-
sso_account_id = 71234567892
sso_role_name = getNpmToken
region = getToken
output = json
The script gets the git username, to use as a session-name, copys depcon.conf, to /projects then uses sed to replace all instances of -USERNAME-, and finally write that to the config
However, something is overwriting /home/mde-user/.aws/config when i run /aws/mde/mde start, or if i use this devfile in a new dev environment. What i am left with is just
[profile codecatalyst]
region=us-west-2
sso_session=codecatalyst
[sso-session codecatalyst]
sso_region=ap-southeast-2
sso_start_url=https://d-97672b9c6e.awsapps.com/start
sso_registration_scopes=codecatalyst:read_write,sso:account:access
This is what i'd get if i did'tn provide a devfile at all.
This is perplexing and i have no idea what might be doing this, or how i can modify its behavior
answered 2 years ago
Relevant content
asked 4 years ago
asked 2 years ago
- AWS OFFICIALUpdated 6 months ago
