Skip to content

Commit 972fb1a

Browse files
committed
feat: publish docker image to ghcr.io
1 parent 17bf472 commit 972fb1a

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: publish-docker-image
2+
on:
3+
workflow_dispatch:
4+
jobs:
5+
build-and-push-image:
6+
runs-on: ubuntu-latest
7+
permissions:
8+
contents: read
9+
packages: write
10+
steps:
11+
- name: docker login
12+
run: docker login ghcr.io -u "${{ github.actor }}" -p "${{ secrets.GITHUB_TOKEN }}"
13+
- name: checkout source
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
fetch-tags: true
18+
- name: docker build and push
19+
env:
20+
GIT_REPO: ${{ github.repository }}
21+
run: ./build-docker.sh push

Dockerfile

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
FROM golang:1.21 as builder
1818

19+
ARG GIT_REPO
1920
ARG GIT_TAG
2021
ARG GIT_COMMIT
2122
ARG BUILD_TIMESTAMP
@@ -26,17 +27,23 @@ RUN ./build.sh
2627

2728
FROM golang:1.21
2829

29-
LABEL org.opencontainers.image.url='https://github.com/apigee/apigee-go-gen' \
30-
org.opencontainers.image.documentation='https://github.com/apigee/apigee-go-gen' \
31-
org.opencontainers.image.source='https://github.com/apigee/apigee-go-gen' \
32-
org.opencontainers.image.vendor='Google LLC' \
33-
org.opencontainers.image.licenses='Apache-2.0' \
34-
org.opencontainers.image.description='This is a tool for generating Apigee bundles and shared flows'
35-
3630
COPY LICENSE /
37-
COPY LICENSE-3RD-PARTY /
3831

3932
COPY --from=builder /src/bin/* /usr/local/bin/
4033

34+
ARG GIT_REPO
35+
ARG GIT_TAG
36+
ARG GIT_COMMIT
37+
ARG BUILD_TIMESTAMP
38+
39+
LABEL org.opencontainers.image.url="https://github.com/${GIT_REPO}" \
40+
org.opencontainers.image.documentation="https://github.com/${GIT_REPO}" \
41+
org.opencontainers.image.source="https://github.com/${GIT_REPO}" \
42+
org.opencontainers.image.version="${GIT_TAG}" \
43+
org.opencontainers.image.revision="${GIT_COMMIT}" \
44+
org.opencontainers.image.vendor='Google LLC' \
45+
org.opencontainers.image.licenses='Apache-2.0' \
46+
org.opencontainers.image.description='This is a tool for generating Apigee bundles and shared flows'
47+
4148
ENTRYPOINT [ "apigee-go-gen" ]
4249

build-docker.sh

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,69 @@
1616
#
1717

1818

19-
docker build -t apigee/apigee-go-gen .
19+
sortSemver() {
20+
local lines=""
21+
while read version; do
22+
if [[ -z "${lines}" ]] ; then
23+
lines=$(printf '%s' "${version}")
24+
else
25+
lines=$(printf '%s\n%s' "${lines}" "${version}")
26+
fi
27+
done
28+
echo "$lines" | sed -r 's:^v::' | sed -r 's:-:~:' | sort -r -V | sed -r 's:^:v:' | sed -r 's:~:-:'
29+
}
30+
31+
pickLatestRelease() {
32+
local first=""
33+
while read version; do
34+
first="${version}"
35+
if [[ "${version}" != *"-"* ]] ; then
36+
echo "${version}"
37+
return
38+
fi
39+
done
40+
echo "${first}"
41+
}
42+
43+
getReleasedTags() {
44+
git tag --list | grep "^v"
45+
}
46+
47+
getLatestRelease() {
48+
echo "$(getReleasedTags | sortSemver | pickLatestRelease)"
49+
}
50+
51+
52+
REGISTRY="${REGISTRY:-ghcr.io}"
53+
GIT_REPO="${GIT_REPO:-apigee/apigee-go-gen}"
54+
BUILD_TIMESTAMP=$(date "+%s")
55+
GIT_COMMIT=$(git rev-parse --short HEAD)
56+
GIT_TAG=$(git describe --tags --abbrev=0)
57+
LATEST_TAG="$(getLatestRelease)"
58+
59+
if [[ "${LATEST_TAG}" == "${GIT_TAG}" ]] ; then
60+
BUILD_TAG="latest"
61+
else
62+
BUILD_TAG="${GIT_TAG}"
63+
fi
64+
65+
66+
echo "LATEST_TAG=${LATEST_TAG}"
67+
echo "BUILD_TAG=${BUILD_TAG}"
68+
echo "GIT_TAG=${GIT_TAG}"
69+
echo "GIT_COMMIT=${GIT_COMMIT}"
70+
71+
docker build -t "${REGISTRY}/${GIT_REPO}:${BUILD_TAG}" \
72+
-t "${REGISTRY}/${GIT_REPO}:${GIT_TAG}" \
73+
-t "${REGISTRY}/${GIT_REPO}:${GIT_COMMIT}" \
74+
--build-arg "GIT_REPO=${GIT_REPO}" \
75+
--build-arg="GIT_TAG=${GIT_TAG}" \
76+
--build-arg="GIT_COMMIT=${GIT_COMMIT}" \
77+
--build-arg="BUILD_TIMESTAMP=${BUILD_TIMESTAMP}" \
78+
.
79+
80+
if [ "${1}" == "push" ] ; then
81+
docker push "${REGISTRY}/${GIT_REPO}:${BUILD_TAG}"
82+
docker push "${REGISTRY}/${GIT_REPO}:${GIT_TAG}"
83+
docker push "${REGISTRY}/${GIT_REPO}:${GIT_COMMIT}"
84+
fi

0 commit comments

Comments
 (0)