Understanding gitlab ci artifacts, using with nightwatchjs

262 Views Asked by At

I'm not really following the artifacts part of gitlab ci too well, and every attempt to do what i want doesn't work.

I have a build and test stage, the build stage uses the Dockerfile in my project to build and image and push to registry, the test stage then uses the image to run nightwatch tests. The dockerfile basically installs chrome and other things needed for the nightwatch test.

I need to be able to get hold of screenshots nightwatch takes during the tests and i thought artifacts would do that but i never end up with screenshots in the downloaded zip, only empty folders.

I think maybe i am misunderstanding the artifacts part. The folders i tell it to use exists in the download but no screenshots are ever in the folders.

So the basic ci would look like this (there is more to it but this is the essentials):

build-develop:
    tags:
        - shell
    stage: build
    only:
        - develop
    script:
        - docker build --platform linux/amd64 -t myusername/myimage:latest .
        - docker image push myusername/myimage:latest

test:
    timeout: 15m
    retry:
        &retry
        max: 2
        when:
            - job_execution_timeout
            - stuck_or_timeout_failure
            - script_failure
    image:
        &image
        name: myusername/myimage:latest
        entrypoint: [ "" ]

    tags:
        - docker
    stage: test
    only:
        - develop
    artifacts:
        &artifacts
        when: always
        paths:
            - screenshots/
            - tests_output/
        expire_in: 1 hour
    script:
        - cd /var/tests/ && nightwatch --env int --test tests/my-test.js

So the code is all taken into /var/tests from when the dockerfile builds the image. I have a WORKDIR set in the image to /var/tests so i assumed that artifacts would be mapped to /var/tests/screenshots but it clearly isn't - or there are some file writing issues in the container it runs?

I'm not sure where to go from here.

1

There are 1 best solutions below

2
Richard On

By default, GitLab runs commands in the /builds/<project-path> directory. See, GitLab Docs: Docker: Where scripts are executed. Verify this by putting - pwd at the top of the scripts: section of .gitlab-ci.yml, and watching the output of the job.

Typically, all CI/CD commands should be run from the container's WORKDIR, the path within the container where you copied your code. So, simply cd /path/to/workdir as the first command in your script. Then, execute your other commands.

Artifact paths should be relative to your WORKDIR.