title: What is the correct usage of cache/artifacts in Gitlab CI?
body: <p>I am facing an issue when cached files are not used in project builds. In my case, I want to download composer dependencies in build stage and then add them into final project folder after all other stages succeeds. I thought that if you set <code>cache</code> attribute into <code>.gitlab-ci.yml</code> file, it will be shared and used in other stages as well. But this sometime works and sometimes not.</p>
  
  <p><strong>Gitlab version is 9.5.4</strong></p>
  
  <p>Here is my <code>.gitlab-ci.yml</code> file:</p>
  
  <pre><code>image: ponk/debian:jessie-ssh
  
  variables:
      WEBSERVER: "user@example.com"
      WEBSERVER_DEPLOY_DIR: "/domains/example.com/web-presentation/deploy/"
      WEBSERVER_CDN_DIR: "/domains/example.com/web-presentation/cdn/"
      TEST_VENDOR: '[ "$(ls -A ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}/vendor)" ]'
  
  cache:
    key: $CI_PIPELINE_ID
    untracked: true
    paths:
      - vendor/
  
  before_script:
  
  
  stages:
      - build
      - tests
      - deploy
      - post-deploy
  
  Build sources:
      image: ponk/php5.6
      stage: build
      script:
          # Install composer dependencies
          - composer -n install --no-progress
      only:
          - tags
          - staging
  
  
  Deploy to Webserver:
      stage: deploy
      script:
          - echo "DEPLOYING TO ... ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}"
          - ssh $WEBSERVER mkdir -p ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}
          - rsync -rzha app bin vendor www .htaccess ${WEBSERVER}:${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}
          - ssh $WEBSERVER '${TEST_VENDOR} &amp;&amp; echo "vendor is not empty, build seems ok" || exit 1'
          - ssh $WEBSERVER [ -f ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}/vendor/autoload.php ] &amp;&amp; echo "vendor/autoload.php exists, build seems ok" || exit 1
          - echo "DEPLOYED"
      only:
          - tags
          - staging
  
  Post Deploy Link PRODUCTION to Webserver:
      stage: post-deploy
      script:
          - echo "BINDING PRODUCTION"
          - ssh $WEBSERVER unlink ${WEBSERVER_DEPLOY_DIR}production-latest || true
          - ssh $WEBSERVER ln -s ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${WEBSERVER_DEPLOY_DIR}production-latest
          - echo "BOUNDED  $CI_COMMIT_SHA -&gt; production-latest"
          - ssh $WEBSERVER sudo service php5.6-fpm reload
      environment:
          name: production
          url: http://www.example.com
      only:
          - tags
  
  Post Deploy Link STAGING to Webserver:
      stage: post-deploy
      script:
          - echo "BINDING STAGING"
          - ssh $WEBSERVER unlink ${WEBSERVER_DEPLOY_DIR}staging-latest || true
          - ssh $WEBSERVER ln -s ${WEBSERVER_DEPLOY_DIR}${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${WEBSERVER_DEPLOY_DIR}staging-latest
          - echo "BOUNDED  ${CI_COMMIT_SHA} -&gt; staging-latest"
          - ssh $WEBSERVER sudo service php5.6-fpm reload
      environment:
          name: staging
          url: http://staging.example.com
      only:
          - staging
  </code></pre>
  
  <p>In <a href="https://docs.gitlab.com/ee/ci/yaml/README.html#cache" rel="noreferrer">Gitlab documentation</a> it says: <code>cache is used to specify a list of files and directories which should be cached between jobs.</code></p>
  
  <p>From what I understand I've set up cache correctly - I have untracked set to true, path includes vendor folder and key is set to Pipeline ID, which should be the same in other stages as well.</p>
  
  <p>I've seen some set ups which contained <code>Artifacts</code>, but unless you use it with <code>Dependencies</code>, it shouldn't have any effect.</p>
  
  <p>I don't know what I'm doing wrong. I need to download composer dependencies first, so I can copy them via <code>rsync</code> in next stage. Do you have any ideas/solutions? Thanks</p>