Skip to content

Commit 6fd8ace

Browse files
committed
feat: update template to support ghcr, tag builds, updated Dockerfile
1 parent ec611d4 commit 6fd8ace

File tree

6 files changed

+59
-64
lines changed

6 files changed

+59
-64
lines changed

.github/workflows/dockerpublish.yml

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,40 @@ on:
1313
# Run tests for any PRs.
1414
pull_request:
1515

16+
env:
17+
REGISTRY: ghcr.io
18+
IMAGE_NAME: ${{ github.repository }}
19+
1620
jobs:
17-
# Push image to GitHub Packages.
18-
# See also https://docs.docker.com/docker-hub/builds/
19-
build:
20-
runs-on: ubuntu-latest
21-
if: github.event_name == 'push'
2221

23-
steps:
24-
#needed to access private repos with ssh
25-
# - uses: shimataro/ssh-key-action@v2.0.1
26-
# with:
27-
# key: ${{ secrets.SSH_PRIV_KEY }}
28-
# known_hosts: ${{ secrets.KNOWN_HOSTS }}
29-
# name: id_rsa # optional
30-
31-
- uses: actions/checkout@v2
32-
33-
- name: Build Container
34-
run: make docker-build
35-
36-
publish:
22+
build_and_publish:
3723
runs-on: ubuntu-latest
38-
if: startsWith(github.ref, 'refs/tags/v')
3924

4025
steps:
41-
#needed to access private repos with ssh
42-
# - uses: shimataro/ssh-key-action@v2.0.1
43-
# with:
44-
# key: ${{ secrets.SSH_PRIV_KEY }}
45-
# known_hosts: ${{ secrets.KNOWN_HOSTS }}
46-
# name: id_rsa # optional
47-
48-
- uses: actions/checkout@v2
49-
50-
- name: Log into registry
51-
#note the registry doesn't allow github_token a PAT is needed. So, you will need to create that post install
52-
run: echo "${{ secrets.GITHUB_TOKEN }}" |docker login docker.pkg.github.com --username publisher --password-stdin
53-
54-
- name: Publish Build
55-
run: make docker-push
56-
26+
- name: Checkout repository
27+
uses: actions/checkout@v3
28+
29+
- name: Log in to the Container registry
30+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
31+
with:
32+
registry: ${{ env.REGISTRY }}
33+
username: ${{ github.actor }}
34+
password: ${{ secrets.CR_PAT }}
35+
36+
- name: Extract metadata (tags, labels) for Docker
37+
id: meta
38+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
39+
with:
40+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
41+
tags: |
42+
type=ref,event=tag
43+
type=sha,prefix=commit-
44+
45+
46+
- name: Build and push Docker image
47+
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
48+
with:
49+
context: .
50+
push: true
51+
tags: ${{ steps.meta.outputs.tags }}
52+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ LABEL stage=release
4242
COPY --from=alpineCerts /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
4343
COPY --from=baseGo /root/gocode/bin /app
4444
COPY --from=baseGo /root/gocode/scripts /app
45-
ENTRYPOINT ["/app/entrypoint.sh"]
45+
ENTRYPOINT ["/app/go-project-template"]
4646

4747

4848
#

Makefile

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,67 @@
11
# Go parameters
22
BINARY_NAME=go-project-template
33
BINARY_UNIX=$(BINARY_NAME)_unix
4-
REPO=docker.pkg.github.com/dathan/go-project-template/go-project-template
4+
REPO=ghcr.io/dathan/go-project-template/go-project-template
55

66
.PHONY: all
77
all: lint test build
88

99
.PHONY: lint
1010
lint:
11-
golangci-lint run ./...
11+
golangci-lint run ./...
12+
13+
.PHONY: buildruntest
14+
buildruntest: build build-linux customrun
15+
16+
.PHONY: customrun
17+
customrun:
18+
./scripts/customrun.sh
1219

1320
.PHONY: build
1421
build:
15-
go build -o ./bin ./cmd/...
22+
go build -o ./bin ./cmd/...
1623

1724
.PHONY: test
1825
test:
19-
go test -p 6 -covermode=count -coverprofile=test/coverage.out test/*.go
26+
go test -p 6 -covermode=count -coverprofile=test/coverage.out test/*.go
2027

2128
.PHONY: clean
2229
clean:
23-
go clean
24-
find . -type d -name '.tmp_*' -prune -exec rm -rvf {} \;
30+
go clean
31+
find . -type d -name '.tmp_*' -prune -exec rm -rvf {} \;
2532

2633
.PHONY: run
2734
run:
28-
go run ./cmd/$(BINARY_NAME)/*.go
35+
go run ./cmd/$(BINARY_NAME)/*.go
2936

3037
.PHONY: vendor
3138
vendor:
32-
go mod vendor
39+
go mod vendor
3340

3441
# Cross compilation
3542
.PHONY: build-linux
3643
build-linux:
37-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(BINARY_UNIX) -v cmd/$(BINARY_NAME)/
44+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(BINARY_UNIX) -v cmd/$(BINARY_NAME)/*.go
3845

3946
# Build docker containers
4047
.PHONY: docker-build
4148
docker-build:
42-
# docker build \
43-
# --build-arg GITHUB_SSH_PRIV_KEY="`cat ~/.ssh/id_rsa`" \
44-
# -t $(or ${dockerImage},$(BINARY_NAME)-release) .
45-
docker build \
46-
-t $(or ${dockerImage},$(BINARY_NAME)-release) .
49+
# docker build \
50+
# --build-arg GITHUB_SSH_PRIV_KEY="`cat ~/.ssh/id_rsa`" \
51+
# -t $(or ${dockerImage},$(BINARY_NAME)-release) .
52+
docker build \
53+
-t $(or ${dockerImage},$(BINARY_NAME)-release) .
4754

4855
.PHONY: docker-tag
4956
docker-tag:
50-
docker tag `docker image ls --filter 'reference=$(BINARY_NAME)-release' -q` $(REPO):`git rev-parse HEAD`
57+
docker tag `docker image ls --filter 'reference=$(BINARY_NAME)-release' -q` $(REPO):`git rev-parse HEAD`
5158

5259
# Push the container
5360
.PHONY: docker-push
5461
docker-push: docker-build docker-tag
55-
docker push $(REPO):`git rev-parse HEAD`
62+
docker push $(REPO):`git rev-parse HEAD`
5663

5764

5865
.PHONY: docker-clean
5966
docker-clean:
60-
docker rmi `docker image ls --filter 'reference=$(BINARY_NAME)-*' -q`
67+
docker rmi `docker image ls --filter 'reference=$(BINARY_NAME)-*' -q`

cmd/example2/main.go

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

scripts/entrypoint.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
echo "Executing a script designed to run from within Docker"
4-
/app/example1
5-
/app/example2
4+
/app/go-project-template

0 commit comments

Comments
 (0)