C
Coder.com•4w ago
TitiMoby

how to use files in script after cloning a repository?

I'd like to use some files that comes from a repository. The ideal use case is: - clone the repository, using module "git-clone" - use a file from this repository as the script parameter of resource "coder_script"
21 Replies
Codercord
Codercord•4w ago
<#1299375544500228096>
Category
Help needed
Product
Coder OSS (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
TitiMoby
TitiMobyOP•4w ago
I tried a workaround and used the
depends_on
depends_on
parameter in a coder_script resource. but the call happen before the clone is done. the part of the template is:
module "git-clone" {
source = "registry.coder.com/modules/git-clone/coder"
version = "1.0.18"
agent_id = coder_agent.main.id
url = "https://github.com/devrel-workshop/101-AI-Enpoints"
branch_name = "add-coder-template-files"
order = 1
}

resource "coder_script" "setup_script" {
agent_id = coder_agent.main.id
display_name = "Create all needed environment variables"
run_on_start = true
script = templatefile("${path.module}/setup.sh", {
})
depends_on = [ module.git-clone ]
}
module "git-clone" {
source = "registry.coder.com/modules/git-clone/coder"
version = "1.0.18"
agent_id = coder_agent.main.id
url = "https://github.com/devrel-workshop/101-AI-Enpoints"
branch_name = "add-coder-template-files"
order = 1
}

resource "coder_script" "setup_script" {
agent_id = coder_agent.main.id
display_name = "Create all needed environment variables"
run_on_start = true
script = templatefile("${path.module}/setup.sh", {
})
depends_on = [ module.git-clone ]
}
and I add a setup.sh file in the Coder template configuration.
#!/bin/bash
# we are supposed to find the cloned repository
101-AI-Enpoints/setup_env.sh
#!/bin/bash
# we are supposed to find the cloned repository
101-AI-Enpoints/setup_env.sh
the thing I try to obtain was done with Gitpod previously. the code is public in https://github.com/devrel-workshop/101-AI-and-py the .gitpod.ym file was sourcing a script (yeah, I have still a lot of road to go with Coder to do the same 😉 )
image: bdmxla3s.gra7.container-registry.ovh.net/workshop_ia/101_ia:1.3.2

tasks:
- name: Activate proper python and create environment file
init: echo 'init script' # runs during prebuild => https://www.gitpod.io/docs/configure/projects/prebuilds
command: |
source /home/gitpod/venv/bin/activate
source ./setup_env.sh
image: bdmxla3s.gra7.container-registry.ovh.net/workshop_ia/101_ia:1.3.2

tasks:
- name: Activate proper python and create environment file
init: echo 'init script' # runs during prebuild => https://www.gitpod.io/docs/configure/projects/prebuilds
command: |
source /home/gitpod/venv/bin/activate
source ./setup_env.sh
Phorcys
Phorcys•4w ago
try setting depends_on = [ coder_script.git_clone ] honestly it looks like you might be better off with devcontainers given your workflow i've seen this issue in the past before though, we might want to add a run_after_clone argument or something
TitiMoby
TitiMobyOP•4w ago
I know, we already have a devcontainer for local dev in case of network issue. But I thought this feature was still alpha I'm trying this
Phorcys
Phorcys•4w ago
not anymore!
TitiMoby
TitiMobyOP•4w ago
Error: Reference to undeclared resource

on main.tf line 253, in resource "coder_script" "setup_script":

253: depends_on = [ coder_script.git-clone ]

A managed resource "coder_script" "git-clone" has not been declared in the root module.
Error: Reference to undeclared resource

on main.tf line 253, in resource "coder_script" "setup_script":

253: depends_on = [ coder_script.git-clone ]

A managed resource "coder_script" "git-clone" has not been declared in the root module.
Phorcys
Phorcys•4w ago
ah yeah I think you should do the cloning directly in your coder_script
TitiMoby
TitiMobyOP•4w ago
so I tried the "baby steps" approach but since the beginning I knew the devcontainer was the way
Phorcys
Phorcys•4w ago
the issue is that with the module i don't think you have any way to know when it's done cloning
TitiMoby
TitiMobyOP•4w ago
but in the end, I won't be able to source the script to have environment variable... so let's have a look at devcontainer
Phorcys
Phorcys•4w ago
why not?
#!/bin/bash
git clone "https://github.com/devrel-workshop/101-AI-Enpoints" -b "add-coder-template-files"

101-AI-Enpoints/setup_env.sh
#!/bin/bash
git clone "https://github.com/devrel-workshop/101-AI-Enpoints" -b "add-coder-template-files"

101-AI-Enpoints/setup_env.sh
this isn't the prettiest but it should do the job
TitiMoby
TitiMobyOP•4w ago
I understood this but you won't be able to have env variable in the end except if coder_script has a syntax to
source setup.sh
source setup.sh
Phorcys
Phorcys•4w ago
@Atif do you have another idea? oh so you mean that the env variables have to be defined in the terminal session when a user opens it?
TitiMoby
TitiMobyOP•4w ago
yes, this was the way we were able to provide the attendee a full environment for the workshop with specific env variables as API token and such
Phorcys
Phorcys•4w ago
i see what you mean yeah except adding it to /etc/profile.d or the .bashrc/.whateverrc i don't think there's a straightforward way to infer env variables from coder_scripts
TitiMoby
TitiMobyOP•4w ago
If this useful, I can describe the compete use case to you or any interested product manager or tech lead being able to have those variable is the base for our workshop. do you think this is possible with the devcontainer approach?
Phorcys
Phorcys•4w ago
well, i don't think devcontainers would solve this issue really because devcontainers still need the variable to be set at some point at build time (of the image) except doing something like this, i'm not sure how you would achieve that
#!/bin/bash
echo "101-AI-Enpoints/setup_env.sh" >> $HOME/.bashrc
#!/bin/bash
echo "101-AI-Enpoints/setup_env.sh" >> $HOME/.bashrc
I can hop on a call if you want
TitiMoby
TitiMobyOP•4w ago
I'm available for the next 20 minutes, I don't know if it is possible for you
TitiMoby
TitiMobyOP•4w ago
thanks a lot, I now have a lot of readings 😉
Codercord
Codercord•4w ago
@Phorcys closed the thread.
Want results from more Discord servers?
Add your server