-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from alphagov/add-action-to-build-image-and-c…
…reate-pr Add GitHub Action to build Docker images and create PR against govuk-dgu-charts repo
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |