From 239ad901dd41fde138578f02fc961df3be9c6f80 Mon Sep 17 00:00:00 2001 From: Lukas Schulte Pelkum Date: Sun, 18 Apr 2021 21:31:07 +0200 Subject: [PATCH] Implement GitHub actions CI --- .github/FUNDING.yml | 1 + .github/workflows/docker.yml | 43 +++++++++++++++++++++++++++++++++++ .github/workflows/general.yml | 21 +++++++++++++++++ Dockerfile | 13 +++++++---- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 .github/FUNDING.yml create mode 100644 .github/workflows/docker.yml create mode 100644 .github/workflows/general.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..8c75f40 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: 'lus' \ No newline at end of file diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..4b80809 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,43 @@ +name: docker + +on: + push: + branches: + - main + - develop + paths-ignore: + - '**.md' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v2 + - name: define branch name + run: echo "BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV + - name: define commit hash + run: echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + - name: define staging tag + if: env.BRANCH == 'develop' + run: echo "TAG=staging" >> $GITHUB_ENV + - name: define latest tag + if: env.BRANCH == 'main' + run: echo "TAG=latest" >> $GITHUB_ENV + - name: set up qemu + uses: docker/setup-qemu-action@v1 + - name: set up buildx + uses: docker/setup-buildx-action@v1 + - name: log in to ghcr + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: lus + password: ${{ secrets.CR_TOKEN }} + - name: build and push + uses: docker/build-push-action@v2 + with: + push: true + tags: ghcr.io/lus/pasty:${{ env.TAG }} + build-args: + PASTY_VERSION=${{ env.BRANCH }}-${{ env.COMMIT_HASH }} \ No newline at end of file diff --git a/.github/workflows/general.yml b/.github/workflows/general.yml new file mode 100644 index 0000000..82d69f5 --- /dev/null +++ b/.github/workflows/general.yml @@ -0,0 +1,21 @@ +name: general + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v2 + - name: setup go environment + uses: actions/setup-go@v2 + with: + go-version: ^1.16 + - name: download dependencies + run: | + go version + go mod download + - name: build + run: | + go build ./cmd/pasty/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index fa66086..96a8901 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,20 @@ -# Build the application +# Choose the golang image as the build base image FROM golang:1.16-alpine AS build -RUN apk update && apk upgrade && \ - apk add --no-cache bash git openssh build-base + +# Define the directory we should work in WORKDIR /app + +# Download the necessary go modules COPY go.mod go.sum ./ RUN go mod download + +# Build the application +ARG PASTY_VERSION=unset-debug COPY . . RUN go build \ -o pasty \ -ldflags "\ - -X github.com/lus/pasty/internal/static.Version=$(git rev-parse --abbrev-ref HEAD)-$(git describe --tags --abbrev=0)-$(git log --pretty=format:'%h' -n 1)" \ + -X github.com/lus/pasty/internal/static.Version=$PASTY_VERSION" \ ./cmd/pasty/main.go # Run the application in an empty alpine environment