Skip to content

Commit 29534b9

Browse files
committed
Add improved workflow for pushing Docker images
1 parent df63429 commit 29534b9

3 files changed

Lines changed: 129 additions & 66 deletions

File tree

.github/workflows/docker-push.yml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: "Docker Push"
2+
3+
on:
4+
release:
5+
types: [published]
6+
schedule:
7+
- cron: '30 12 * * *'
8+
9+
permissions:
10+
packages: write
11+
contents: read
12+
13+
env:
14+
IMAGE_NAME: ${{ github.repository }}
15+
16+
jobs:
17+
Docker:
18+
name: Docker Build & Publish
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
with:
25+
fetch-depth: 0
26+
27+
- name: Login to DockerHub
28+
uses: docker/login-action@v2
29+
with:
30+
username: ${{ secrets.DOCKERHUB_USERNAME }}
31+
password: ${{ secrets.DOCKERHUB_TOKEN }}
32+
33+
- name: Login to GitHub Container Registry
34+
uses: docker/login-action@v2
35+
with:
36+
registry: ghcr.io
37+
username: ${{ github.actor }}
38+
password: ${{ secrets.GITHUB_TOKEN }}
39+
40+
- name: Prepare metadata for build
41+
id: metadata
42+
run: |
43+
rev=$(git rev-list --tags --max-count=1)
44+
version=$(git describe --tags "$rev" | tr -d 'v')
45+
46+
if [[ -z "$version" ]] ; then
47+
echo "::error::Can't find version info"
48+
exit 1
49+
fi
50+
51+
docker_file="Dockerfile"
52+
base_image=$(grep 'FROM ' $docker_file | tail -1 | cut -f2 -d' ')
53+
54+
if [[ -z "$base_image" ]] ; then
55+
echo "::error::Can't extract base image info"
56+
exit 1
57+
fi
58+
59+
echo "::set-output name=version::$version"
60+
echo "::set-output name=dockerfile::$docker_file"
61+
echo "::set-output name=baseimage::$base_image"
62+
63+
echo -e "\033[34mVersion:\033[0m $version"
64+
echo -e "\033[34mDockerfile:\033[0m $docker_file"
65+
echo -e "\033[34mBase image:\033[0m $base_image"
66+
67+
- name: Check if build/rebuild is required
68+
id: build_check
69+
run: |
70+
if [[ "${{github.event_name}}" == "release" ]] ; then
71+
echo "::set-output name=build::true"
72+
exit 0
73+
fi
74+
75+
echo -e "::group::\033[34mDownloading built image…\033[0m"
76+
77+
if ! docker pull ghcr.io/${{env.IMAGE_NAME}}:latest ; then
78+
echo "::error::Can't download image ghcr.io/${{env.IMAGE_NAME}}:latest"
79+
exit 1
80+
fi
81+
82+
echo "::endgroup::"
83+
echo -e "::group::\033[34mDownloading base image…\033[0m"
84+
85+
if ! docker pull ${{steps.metadata.outputs.baseimage}} ; then
86+
echo "::error::Can't download image ${{steps.metadata.outputs.baseimage}}"
87+
exit 1
88+
fi
89+
90+
echo "::endgroup::"
91+
92+
base_layer=$(docker inspect "${{steps.metadata.outputs.baseimage}}" | jq -r '.[0].RootFS.Layers[-1]')
93+
94+
if [[ -z "$base_layer" ]] ; then
95+
echo "::error::Can't extract layers info from base image"
96+
exit 1
97+
fi
98+
99+
if ! docker inspect "ghcr.io/${{env.IMAGE_NAME}}:latest" | jq -r '.[0].RootFS.Layers' | grep -q "$base_layer" ; then
100+
echo "::warning::Rebuild image (reason: base image rebuilt)"
101+
echo "::set-output name=build::true"
102+
exit 0
103+
fi
104+
105+
- name: Build and push Docker image
106+
if: ${{ steps.build_check.outputs.build == 'true' }}
107+
uses: docker/build-push-action@v3
108+
with:
109+
push: true
110+
context: .
111+
file: ${{steps.metadata.outputs.dockerfile}}
112+
tags: |
113+
ghcr.io/${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
114+
ghcr.io/${{env.IMAGE_NAME}}:latest
115+
${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
116+
${{env.IMAGE_NAME}}:latest
117+
118+
- name: Show info about built Docker image
119+
uses: essentialkaos/docker-info-action@v1
120+
with:
121+
image: ${{env.IMAGE_NAME}}:latest
122+
show-labels: true

.github/workflows/ghcr.yml

Lines changed: 0 additions & 59 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<p align="center"><a href="#readme"><img src="https://gh.kaos.st/sslcli.svg"/></a></p>
22

33
<p align="center">
4-
<a href="https://goreportcard.com/report/github.com/essentialkaos/sslcli"><img src="https://goreportcard.com/badge/github.com/essentialkaos/sslcli"></a>
5-
<a href="https://codebeat.co/projects/github-com-essentialkaos-sslcli-master"><img src="https://codebeat.co/badges/edc52bb1-c807-470b-8466-b86cc0cfcdbe"></a>
6-
<a href="https://github.com/essentialkaos/sslcli/actions"><img src="https://github.com/essentialkaos/sslcli/workflows/CI/badge.svg" alt="GitHub Actions Status" /></a>
7-
<a href="https://github.com/essentialkaos/sslcli/actions?query=workflow%3ACodeQL"><img src="https://github.com/essentialkaos/sslcli/workflows/CodeQL/badge.svg" /></a>
4+
<a href="https://kaos.sh/w/sslcli/ci"><img src="https://kaos.sh/w/sslcli/ci.svg" alt="GitHub Actions CI Status" /></a>
5+
<a href="https://kaos.sh/r/sslcli"><img src="https://kaos.sh/r/sslcli.svg" alt="GoReportCard" /></a>
6+
<a href="https://kaos.sh/b/sslcli"><img src="https://kaos.sh/b/edc52bb1-c807-470b-8466-b86cc0cfcdbe.svg" alt="codebeat badge" /></a>
7+
<a href="https://kaos.sh/w/sslcli/codeql"><img src="https://kaos.sh/w/sslcli/codeql.svg" alt="GitHub Actions CodeQL Status" /></a>
88
<a href="#license"><img src="https://gh.kaos.st/apache2.svg"></a>
99
</p>
1010

@@ -112,9 +112,9 @@ Examples
112112
### Build Status
113113

114114
| Branch | Status |
115-
|--------|--------|
116-
| `master` | [![CI](https://github.com/essentialkaos/sslcli/workflows/CI/badge.svg?branch=master)](https://github.com/essentialkaos/sslcli/actions) |
117-
| `develop` | [![CI](https://github.com/essentialkaos/sslcli/workflows/CI/badge.svg?branch=develop)](https://github.com/essentialkaos/sslcli/actions) |
115+
|------------|--------|
116+
| `master` | [![CI](https://kaos.sh/w/bibop/ci.svg?branch=master)](https://kaos.sh/w/bibop/ci?query=branch:master) |
117+
| `develop` | [![CI](https://kaos.sh/w/bibop/ci.svg?branch=develop)](https://kaos.sh/w/bibop/ci?query=branch:develop) |
118118

119119
### Contributing
120120

0 commit comments

Comments
 (0)