# Artifacts and Versions

Course: [CI/CD](https://www.devtutorials.ai/learn/cicd) · Section: Continuous Integration
Source: https://www.devtutorials.ai/learn/cicd/continuous-integration/artifacts-and-versions

> Save the build output as a versioned artifact, pass it to the next job, and deploy the same tested artifact to staging and production. Never rebuild after tests.

## Artifacts and Versions

This lesson fixes a quiet but serious problem:

- Staging gets one build.
- Production gets a **different** build, made later.
- Nobody tested the production build.

You will:

- save the build output as an artifact.
- give it a version name.
- pass it to the next job.
- deploy the same artifact to staging and production.

**Simple lesson:** what you tested must be exactly what users get.

### A Real-World Example

Think about a sealed exam paper.

- The paper is printed **once** and sealed with a number.
- The same sealed packet goes to every exam hall.
- Nobody prints a fresh copy at the hall door.
- If something is wrong, the number tells you which packet.

Now map it:

- **Printing once** = build once.
- **Sealed packet with a number** = versioned artifact.
- **Exam halls** = staging and production.

**Simple lesson:** build once, seal it with a version, send the same packet everywhere.

## What Goes in an Artifact

**Artifact** = files saved from a run so other jobs or people can use them.

What to save depends on your app:

| App | Build command | Save this folder |
|---|---|---|
| React + TypeScript | `npm run build` | `dist/` (HTML, JS, CSS) |
| ASP.NET Core | `dotnet publish -c Release -o out` | `out/` (DLLs and settings) |

What **not** to save:

- `node_modules/` or the NuGet cache. Too big, and the cache does that job.
- Secrets or `.env` files. Artifacts can be downloaded by people with access.

**Real tools vs this lab:** some teams package the app as a container image instead of a folder. That is another kind of artifact with the same rule: build once, deploy the same image. You build images in Containers in CI.

**Simple lesson:** save the ready-to-run folder, nothing more.

## Versions

A version name answers: "Which build is this?"

Three common kinds:

- **Release number**, like `1.4.2`. People use it. Bigger first number = bigger change.
- **Commit SHA**, like `3f9c2ab`. Points to the exact code.
- **Build number**, like `20261116.42`. Counts pipeline runs.

Many teams use more than one: `1.4.2+3f9c2ab`.

| Platform | The commit | The build number |
|---|---|---|
| GitHub Actions | `${{ github.sha }}` | `${{ github.run_number }}` |
| Azure Pipelines | `$(Build.SourceVersion)` | `$(Build.BuildNumber)` |

**Simple lesson:** a version name makes "which build is live?" a one-second answer.

## Passing Artifacts Between Jobs

- The build job **uploads** the artifact.
- The deploy job **downloads** it.
- The deploy job must **wait** for the build job.

| Platform | Save | Get |
|---|---|---|
| GitHub Actions | `actions/upload-artifact` | `actions/download-artifact` |
| Azure Pipelines | `PublishPipelineArtifact@1` | `DownloadPipelineArtifact@2` |

In Azure, a **deployment** job downloads the run's artifacts by itself. A normal job needs the download task. In GitHub, every job needs the download step.

**Simple lesson:** upload once in build. Download in every job that needs it.

## Build Once, Promote Everywhere

**Promote** = move the same artifact to the next environment.

Why a second build is risky:

- A package may have a new patch release since the first build.
- A setting on the runner may be different.
- The result can differ, even with the same code.

- After the run, look at the build numbers under each environment.
- Both should show the same build.

If a deploy job has `npm run build` or `dotnet publish` in it, it is building again. Delete that step and download the artifact instead.

**Simple lesson:** never rebuild after tests. Promote the tested artifact.

### Fix It: Save After the Build

**Simple lesson:** you can only save what the build has already made.

1. Open any green run of your starter repo.
2. Scroll to the bottom of the run summary.
3. Find the run number and the commit SHA at the top.

**What you should see:** every run has its own number and SHA. Your image tags use the same SHA.

## Quick Check

## Cheat Sheet

GitHub Actions:

```yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm ci
      - run: npm run build
      - run: npm test
      - uses: actions/upload-artifact@v4
        with:
          name: web-${{ github.sha }}
          path: dist

  deploy-staging:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: web-${{ github.sha }}
          path: dist
      # sign in to Azure first (Environments and Secrets)
      - uses: azure/webapps-deploy@v3
        with:
          app-name: shop-web-staging
          package: dist
```

Azure Pipelines:

```yaml
jobs:
  - job: build
    steps:
      - script: dotnet restore
      - script: dotnet test --no-restore
      - script: dotnet publish Shop -c Release -o out
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: out
          artifact: web-$(Build.BuildNumber)

  - deployment: deploy_staging
    dependsOn: build
    environment: staging
    strategy:
      runOnce:
        deploy:
          steps:
            - task: AzureWebApp@1
              inputs:
                azureSubscription: shop-staging-oidc
                appType: webAppLinux
                appName: shop-web-staging
                package: $(Pipeline.Workspace)/web-$(Build.BuildNumber)
```

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Rebuilding in the deploy job | It is the same code | It may not be the same build | Untested bits reach users |
| Same artifact name every run | "web" is clear enough | Add the SHA or build number | You cannot tell builds apart |
| Saving before building | Order does not matter | Save after build or publish | "No files were found" |
| Deploy job with no needs | The file order is the run order | Jobs start together | "Artifact not found" |
| Secrets in the artifact | It is private | Others can download it | A key leaks |

## What to Remember

- Artifact = the ready-to-run folder, saved by the pipeline.
- Name it with a version: SHA, build number, or both.
- Upload in build, download where needed, with needs or dependsOn.
- Build once. Promote the same artifact.

## Exercises

Next: **Containers in CI**
