Getting "404 Group Not Found" when trying to deploy composer package

I just created a new project and I’m using a .gitlab-ci.yml to build a composer package, but am getting a “404 Group Not Found” error:

The “https://gitlab.com/api/v4/group/996XXXX/-/packages/composer/packages.json” file could not be downloaded (HTTP/2 404 ):
{“message”:“404 Group Not Found”}

I copied the .gitlab-ci.yml file from another project (where it is still working fine to create a composer package). The projects are both part of the same group.

I cannot figure out why it works on the old project, but not on the new one. Here’s the deploy section of my yml file:

deploy_composer:
      stage: deploy
      variables:
          URL: "$CI_SERVER_PROTOCOL://$CI_SERVER_HOST:$CI_SERVER_PORT/api/v4/projects/$CI_PROJECT_ID/packages/composer?job_token=$CI_JOB_TOKEN"
      script:
      - version=$([[ -z "$CI_COMMIT_TAG"  ]] && echo "branch=$CI_COMMIT_REF_NAME" || echo "tag=$CI_COMMIT_TAG")
      - insecure=$([ "$CI_SERVER_PROTOCOL" = "http"  ] && echo "--insecure" || echo "")
      - response=$(curl -s -w "\n%{http_code}" $insecure --data $version $URL)
      - code=$(echo "$response" | tail -n 1)
      - body=$(echo "$response" | head -n 1)
      # Output state information
      - if [ $code -eq 201  ]; then
          echo "Package created - Code $code - $body";
        else
          echo "Could not create package - Code $code - $body";
          exit 1;
        fi

I don’t know if it’s the best way, but I figured out a solution to my problem…

First, I created a GITLAB_TOKEN variable (I made it a group variable because I have several projects in the same group). I set it to the value in my auth.json file.

Then I added this to my .gitlab-ci.yml file:

before_script:
- 'mkdir -p ~/.composer/ && echo "{\"gitlab-token\": {\"gitlab.com\": \"$GITLAB_TOKEN\"}}" > ~/.composer/auth.json'

Now everything works fine and it creates a composer package.

For those who are interested, here’s a complete copy of my .gitlab-ci.yml file:

cache:
    paths:
    - vendor/

stages:
- test
- deploy

test:
    stage: test
    # select image from https://hub.docker.com/_/php
    image: php:7.4

    before_script:
    - 'mkdir -p ~/.composer/ && echo "{\"gitlab-token\": {\"gitlab.com\": \"$GITLAB_TOKEN\"}}" > ~/.composer/auth.json'

    # Install git, the php image does not have it
    - apt-get update -yqq
    - apt-get install git -yqq

    # the "--2" option installs version 2 of composer
    - curl -sS https://getcomposer.org/installer | php -- --2
    - php -v

    # Install project dependencies
    #- php composer.phar install --prefer-dist -a --no-progress
    - echo "Skip project dependencies"

    script:
    #- ./vendor/bin/phpunit -v --coverage-text --colors=never --stderr
    - echo "Skip phpunit"

deploy_composer:
    stage: deploy
    variables:
        URL: "$CI_SERVER_PROTOCOL://$CI_SERVER_HOST:$CI_SERVER_PORT/api/v4/projects/$CI_PROJECT_ID/packages/composer?job_token=$CI_JOB_TOKEN"
    script:
    - version=$([[ -z "$CI_COMMIT_TAG"  ]] && echo "branch=$CI_COMMIT_REF_NAME" || echo "tag=$CI_COMMIT_TAG")
    - insecure=$([ "$CI_SERVER_PROTOCOL" = "http"  ] && echo "--insecure" || echo "")
    - response=$(curl -s -w "\n%{http_code}" $insecure --data $version $URL)
    - code=$(echo "$response" | tail -n 1)
    - body=$(echo "$response" | head -n 1)
    # Output state information
    - if [ $code -eq 201  ]; then
        echo "Package created - Code $code - $body";
      else
        echo "Could not create package - Code $code - $body";
        exit 1;
      fi