> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/troubleshooting-and-resources/knowledge-base-article/last-published-tag.md).

# Use the <+lastPublished.tag> expression

How the <+lastPublished.tag> expression resolves across different artifact servers, and practices to avoid misdeployments.

The Harness `<+lastPublished.tag>` expression allows for deployment of the latest version published to your artifact servers. Like any powerful expression, it requires a deep understanding to avoid potential pitfalls. This topic describes how `<+lastPublished.tag>` works, its behavior across different artifact servers, and best practices for deploying with confidence.

## How the expression resolves <a href="#unveiling-the-power" id="unveiling-the-power"></a>

Consider a scenario where you manage multiple versions of your application image across different environments. Using an ECR registry, you follow a tagging scheme:

* `v{TIMESTAMP}-dev` for development builds
* `v{GIT_TAG}-stable` for production

Now, you want to deploy the latest dev version to a test environment. This is where `<+lastPublished.tag>` is useful.

Within your run form, you specify `<+lastPublished.tag>.regex(*-dev)`. This expression performs three key actions:

1. **Fetches all tags:** Harness retrieves all available tags stored in your ECR registry.
2. **Sorts the tags:** The retrieved tags are sorted to determine the correct deployment order.
3. **Selects the latest match:** Using the provided regex (for example, `*-dev`), the expression identifies the first tag that matches the specified pattern.

This selected tag becomes your target, deploying the latest development version to your test environment.

## Sorting behavior by artifact server <a href="#navigating-the-quirks" id="navigating-the-quirks"></a>

While `<+lastPublished.tag>` offers convenience, its behavior has nuances to be aware of. Different artifact servers use distinct sorting mechanisms, which affects the behavior of the expression.

### Lexicographic sorting <a href="#lexicographic-sorting" id="lexicographic-sorting"></a>

ECR, for instance, sorts tags lexicographically due to limitations in its API. This means the order of tags primarily relies on their alphabetical characters rather than timestamps. Consequently, even if you push tags in a specific order, their lexicographic order might differ.

For example, consider the following scenario:

You push tags in the following order:

```
v834a275-dev
v6b12c0d-dev
v4df5e8-dev
v92037f3-dev
v745c91a-dev
```

You use `<+lastPublished.tag>.regex(*-dev)`. In this case, despite `v745c91a-dev` being the latest pushed tag, `<+lastPublished.tag>` would deploy `v92037f3-dev` due to lexicographic sorting.

### Timestamp-based sorting <a href="#timestamp-based-sorting" id="timestamp-based-sorting"></a>

Conversely, artifact servers like Nexus provide timestamp information, allowing Harness to sort tags based on their actual publication time. This ensures the latest version, regardless of its lexicographic position, is accurately identified.

## Avoiding misdeployments <a href="#avoiding-misdeployments" id="avoiding-misdeployments"></a>

To prevent potential misdeployments and maintain clarity, consider the following best practice.

### Predictable tagging <a href="#predictable-tagging" id="predictable-tagging"></a>

Use semantic versioning or a consistent tagging strategy, such as appending build numbers or timestamps. This promotes clear understanding and prevents confusion caused by lexicographic sorting.

For example, use tags like `v1.2.3-dev` instead of `v834a275-dev`.

## Next steps

Review the specific sorting behavior and limitations applicable to your chosen artifact server. Go to [Artifact limits and display in the Harness UI](/continuous-delivery/use-continuous-delivery/cd-building-blocks/services/artifact-sources.md#artifact-limits-and-display-in-the-harness-ui) for the sort order of different artifact sources.

{% @harness-feedback/feedback %}
