Skip to content

Using Environment variables in Code Catalyst Devfiles

0

I want to be able to use an envirionment varible in a devfile, so i can dynamically change the workking directory for files. The reason for doing this is because I want to put the devfile in a github repo template, ( for consistency ). The files in my template repo look like this..

/scripts setupSSO.sh setnpm.sh devfile.yaml

Using a devfile that looks like this, works if my project name is cocaTemplateRepo.. Of course it doe'snt work when the project name is differnet.

schemaVersion: 2.0.0 metadata: name: aws-universal version: 1.0.1 displayName: AWS Universal description: Stack with AWS Universal Tooling tags: ["aws", "al2"] projectType: "aws" components:

  • name: aws-runtime container: image: public.ecr.aws/aws-mde/universal-image:latest mountSources: true volumeMounts: - name: docker-store path: /var/lib/docker
  • name: docker-store volume: size: 16Gi commands:
  • id: setexecuteforsso exec: component: aws-runtime commandLine: "chmod +x setupSSO.sh" workingDir: /projects/cocaTemplateRepo/scripts
  • id: setexefornpm exec: component: aws-runtime commandLine: "chmod +x setnpm.sh" workingDir: /projects/cocaTemplateRepo/scripts
  • id: setupsso exec: component: aws-runtime commandLine: "./setupSSO.sh" workingDir: /projects/cocaTemplateRepo/scripts events: postStart:
    • setexecuteforsso
    • setexefornpm
    • setupsso

There is a envar that provides the right path, called OLDPWD, but i does'nt seem i can reference it.

commands:

  • id: setexecuteforsso exec: component: aws-runtime commandLine: "chmod +x setupSSO.sh" workingDir: ${OLDPWD}/scripts
  • id: setexefornpm exec: component: aws-runtime commandLine: "chmod +x setnpm.sh" workingDir: ${OLDPWD}/scripts
  • id: setupsso exec: component: aws-runtime commandLine: "./setupSSO.sh" workingDir: ${OLDPWD}/scripts

asked 2 years ago219 views

3 Answers
1

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.

  1. 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
  2. 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
  3. 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

answered 2 years ago

EXPERT

reviewed 2 years ago

0
Accepted Answer

I was almost there.. It turns out in my dev file i'd not added setupsso in the postStart Events. :-(

answered 2 years ago

0

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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.