Scott Centoni
Scott Centoni

Reputation: 1133

Jenkinsfile with writeFile inside docker container?

If I have a Jenkinsfile with

docker.image('foo').inside {
    writeFile file: bar, text: baz
}

the file gets written to the Jenkins agent's workspace, not inside the container, right? Is there a sane way to write a file inside a container?

The closest thing I was able to find on the web is https://issues.jenkins-ci.org/browse/JENKINS-33510. I suppose I could use a sh step with a here-doc, but that sounds pretty ugly.

Upvotes: 2

Views: 3722

Answers (1)

arasio
arasio

Reputation: 1316

There is a workspace in the container and you can write files inside the container with writeFiles. But it's also shared with the workspace of the host node because it's mounted as a volume.

inside will:

  1. Automatically grab a slave and a workspace (no extra node block is required).
  2. Pull the requested image to the Docker server (if not already cached).
  3. Start a container running that image.
  4. Mount the Jenkins workspace as a “volume” inside the container, using the same file path.

You can see more detail what happens in inside here: https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/chapter-docker-workflow.html

Upvotes: 1

Related Questions