Skip to content

Commit be7edd7

Browse files
authored
Use GHA directly to push container (#576)
* Use GHA directly to push container * Build docker image in CI for PRs as well
1 parent 4eddcf9 commit be7edd7

File tree

3 files changed

+68
-55
lines changed

3 files changed

+68
-55
lines changed

.github/workflows/pull_request.yaml

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: Pull Request
22
on: [pull_request]
33

4+
env:
5+
REGISTRY: ghcr.io
6+
IMAGE_NAME: ${{ github.repository }}
7+
48
jobs:
59
lint:
610
name: Lint
@@ -74,19 +78,44 @@ jobs:
7478
name: Docker Tests
7579
needs: [lint]
7680
runs-on: ubuntu-latest
77-
81+
7882
steps:
79-
- name: checkout
83+
- name: Checkout repository
8084
uses: actions/checkout@v4
8185

82-
- name: docker build
83-
run: make docker-build
86+
- name: Set up Docker Buildx
87+
uses: docker/setup-buildx-action@v3
88+
with:
89+
driver: docker
90+
91+
- name: Log in to the Container registry
92+
uses: docker/login-action@v3
93+
with:
94+
registry: ${{ env.REGISTRY }}
95+
username: ${{ github.actor }}
96+
password: ${{ secrets.GITHUB_TOKEN }}
97+
98+
- name: Extract metadata (tags, labels) for Docker
99+
id: meta
100+
uses: docker/metadata-action@v5
101+
with:
102+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
103+
104+
- name: Build and push Docker image
105+
uses: docker/build-push-action@v6
106+
with:
107+
context: .
108+
build-args: |
109+
KONSTRAINT_VER="${{ env.DOCKER_METADATA_OUTPUT_VERSION }}"
110+
push: false
111+
tags: ${{ steps.meta.outputs.tags }}
112+
labels: ${{ steps.meta.outputs.labels }}
84113

85114
- name: test create command
86-
run: docker run -v $PWD:/konstraint konstraint create /konstraint/examples
115+
run: docker run -v $PWD:/konstraint ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DOCKER_METADATA_OUTPUT_VERSION }} create /konstraint/examples
87116

88117
- name: test doc command
89-
run: docker run -v $PWD:/konstraint konstraint doc /konstraint/examples
118+
run: docker run -v $PWD:/konstraint ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DOCKER_METADATA_OUTPUT_VERSION }} doc /konstraint/examples
90119

91120
policy-checks:
92121
name: Policy Checks

.github/workflows/push_container.yaml

+33-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
tags:
55
- v*.*.*
66

7+
env:
8+
REGISTRY: ghcr.io
9+
IMAGE_NAME: ${{ github.repository }}
10+
711
jobs:
812
unit-test:
913
runs-on: ubuntu-latest
@@ -28,17 +32,35 @@ jobs:
2832
needs: [unit-test]
2933

3034
steps:
31-
- name: checkout
32-
uses: actions/checkout@v4
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
38+
- name: Set up QEMU
39+
uses: docker/setup-qemu-action@v3
40+
41+
- name: Set up Docker Buildx
42+
uses: docker/setup-buildx-action@v3
3343

34-
- name: get version tag
35-
run: echo TAG_NAME=$(echo ${GITHUB_REF} | rev | cut -d"/" -f1 | rev) >> $GITHUB_ENV
44+
- name: Log in to the Container registry
45+
uses: docker/login-action@v3
46+
with:
47+
registry: ${{ env.REGISTRY }}
48+
username: ${{ github.actor }}
49+
password: ${{ secrets.GITHUB_TOKEN }}
3650

37-
- name: docker login
38-
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
51+
- name: Extract metadata (tags, labels) for Docker
52+
id: meta
53+
uses: docker/metadata-action@v5
54+
with:
55+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
3956

40-
- name: docker build
41-
run: >-
42-
docker buildx create --name builder &&
43-
docker buildx use --builder builder &&
44-
make dockerx-build version=${TAG_NAME}
57+
- name: Build and push Docker image
58+
uses: docker/build-push-action@v6
59+
with:
60+
context: .
61+
build-args: |
62+
KONSTRAINT_VER="${{ github.ref_name }}"
63+
platforms: linux/arm/v7,linux/arm64,linux/amd64
64+
push: true
65+
tags: ${{ steps.meta.outputs.tags }}
66+
labels: ${{ steps.meta.outputs.labels }}

Makefile

-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
## The repository where the container image will be pushed to.
2-
IMAGE := ghcr.io/plexsystems/konstraint
3-
4-
PLATFORMS := linux/arm/v7,linux/arm64/v8,linux/amd64
5-
61
#
72
##@ Development
83
#
@@ -42,39 +37,6 @@ fmt: ## Ensures consistent formatting on policy tests.
4237
##@ Releases
4338
#
4439

45-
.PHONY: docker-build
46-
docker-build: ## Builds the docker image. Can optionally pass in a version.
47-
ifeq ($(version),)
48-
docker build -t konstraint:latest .
49-
else
50-
docker build -t konstraint:latest -t konstraint:$(version) --build-arg KONSTRAINT_VER=$(version) .
51-
endif
52-
53-
.PHONY: dockerx-build
54-
dockerx-build: ## Builds the docker image. Can optionally pass in a version.
55-
ifeq ($(version),)
56-
docker buildx build \
57-
--platform "$(PLATFORMS)" \
58-
-t konstraint:latest \
59-
.
60-
else
61-
docker buildx build \
62-
--push \
63-
--platform "$(PLATFORMS)" \
64-
-t konstraint:latest \
65-
-t "konstraint:$(version)" \
66-
--build-arg "KONSTRAINT_VER=$(version)" \
67-
.
68-
endif
69-
70-
.PHONY: docker-push
71-
docker-push: ## Pushes the docker image to the container registry.
72-
@test $(version)
73-
docker tag konstraint:latest $(IMAGE):$(version)
74-
docker tag konstraint:latest $(IMAGE):latest
75-
docker push $(IMAGE):$(version)
76-
docker push $(IMAGE):latest
77-
7840
.PHONY: release
7941
release: ## Builds the binaries for each OS and creates the checksums.
8042
@test $(version)

0 commit comments

Comments
 (0)