Skip to content

Commit

Permalink
Revert in-docker version generation and use the CI env instead
Browse files Browse the repository at this point in the history
  • Loading branch information
oskirby committed Sep 11, 2024
1 parent aab5392 commit 5c55fdb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
9 changes: 5 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ jobs:
- checkout
- setup_remote_docker

- run:
name: Create a version.json
command: ./version.sh | tee version.json

- run:
name: Build Docker image
command: |
docker build -t app:build . \
--build-arg VERSION_BUILD_URL="$CIRCLE_BUILD_URL" \
--build-arg VERSION_TAG_NAME="$CIRCLE_TAG"
command: docker build -t app:build .

# save the built docker container into CircleCI's cache. This is
# required since Workflows do not have the same remote docker instance.
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
integration_test/
.git/
coverage.out
docker-compose.yml
Dockerfile
9 changes: 5 additions & 4 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ jobs:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Generate version.json
shell: bash
run: version.sh | tee version.json

- name: Build and push
uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
sbom: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=true
VERSION_BUILD_URL=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
VERSION_TAG_NAME=${{ startsWith(github.ref, 'refs/tags/') && github.ref_name || '' }}
context: .
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ RUN apt-get update && \
# Build Stage
#------------------------------------------------------------------------------
FROM base AS builder
ARG VERSION_COMMIT_HASH
ARG VERSION_SOURCE_URL
ARG VERSION_BUILD_URL
ARG VERSION_TAG_NAME
ENV GO111MODULE=on
ENV CGO_ENABLED=1

ADD . /app/src

RUN cd /app/src && go generate
RUN cd /app/src && go install .

#------------------------------------------------------------------------------
Expand All @@ -54,7 +49,7 @@ EXPOSE 8080
# Copy compiled appliation from the builder.
RUN mkdir /app
ADD autograph-edge.yaml /app
COPY --from=builder /app/src/version.json /app
ADD version.json /app
COPY --from=builder /go/bin/autograph-edge /usr/local/bin/autograph-edge

# Setup the worker and entrypoint.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type authorization struct {
AddonCOSEAlgorithms []string
}

//go:generate ./version.sh
//go:generate ./version.sh version.json
//go:embed "version.json"
var jsonVersion []byte

Expand Down
32 changes: 25 additions & 7 deletions version.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
#!/bin/sh
#!/bin/bash
SRCDIR=$(dirname $0)

if [ -d ${SRCDIR}/.git ]; then
VERSION_COMMIT_HASH=${VERSION_COMMIT_HASH:=$(git -C ${SRCDIR} rev-parse HEAD)}
VERSION_SOURCE_URL=${VERSION_SOURCE_URL:=$(git -C ${SRCDIR} remote get-url origin)}
if [ -n "$GITHUB_SHA" ]; then
# We are probably running in a Github workflow.
VERSION_SOURCE_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
VERSION_COMMIT_HASH="$GITHUB_SHA"
VERSION_BUILD_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
if [[ "$GITHUB_REF" =~ ^refs/tags/ ]]; then
VERSION_TAG_NAME="$GITHUB_REF_NAME"
fi
elif [ -n "$CIRCLE_SHA1" ]; then
# We are running in a CircleCI job.
VERSION_SOURCE_URL="https://github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
VERSION_COMMIT_HASH="$CIRCLE_SHA1"
VERSION_BUILD_URL="$CIRCLE_BUILD_URL"
VERSION_TAG_NAME="$CIRCLE_TAG"
elif [ -d ${SRCDIR}/.git ]; then
# Otherwise, try to grab version information from the git repository.
VERSION_COMMIT_HASH=$(git -C ${SRCDIR} rev-parse HEAD)
VERSION_SOURCE_URL=$(git -C ${SRCDIR} remote get-url origin)
VERSION_TAG_NAME=$(git -C ${SRCDIR} describe --tags --always)
fi
if [ -z "${VERSION_SOURCE_URL}" ]; then
VERSION_SOURCE_URL="https://github.com/mozilla-services/autograph-edge.git"

# Redirect to a file if provided as an argument.
if [ $# -ge 1 ]; then
exec > $1
fi

cat << EOF > ${SRCDIR}/version.json
cat << EOF
{
"source": "${VERSION_SOURCE_URL}",
"commit": "${VERSION_COMMIT_HASH}",
Expand Down

0 comments on commit 5c55fdb

Please sign in to comment.