How to build in multiple steps?

I’m new to Gitlab CI and I’m trying to figure out a problem.

For my first pipeline I want to do the following:

  1. Use an Ubuntu image
  2. Update the apt package database and install a few utilities
  3. Use curl to pull down kubectl
  4. Validate that kubectl works.

Here’s my configuration:

image: ubuntu:focal

  K8S_VERSION: 'v1.19.15'

  stage: build
  - apt -qq update
  - apt -qq -y install curl

  stage: build
    - curl --silent --show-error -LO${K8S_VERSION}/bin/linux/amd64/kubectl && chmod +x kubectl
    - curl --silent --show-error -LO${K8S_VERSION}/bin/linux/amd64/kubectl.sha256
    - echo "$(<kubectl.sha256) kubectl" | sha256sum --check

  stage: test
    - ./kubectl version --short --client

The trouble is, the install-kubectl stage is failing with this error:

$ curl --silent --show-error -LO${K8S_VERSION}/bin/linux/amd64/kubectl.sha256
/usr/bin/bash: line 108: curl: command not found
ERROR: Job failed: exit code 1

I’m confused why curl is not installed, when it should have been installed in the install-apt build stage.

I thought the build stages happened in the order specified in the file. Is that not the case?

My example here is simplified. I my real-world use case, I actually have 1 or 2 more build stages, each with 2-3 commands each, and I’m trying to figure out how to do that without squashing all 10 commands into a single build stage.