#Sourcing a .bashrc for subsequent usage

1 messages · Page 1 of 1 (latest)

stiff pagoda
#

PandaWave Just sharing a nicely discovered way to source a .bashrc file to use when using dagger to move away from a Gitlab CI written in bash

Context: I'm rewriting the auto generated Gitlab CI of an asdf-vm plugin.

The asdf-vm generated install in a Gitlab CI is the following

asdf-plugin-test:
  stage: test
  needs: []
  before_script:
    - git clone https://github.com/asdf-vm/asdf.git
    - . asdf/asdf.sh
  script:
    - asdf plugin-add "${PLUGIN_NAME}" "${CI_REPOSITORY_URL}"
    - asdf list-all "${PLUGIN_NAME}"
    - asdf plugin test "${PLUGIN_NAME}" "${CI_REPOSITORY_URL}" --asdf-plugin-gitref "${CI_COMMIT_SHA}" "$TOOL_CHECK" || exit 1

The . asdf/asdf.sh line add the asdf executable to the PATH which is used in the script: section.
In the usual installation it is added to the .bashrc file at the end of the installation process.

As i did not want to use a multi line bash shell or a script file i looked around and found out the BASH_ENV variable is taken into account in the subsequent execs if using bash -c command allowing to source the .bashrc file and use it for the future WithExec() like the following:

type Foo struct{}
func (m *Foo) TestAsdf(name, repository, reference, toolCheck string) *dagger.Container {
    return m.BaseContainer().
        WithExec([]string{"git", "clone", "https://github.com/asdf-vm/asdf.git", "/root/.asdf"}).
        WithExec([]string{"sh", "-c", "echo . $HOME/.asdf/asdf.sh > /root/.bashrc"}).
        WithEnvVariable("BASH_ENV", "/root/.bashrc").
        WithExec([]string{"bash", "-c", fmt.Sprintf("asdf plugin-add %s %s", name, repository)}).
        WithExec([]string{"bash", "-c", fmt.Sprintf("asdf list-all %s", name)}).
        WithExec([]string{"bash", "-c", fmt.Sprintf("asdf plugin test %s %s --asdf-plugin-gitref %s %s || exit 1", name, repository, reference, toolCheck)})
}

i hope it'll help those who needs to do this kind of migrations 😛