Skip to content

Commit

Permalink
Added base repo (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
desholmes authored Jan 15, 2021
1 parent 460f5e5 commit 7a3b8cc
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.git*
.env*
Makefile
README.md
8 changes: 8 additions & 0 deletions .env-dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AZURE_TENANT=000-000-000-000
AZURE_SUBSCRIPTION=000-000-000-000
VERSION=0.0.0
REPO=tbc
REGISTRY_NAME=tbc
REGISTRY_USERNAME=addme
REGISTRY_PASSWORD=addme
DRY_RUN=1
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Environments
.env
# MISC
.DS_Store
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM mcr.microsoft.com/azure-cli:2.9.1

ARG APP_VERSION
ENV APP_VERSION=$APP_VERSION

WORKDIR /usr/src

COPY untag.sh .
RUN chmod +x untag.sh

ENTRYPOINT ["bash", "/usr/src/untag.sh"]
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.PHONY: clean-dangling-images delete-image \
build push pull run run-clean

-include .env

export REGISTRY=desholmes
export REPOSITORY=acr-rc-deleter
export VERSION=0.1.0

clean-dangling-images:
@docker rmi -f $$(docker images -f 'dangling=true' -q)

delete-image:
@docker rmi -f $(REGISTRY)/$(REPOSITORY):$(VERSION)

build:
@make -s clean-dangling-images &
docker build \
--build-arg APP_VERSION="$(VERSION)" \
-t $(REGISTRY)/$(REPOSITORY):$(VERSION) .

docker build --build-arg APP_VERSION=0.1.0 -t desholmes/acr-rc-deleter:latest .

build-and-push:
@make -s build
@make -s push

push:
docker push $(REGISTRY)/$(REPOSITORY):$(VERSION)

run:
@make -s build
@docker run -it \
-e AZURE_TENANT=$(AZURE_TENANT) \
-e AZURE_SUBSCRIPTION=$(AZURE_SUBSCRIPTION) \
-e REGISTRY_NAME=$(REGISTRY_NAME) \
-e REGISTRY_USERNAME=$(REGISTRY_USERNAME) \
-e REGISTRY_PASSWORD=$(REGISTRY_PASSWORD) \
-e REPO=$(REPO) \
-e VERSION=$(VERSION) \
-e DRY_RUN=$(DRY_RUN) \
-v $(PWD)/untag.sh:/usr/src/untag.sh \
$(REGISTRY)/$(REPOSITORY):$(VERSION)

run-clean:
@make -s delete-image & make build
@make -s run
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Azure Container Registry Release Candidate Docker Image Deleter

A [dockerised](https://hub.docker.com/repository/docker/desholmes/acr-rc-deleter) bash script using the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/) to delete [Release Candidate (RC)](https://semver.org/spec/v2.0.0-rc.1.html) docker image tags from [Azure Container Registry (ACR)](https://docs.microsoft.com/en-us/azure/container-registry/).

Before using the docker container in your pipeline you'll need to create a[service principle with access to ACR](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-service-principal).

## Usage

See the [Environment Variables](#Environment-Variables) table below.

### Command

The following example would check the repo `regname/repo-name` for [RC](https://semver.org/spec/v2.0.0-rc.1.html) tags of `0.12.2`, but not delete them:

```bash
docker run -it \
-e AZURE_TENANT=000-000-000-000 \
-e AZURE_SUBSCRIPTION=000-000-000-000 \
-e REGISTRY_NAME=regname \
-e REGISTRY_USERNAME=username \
-e REGISTRY_PASSWORD=password \
-e REPO=repo-name \
-e VERSION=0.12.2 \
-e DRY_RUN=1 \
desholmes/acr-rc-deleter:0.1.0
```

### Environment Variables

| Environment Variable | Description |
|---|---|
|`AZURE_TENANT`|[Locate your Azure Account Tenant ID](https://microsoft.github.io/AzureTipsAndTricks/blog/tip153.html).|
|`AZURE_SUBSCRIPTION`|[Locate your Azure Subscription ID](https://docs.bitnami.com/azure/faq/administration/find-subscription-id/).|
|`REGISTRY_NAME`|[Locate your ACR name](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal).|
|`REGISTRY_USERNAME`|The [service principle](https://open.spotify.com/track/2SkypU3flBcYuyyFqfcsTR) username.|
|`REGISTRY_PASSWORD`|The [service principle](https://open.spotify.com/track/2SkypU3flBcYuyyFqfcsTR) password.|
|`REPO`|The docker repo you want to check/delete the tags from|
|`VERSION`|The stable version of your tag, ie `0.1.0` for release candidates `0.1.0-rc.15`, `0.1.0-rc.15`|
|`DRY_RUN`|Boolean `1` to output the number of tags, `0` to delete the tags|

## Development

The docker image tag, registry and repo are tracked in the [Makefile](./Makefile).

### Make Commands

Make commands are included in this repo to automate the repetitive tasks. Copy [.env-dist](.env-dist) to `.env` and populate the details before using the commands below.

| Command | Description |
|---|---|
|`make build`|Builds the docker image.|
|`make build-push`|Runs `make build` and `make push`.|
|`make clean-dangling-images`|Removes intermediate docker images.|
|`make delete-image`| Removes the docker image based.|
|`make push`|Pushes the docker image into the registry.|
|`make run`|Runs the built docker image as a container bind mounts the `./app/` folder into the container for live reloading.|
|`make run-clean`|Runs `make delete-image`, `make build` and `make run`.|
100 changes: 100 additions & 0 deletions untag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

set -e

echoError() { echo "Error: $@" >&2; }
echoInfo() { echo "Info: $@"; }

echoInfo "ACR RC Untagger (v$APP_VERSION)"

# Check required environment variables are set
for envVar in AZURE_TENANT \
AZURE_SUBSCRIPTION \
VERSION \
REPO \
REGISTRY_NAME \
REGISTRY_USERNAME \
REGISTRY_PASSWORD \
DRY_RUN; do
if [[ -z "${!envVar}" ]];
then
echoError "Required environment variable '$envVar' isn't set, script exiting."
exit 1
fi
done

## Dry run message
if [[ -z ${DRY_RUN} || ${DRY_RUN} != "1" ]];
then
echoInfo "DRY_RUN is disabled, rc tags will be untagged"
else
echoInfo "DRY_RUN is enabled, rc tags won't be untagged"
fi

echoInfo "Attempting to log into Azure"
az login --service-principal \
-u "$REGISTRY_USERNAME" \
-p "$REGISTRY_PASSWORD" \
--tenant "$AZURE_TENANT" -o none

if [ $? -eq 0 ];
then
echoInfo "Login successful"
else
echoError "Login failed (exit code: $?), check the credentials are correct"
exit 1
fi

function remove_rc_tags {
## Fetch RC images
echoInfo "Attempting to fetch rc tags for repo: '$1', version: '$VERSION'"

# Note: Incorrect REGISTRY_NAME or REPO will display 'az acr' message and exit 1
allTags=$(az acr repository show-tags --subscription "$AZURE_SUBSCRIPTION" --name "$REGISTRY_NAME" --repository "$1")

rcTags=$(echo "$allTags" | jq -c '[.[] | select(contains ("'$VERSION'rc"))]')
rcCount=$(echo "$rcTags" | jq -c '. | length')

# Do we have rc tags?
if [[ -z ${rcCount} || ${rcCount} == "0" ]];
then
echoInfo "No rc tags found, exiting"
return
fi

echoInfo "Fetching rc tags successful. Found: '$rcCount'"

# Loop through RC tags and create a report, or untag them
for tag in $(echo "${rcTags}" | jq -r '.[]');
do
if [[ -z ${DRY_RUN} || ${DRY_RUN} != "1" ]];
then
az acr repository untag --subscription "$AZURE_SUBSCRIPTION" --name "$REGISTRY_NAME" --image "$1:$tag"
echoInfo "Untagged: '$1:$tag'"
else
echoInfo "DRY RUN - '$1:$tag' would have been untagged"
fi
done
}

while IFS=',' read -ra ADDR; do
for i in "${ADDR[@]}"; do
remove_rc_tags "$i"

echoInfo "Removing untagged manifests from the repository '$i'"

UNTAGGED_MANIFESTS=$(az acr repository show-manifests --subscription "$AZURE_SUBSCRIPTION" --name "$REGISTRY_NAME" --repository "$i" --query "[?tags[0]==null].digest" -o tsv)

if [[ -z ${DRY_RUN} || ${DRY_RUN} != "1" ]];
then
echoInfo "DRY_RUN is disabled, manifests with no tags will be deleted"
echo "$UNTAGGED_MANIFESTS" | xargs -I% az acr repository delete --subscription "$AZURE_SUBSCRIPTION" --name "$REGISTRY_NAME" --image $i@% --yes
else
echoInfo "DRY_RUN is enabled, manifests with no tags won't be deleted"
echo "$UNTAGGED_MANIFESTS" | xargs -I% echo "Info: Manifest % would have been deleted"
fi

done
done <<< "$REPO"

echoInfo "ACR RC Untagger complete"

0 comments on commit 7a3b8cc

Please sign in to comment.