Skip to content

Commit e995008

Browse files
committed
Initial code
1 parent f5bc776 commit e995008

File tree

11 files changed

+1381
-1
lines changed

11 files changed

+1381
-1
lines changed

.github/workflows/builder.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Create and publish a Docker image
2+
3+
on: [push]
4+
5+
env:
6+
REGISTRY: ghcr.io
7+
IMAGE_NAME: ${{ github.repository }}
8+
9+
jobs:
10+
build-and-push-image:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
packages: write
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v2
19+
20+
- name: Log in to the Container registry
21+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
22+
with:
23+
registry: ${{ env.REGISTRY }}
24+
username: ${{ github.actor }}
25+
password: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: Read builder-version
28+
id: builder
29+
uses: juliangruber/read-file-action@v1
30+
with:
31+
path: ./builder-version
32+
33+
- name: Build and push Docker image
34+
uses: docker/build-push-action@v2
35+
with:
36+
context: .
37+
file: Dockerfile.build
38+
push: true
39+
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.builder.outputs.content }}

.github/workflows/test.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
name: Test-Action
8+
runs-on: ubuntu-latest
9+
continue-on-error: true
10+
steps:
11+
- name: Checkout Repository
12+
uses: actions/checkout@v2
13+
- name: Test Action
14+
id: test
15+
uses: ./
16+
with:
17+
base-image: alpine:3.13
18+
image: alpine:3.14
19+
- name: Get Test Output
20+
run: echo "Workflow Docker Image ${{ steps.test.outputs.needs-update }}"
21+
- name: Check value
22+
run: echo "Needs updating"
23+
if: steps.test.outputs.needs-update == 'true'

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM ghcr.io/mkumatag/container-image-updater-action:v1.0
2+
3+
# Copies your code file from your action repository to the filesystem path `/` of the container
4+
#COPY entrypoint.sh /entrypoint.sh
5+
6+
# Code file to execute when the docker container starts up (`entrypoint.sh`)
7+
ENTRYPOINT ["/usr/local/bin/container-image-updater"]

Dockerfile.build

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# syntax=docker/dockerfile:1.3
2+
# reuse this multistage once the issue resolved with manifest-tool, rightnow inspect with --raw option is not returning all the layers
3+
FROM golang:1.17-alpine3.14 as builder
4+
5+
WORKDIR /app
6+
COPY go.mod ./
7+
COPY go.sum ./
8+
9+
RUN go mod download
10+
11+
COPY *.go ./
12+
13+
RUN go build -o /container-image-updater
14+
15+
FROM alpine:3.14
16+
17+
COPY --from=builder /container-image-updater /usr/local/bin/container-image-updater
18+
19+
#ADD --chmod=0755 https://github.com/estesp/manifest-tool/releases/download/v1.0.3/manifest-tool-linux-amd64 /usr/local/bin/manifest-tool
20+
21+
#RUN apk add jq

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
# container-image-updater
1+
# container-image-updater
2+
This action prints "true" if image is required to update based on the base image update.
3+
4+
## Inputs
5+
6+
| Name | Type | Description |
7+
|---------------------|----------|------------------------------------|
8+
| `base-image` | Required | Base image of the image |
9+
| `image` | Required | The container image to be monitored, based on `base-image` |
10+
| `base-reg-username`, <br>`base-reg-password` | Optional | Image registry credential to access base image.|
11+
| `image-reg-username`, <br>`image-reg-password` | Optional | Image registry credential to access image to be monitored.|
12+
13+
14+
## Outputs
15+
16+
| Name | Description |
17+
|---------------------|------------------------------------|
18+
| `needs-update` | Returns `true` or `false`. |
19+
20+
## Example usage
21+
22+
### Public images
23+
24+
```yaml
25+
uses: mkumatag/[email protected]
26+
with:
27+
base-image: 'alpine:3.14'
28+
image: 'alpine:3.13'
29+
```
30+
31+
### Private images
32+
33+
```yaml
34+
uses: mkumatag/[email protected]
35+
with:
36+
base-image: 'alpine:3.14'
37+
image: 'alpine:3.13'
38+
base-reg-username: someuser
39+
base-reg-password: somepassword
40+
image-reg-username: someuser
41+
image-reg-password: somepassword
42+
```

