Skip to content

Commit

Permalink
Merge pull request #124 from alphagov/add-action-to-build-image-and-c…
Browse files Browse the repository at this point in the history
…reate-pr

Add GitHub Action to build Docker images and create PR against govuk-dgu-charts repo
  • Loading branch information
nimalank7 authored Nov 23, 2023
2 parents 8cd6f1e + 5199605 commit 2dd9493
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Build and push images

on:
workflow_dispatch:
inputs:
buildType:
description: Decide on build type
required: true
type: choice
options:
- build_push
- build_only
push:
branches:
- main

jobs:
build_and_push:
name: datagovuk-tech-docs
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Login to GHCR
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a # v2.1.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
with:
ref: ${{ inputs.gitRef }}
- name: Build images (without pushing to registry)
if: ${{ inputs.buildType == 'build_only' }}
env:
DRY_RUN: "1"
APP: datagovuk-tech-docs
ARCH: amd64
run: ./docker/build-image.sh
- name: Build and push images
if: ${{ inputs.buildType == 'build_push' || github.ref == 'refs/heads/main' }}
env:
APP: datagovuk-tech-docs
ARCH: amd64
run: ./docker/build-image.sh
27 changes: 27 additions & 0 deletions .github/workflows/create-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Create charts PR

on:
workflow_dispatch:
workflow_run:
workflows: [ "Build and push images" ]
types:
- completed
branches:
- main

jobs:
create_pr:
name: Create charts PR
runs-on: ubuntu-latest
steps:
- name: Checkout datagovuk-tech-docs repository
uses: actions/checkout@v4
with:
repository: alphagov/datagovuk-tech-docs
- name: Create PR
run: bash ./docker/create-pr.sh
env:
GH_TOKEN: ${{ secrets.PR_GITHUB_TOKEN }}
GH_REF: ${{ github.ref_name }}
IS_TAG: "false"
ENVS: "integration"
25 changes: 25 additions & 0 deletions docker/build-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -eux

build () {
if [ "${ARCH}" = "amd64" ]; then
docker build . -t "ghcr.io/alphagov/${APP}:${1}" -f "docker/Dockerfile"
else
docker buildx build --platform "linux/${ARCH}" . -t "ghcr.io/alphagov/${APP}:${1}" -f "docker/Dockerfile"
fi
}

DOCKER_TAG="${GITHUB_SHA}"

if [[ -n ${GH_REF:-} ]]; then
DOCKER_TAG="${GH_REF}"
fi

build "${DOCKER_TAG}"

if [[ -n ${DRY_RUN:-} ]]; then
echo "Dry run; not pushing to registry"
else
docker push "ghcr.io/alphagov/${APP}:${DOCKER_TAG}"
fi
43 changes: 43 additions & 0 deletions docker/create-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

set -eux

if [[ ${IS_TAG:-} = "true" ]]; then
export IMAGE_TAG="${GH_REF}"
export SOURCE_BRANCH="main"
else
export IMAGE_TAG=$(gh api repos/alphagov/datagovuk-tech-docs/branches/${GH_REF} | jq .commit.sha -r)
export SOURCE_BRANCH=${GH_REF}
fi

git config --global user.email "govuk-ci@users.noreply.github.com"
git config --global user.name "govuk-ci"

git clone https://${GH_TOKEN}@github.com/alphagov/govuk-dgu-charts.git charts

cd charts/charts/datagovuk/images

for ENV in $(echo $ENVS | tr "," " "); do
(
BRANCH="ci/${IMAGE_TAG}-${ENV}"

if git show-ref --quiet refs/heads/${BRANCH}; then
echo "Branch ${BRANCH} already exists on govuk-dgu-charts"
else
git checkout -b ${BRANCH}

cd "${ENV}"
yq -i '.tag = env(IMAGE_TAG)' "techdocs.yaml"
yq -i '.branch = env(SOURCE_BRANCH)' "techdocs.yaml"
git add "techdocs.yaml"

if [[ $(git status | grep "nothing to commit") ]]; then
echo "Nothing to commit"
else
git commit -m "Update datagovuk-tech-docs image tags for ${ENV} to ${IMAGE_TAG}"
git push --set-upstream origin "${BRANCH}"
gh pr create --title "Update datagovuk-tech-docs image tags for ${ENV} (${IMAGE_TAG})" --base main --head "${BRANCH}" --fill
fi
fi
)
done

0 comments on commit 2dd9493

Please sign in to comment.