Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #858 from DFE-Digital/2313-cache-docker-build-in-p…
Browse files Browse the repository at this point in the history
…ull-requests

[2313] Use PR branch sha as docker tag
  • Loading branch information
saliceti committed Jul 28, 2021
2 parents 638baeb + 35a5ba5 commit ea32e52
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
71 changes: 52 additions & 19 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
branches:
- master

env:
DOCKER_IMAGE: dfedigital/find-teacher-training

jobs:
build:
name: build
Expand All @@ -24,30 +27,60 @@ jobs:
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1.5.1
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2.1.6
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set Environment Variables (Push)
if: github.event_name == 'push'
run: |
GIT_REF=${{ github.ref }}
GIT_BRANCH=${GIT_REF##*/}
echo "BRANCH_TAG=$GIT_BRANCH" >> $GITHUB_ENV
echo "DOCKER_IMAGE_TAG=$GITHUB_SHA" >> $GITHUB_ENV
- name: Set Environment Variable
run: echo "DOCKER_IMAGE=dfedigital/find-teacher-training:${{ github.sha }}" >> $GITHUB_ENV
- name: Set Environment Variables (Pull request)
if: github.event_name == 'pull_request'
run: |
# This is the actual PR branch
GIT_REF=${{ github.head_ref }}
GIT_BRANCH=${GIT_REF##*/}
echo "BRANCH_TAG=$GIT_BRANCH" >> $GITHUB_ENV
# This is the latest commit on the actual PR branch
echo "DOCKER_IMAGE_TAG=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
- name: Build Docker Image - base-image target
uses: docker/build-push-action@v2
with:
tags: |
${{ env.DOCKER_IMAGE}}:base-image-${{ env.BRANCH_TAG }}
push: ${{ github.actor != 'dependabot[bot]' }}
load: ${{ github.actor == 'dependabot[bot]' }}
target: base-image
cache-from: |
${{ env.DOCKER_IMAGE}}:base-image-${{ env.BRANCH_TAG }}
${{ env.DOCKER_IMAGE}}:base-image-master
build-args: |
BUILDKIT_INLINE_CACHE=1
COMMIT_SHA=${{ env.DOCKER_IMAGE_TAG}}
- name: Build Docker Image
uses: docker/build-push-action@v2.6.1
uses: docker/build-push-action@v2
with:
tags: ${{ env.DOCKER_IMAGE}}
tags: |
${{ env.DOCKER_IMAGE}}:${{ env.DOCKER_IMAGE_TAG}}
${{ env.DOCKER_IMAGE}}:${{ env.BRANCH_TAG }}
push: ${{ github.actor != 'dependabot[bot]' }}
load: ${{ github.actor == 'dependabot[bot]' }}
builder: ${{ steps.buildx.outputs.name }}
cache-to: type=local,dest=/tmp/.buildx-cache
cache-from: type=local,src=/tmp/.buildx-cache
build-args: COMMIT_SHA=${{ github.sha }}
cache-from: |
${{ env.DOCKER_IMAGE}}:${{ env.BRANCH_TAG }}
${{ env.DOCKER_IMAGE}}:master
${{ env.DOCKER_IMAGE}}:base-image-${{ env.BRANCH_TAG }}
build-args: |
BUILDKIT_INLINE_CACHE=1
COMMIT_SHA=${{ env.DOCKER_IMAGE_TAG}}
- name: Pull image if not loaded
if: ${{ github.actor != 'dependabot[bot]' }}
run: docker pull ${DOCKER_IMAGE}:${{ env.DOCKER_IMAGE_TAG }}

- name: Run ruby linter
run: make rubocop
Expand All @@ -68,7 +101,7 @@ jobs:
if: github.actor != 'dependabot[bot]'
run: make publish.codeclimate
env:
GIT_BRANCH: ${{ github.ref }}
GIT_BRANCH: ${{ env.BRANCH_TAG }}
CC_TEST_REPORTER_ID: ${{ secrets.CODE_CLIMATE_REPORTER_ID }}

- name: Trigger QA Deployment
Expand All @@ -77,4 +110,4 @@ jobs:
with:
workflow: Deploy
token: ${{ secrets.ACTIONS_API_ACCESS_TOKEN }}
inputs: '{"qa": "true", "staging": "true", "production": "true", "sandbox": "true", "sha": "${{ github.sha }}"}'
inputs: '{"qa": "true", "staging": "true", "production": "true", "sandbox": "true", "sha": "${{ env.DOCKER_IMAGE_TAG}}"}'
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ RUN bundle exec rake assets:precompile && \


FROM ${BASE_RUBY_IMAGE}
ARG COMMIT_SHA
ENV FREEDESKTOP_MIME_TYPES_PATH=/usr/share/mime/packages/freedesktop.org.xml

WORKDIR /app

RUN apk add --update --no-cache tzdata && \
cp /usr/share/zoneinfo/Europe/London /etc/localtime && \
echo "Europe/London" > /etc/timezone

COPY --from=base-image ${FREEDESKTOP_MIME_TYPES_PATH} ${FREEDESKTOP_MIME_TYPES_PATH}
COPY --from=base-image /app /app
COPY --from=base-image /usr/local/bundle/ /usr/local/bundle/

ARG COMMIT_SHA
ENV SHA=${COMMIT_SHA}

RUN apk add --update --no-cache tzdata && \
cp /usr/share/zoneinfo/Europe/London /etc/localtime && \
echo "Europe/London" > /etc/timezone

CMD bundle exec rails server -b 0.0.0.0
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ifndef VERBOSE
.SILENT:
endif

IMAGE=${DOCKER_IMAGE}
IMAGE=dfedigital/find-teacher-training:${DOCKER_IMAGE_TAG}

.PHONY: help
help: ## Show this help
Expand Down Expand Up @@ -91,15 +91,13 @@ sandbox: ## Set DEPLOY_ENV to production

.PHONY: plan
plan: ## Run terraform for ${DEPLOY_ENV} eg: make qa plan, make staging plan, make production plan
$(eval export TF_VAR_paas_app_secrets_file=terraform/workspace_variables/app_secrets.yml)
$(eval export TF_VAR_paas_app_config_file=terraform/workspace_variables/app_config.yml)
az account set -s ${AZ_SUBSCRIPTION} && az account show
terraform init -backend-config terraform/workspace_variables/${DEPLOY_ENV}_backend.tfvars terraform
terraform plan -var-file terraform/workspace_variables/${DEPLOY_ENV}.tfvars terraform

.PHONY: deploy
deploy: ## Run terraform apply for ${DEPLOY_ENV} eg: make qa plan, make staging plan, make production plan
$(eval export TF_VAR_paas_app_secrets_file=terraform/workspace_variables/app_secrets.yml)
$(eval export TF_VAR_paas_app_config_file=terraform/workspace_variables/app_config.yml)
az account set -s ${AZ_SUBSCRIPTION} && az account show
terraform init -backend-config terraform/workspace_variables/${DEPLOY_ENV}_backend.tfvars terraform
Expand Down

0 comments on commit ea32e52

Please sign in to comment.