action.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# action.yml
2+
name: 'Container Image Updater'
3+
description: 'Github action to notify if the container image needs update based on the baseimage change'
4+
inputs:
5+
base-image:
6+
description: "Container Base image"
7+
required: true
8+
image:
9+
description: "Container image"
10+
required: true
11+
base-reg-username:
12+
description: "Base Image Container registry username"
13+
required: false
14+
base-reg-password:
15+
description: "Base Image Container registry password"
16+
required: false
17+
image-reg-username:
18+
description: "Container registry username"
19+
required: false
20+
image-reg-password:
21+
description: "Container registry password"
22+
required: false
23+
outputs:
24+
needs-update:
25+
description: 'Needs the update or not(true or false)'
26+
27+
runs:
28+
using: 'docker'
29+
image: 'Dockerfile'
30+
args:
31+
- --base-image
32+
- ${{ inputs.base-image }}
33+
- --image
34+
- ${{ inputs.image }}
35+
- --base-reg-username
36+
- ${{ inputs.base-reg-username }}
37+
- --base-reg-password
38+
- ${{ inputs.base-reg-password }}
39+
- --image-reg-username
40+
- ${{ inputs.image-reg-username }}
41+
- --image-reg-password
42+
- ${{ inputs.image-reg-password }}

builder-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v1.0

entrypoint.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env sh
2+
3+
4+
set -o pipefail
5+
set -e
6+
7+
BASE_IMAGE=${1:-}
8+
IMAGE=${2:-}
9+
BASE_REG_USERNAME=${3:-}
10+
BASE_REG_PASSWORD=${4:-}
11+
IMAGE_REG_USERNAME=${5:-}
12+
IMAGE_REG_PASSWORD=${6:-}
13+
14+
if [[ -z "${BASE_IMAGE}" || -z "${IMAGE}" ]]; then
15+
echo "::error title=argument::Missing Argument base-image or image"
16+
exit 1
17+
fi
18+
19+
get_layers(){
20+
image=$1
21+
username=$2
22+
password=$3
23+
cmd="manifest-tool"
24+
if [ -n "${username}" ] && [ -n "${password}" ]; then
25+
cmd="${cmd} --username=${username} --password=${password}"
26+
fi
27+
28+
cmd="${cmd} inspect ${image} --raw"
29+
op=$($cmd)
30+
retval=$?
31+
if [ $retval -ne 0 ]; then
32+
echo "::error title=get_layers::Failed to get layers for ${image}"
33+
return $retval
34+
fi
35+
layers=$(echo ${op} | jq -r ".[]|.Layers[]?")
36+
echo "$layers"
37+
}
38+
39+
get_base_layers(){
40+
get_layers "${BASE_IMAGE}" "${BASE_REG_USERNAME}" "${BASE_REG_PASSWORD}"
41+
}
42+
43+
get_image_base_layer(){
44+
# returns only the first layer(base layer)
45+
get_layers "${IMAGE}" "${IMAGE_REG_USERNAME}" "${IMAGE_REG_PASSWORD}" | head -n 1
46+
}
47+
48+
base_layers=$(get_base_layers)
49+
50+
image_base_layer=$(get_image_base_layer)
51+
52+
found=$(echo "${base_layers}" | grep -c "${image_base_layer}")
53+
retval=$?
54+
if [ "$found" -gt 0 ]; then
55+
echo "::set-output name=needs-update::false"
56+
else
57+
echo "::set-output name=needs-update::true"
58+
fi

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/mkumatag/container-image-updater-action
2+
3+
go 1.16
4+
5+
require (
6+
github.com/estesp/manifest-tool/v2 v2.0.0-beta.1
7+
github.com/opencontainers/go-digest v1.0.0
8+
github.com/opencontainers/image-spec v1.0.1
9+
github.com/sirupsen/logrus v1.8.1
10+
)

0 commit comments

Comments
 (0)