Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Go Module Cache For Tests in CI #1875

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ jobs:
uses: actions/checkout@v3

- name: Set up Go
id: setup-go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.goversion }}
cache-dependency-path: "**/go.sum"

- name: Ensure Module Path
run: mkdir -p /opt/go/pkg/mod

- name: Copy From Module Cache
if: steps.setup-go.outputs.cache-hit == 'true'
run: |
rsync -au "/home/runner/go/pkg/" "/opt/go/pkg"

- name: Install Docker Compose
uses: KengoTODA/actions-setup-docker-compose@92cbaf8ac8c113c35e1cedd1182f217043fbdd00
Expand All @@ -64,9 +74,18 @@ jobs:
- name: Start Vault
run: |
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d vault;
sleep 3;
sleep 3

- name: Run Tests
run: |
export VAULT_TOKEN=$(docker logs grant-vault 2>&1 | grep "Root Token" | tail -1 | cut -d ' ' -f 3 );
docker-compose -f docker-compose.yml -f docker-compose.dev.yml run --rm dev make;
docker-compose -f docker-compose.yml -f docker-compose.dev.yml run --rm -v /opt/go/pkg:/go/pkg dev make

- name: Ensure Module Directory
if: steps.setup-go.outputs.cache-hit != 'true'
run: mkdir -p /home/runner/go/pkg

- name: Copy To Module Cache
run: |
sudo rsync -au "/opt/go/pkg/" "/home/runner/go/pkg"
sudo chown -R runner:runner /home/runner/go/pkg
20 changes: 7 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
FROM golang:1.18-alpine as builder


# put certs in builder image
# Put certs in builder image.
RUN apk update
RUN apk add -U --no-cache ca-certificates && update-ca-certificates
RUN apk add make
RUN apk add build-base
RUN apk add git
RUN apk add bash
RUN apk add make build-base git bash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Dockerfile recommendations, the fewer the layers, the better.


ARG VERSION
ARG BUILD_TIME
ARG COMMIT

WORKDIR /src
COPY . ./
RUN chown -R nobody:nobody /src/
RUN mkdir /.cache
RUN chown -R nobody:nobody /.cache

RUN chown -R nobody:nobody /src/ && mkdir /.cache && chown -R nobody:nobody /.cache

USER nobody
RUN cd main && go mod download

RUN cd main && CGO_ENABLED=0 GOOS=linux go build \
RUN cd main && go mod download && CGO_ENABLED=0 GOOS=linux go build \
-ldflags "-w -s -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME} -X main.commit=${COMMIT}" \
-o bat-go main.go

FROM alpine:3.15 as base
# put certs in artifact from builder

# Put certs in artifact from builder.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /src/main/bat-go /bin/

Expand All @@ -38,4 +33,3 @@ FROM base as artifact
COPY --from=builder /src/migrations/ /migrations/
EXPOSE 3333
CMD ["bat-go", "serve", "grant", "--enable-job-workers", "true"]

6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ format:
format-lint:
make format && make lint

lint:
docker volume create batgo_lint_gomod
lint: ensure-gomod-volume
docker run --rm -v "$$(pwd):/app" -v batgo_lint_gomod:/go/pkg --workdir /app/main golangci/golangci-lint:v1.49.0 golangci-lint run -v ./...
docker run --rm -v "$$(pwd):/app" -v batgo_lint_gomod:/go/pkg --workdir /app/cmd golangci/golangci-lint:v1.49.0 golangci-lint run -v ./...
docker run --rm -v "$$(pwd):/app" -v batgo_lint_gomod:/go/pkg --workdir /app/libs golangci/golangci-lint:v1.49.0 golangci-lint run -v ./...
Expand All @@ -208,3 +207,6 @@ download-mod:
cd ./serverless/email/status && go mod download && cd ../../..
cd ./serverless/email/unsubscribe && go mod download && cd ../../..
cd ./serverless/email/webhook && go mod download && cd ../../..

ensure-gomod-volume:
docker volume create batgo_lint_gomod