Skip to content

Commit

Permalink
add docker image build testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Mar 13, 2024
1 parent c3eb61c commit a4cc02c
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 167 deletions.
35 changes: 27 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ concurrency:
cancel-in-progress: true

jobs:
pack-pages:
pack-test-pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,19 +21,19 @@ jobs:
python-version: "3.12"
cache: "pip"
- run: pip install -r requirements.txt
- id: pack-pages
run: python pack_pages.py
- id: pack-test-pages
run: python scripts/pack_test_pages.py
- name: Upload artifact
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: github-pages
path: ${{ steps.pack-pages.outputs.artifact }}
path: ${{ steps.pack-test-pages.outputs.artifact }}
retention-days: 30

deploy-pages:
deploy-test-pages:
runs-on: ubuntu-latest
needs: pack-pages
needs: pack-test-pages
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
Expand All @@ -45,9 +45,28 @@ jobs:
id: deployment
uses: actions/deploy-pages@v4

build-test-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get Ansible Vault key
run: |
mkdir .ansible
echo "${{ secrets.ANSIBLE_VAULT_KEY }}" > .ansible/vault_key
- uses: actions/setup-python@v4
with:
python-version: "3.12"
cache: "pip"
- run: pip install -r requirements.txt
- run: python scripts/build_test_image.py ${{ secrets.GITHUB_TOKEN }}

build-python-lib:
runs-on: ubuntu-latest
needs: deploy-pages
needs:
- deploy-test-pages
- build-test-image
permissions:
contents: write
steps:
Expand All @@ -59,4 +78,4 @@ jobs:
python-version: "3.12"
cache: "pip"
- run: pip install -r requirements.txt
- run: python publish_lib.py ${{ secrets.GITHUB_TOKEN }}
- run: python scripts/publish_lib.py ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.ansible

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
24 changes: 24 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "ansible-vault encrypt vars/vault.yaml --vault-password-file .ansible/vault_key",
"label": "Encrypt vault",
"problemMatcher": []
},
{
"type": "shell",
"command": "ansible-vault decrypt vars/vault.yaml --vault-password-file .ansible/vault_key",
"label": "Decrypt vault",
"problemMatcher": []
},
{
"type": "shell",
"command": "ansible-vault view vars/vault.yaml --vault-password-file .ansible/vault_key",
"label": "view vault",
"problemMatcher": []
}
]
}
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- add NPN publishibg support
- add GitHub pages support
- add NPN publishibg support https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages#publishing-packages-to-the-npm-registry
6 changes: 0 additions & 6 deletions pack_pages.py

This file was deleted.

25 changes: 25 additions & 0 deletions scripts/build_test_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import sys
from pathlib import Path

root_directory = Path(__file__).parent
sys.path.append(str(root_directory))

from src.ansible_utils import load_vars
from src.docker_utils import build_and_push_docker_img

root_directory = Path(__file__).parent.parent
data = load_vars(root_directory / '.ansible/vault_key',
root_directory / 'vars/vault.yaml')
docker_username = data['docker_username']
docker_password = data['docker_password']

access_token = sys.argv[1]

if not access_token:
print("GitHub access token is missing", flush=True, file=sys.stderr)
exit(1)

build_and_push_docker_img(src="src", tag_prefix="docker-image",
image_name="publish-tools-test", docker_username=docker_username, docker_password=docker_password, github_access_token=access_token)
13 changes: 13 additions & 0 deletions scripts/create_vault_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

from pathlib import Path
import sys

root_directory = Path(__file__).parent.parent
sys.path.append(str(root_directory))

from src.ansible_utils import create_vault_key

root_directory = Path(__file__).parent.parent

create_vault_key(root_directory / '.ansible/vault_key')
12 changes: 12 additions & 0 deletions scripts/pack_test_pages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3

from pathlib import Path
import sys

root_directory = Path(__file__).parent
sys.path.append(str(root_directory))

from src.github_utils import create_pages_artifact


create_pages_artifact(directory="test")
2 changes: 1 addition & 1 deletion publish_lib.py → scripts/publish_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from setuptools import sandbox

root_directory = Path(__file__).parent
root_directory = Path(__file__).parent.parent

sys.path.append(str(root_directory))

Expand Down
1 change: 1 addition & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine
150 changes: 0 additions & 150 deletions src/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,156 +6,6 @@
from typing import List
from .github_utils import create_release
from .version_utils import get_version


def build_and_push_client_img(
*,
src: Path,
tag_prefix: str,
image_name: str,
docker_username: str,
docker_password: str,
github_access_token: str,
pack_args: List[str] = [],
ignore: List[str] = [],
):
build_and_push_img(
src=src,
tag_prefix=tag_prefix,
image_name=image_name,
pack_args=[
'--builder', 'paketobuildpacks/builder:base',
'--buildpack', 'paketo-buildpacks/web-servers',
'--buildpack', 'paketo-buildpacks/source-removal',
'--buildpack', 'gcr.io/paketo-buildpacks/health-checker',
'--env', 'BP_NODE_RUN_SCRIPTS=build',
'--env', 'BP_WEB_SERVER=nginx',
'--env', 'BP_WEB_SERVER_ROOT=dist',
'--env', 'BP_WEB_SERVER_ENABLE_PUSH_STATE=true',
'--env', 'BP_NGINX_STUB_STATUS_PORT=8033',
'--env', 'BP_INCLUDE_FILES=nginx.conf:dist/**',
'--env', 'BP_HEALTH_CHECKER_ENABLED=true',
'--env', 'BPE_PORT=80',
'--env', 'THC_PATH=/stub_status',
'--env', 'THC_PORT=8033',
*pack_args,
],
docker_username=docker_username,
docker_password=docker_password,
github_access_token=github_access_token,
ignore=[
'node_modules',
'dist',
*ignore
]
)


def build_and_push_server_img(
*,
src: Path,
tag_prefix: str,
image_name: str,
docker_username: str,
docker_password: str,
github_access_token: str,
pack_args: List[str] = [],
ignore: List[str] = [],
):
build_and_push_img(
src=src,
tag_prefix=tag_prefix,
image_name=image_name,
pack_args=[
'--builder', 'paketobuildpacks/builder:base',
'--buildpack', 'paketo-buildpacks/java',
'--buildpack', 'gcr.io/paketo-buildpacks/health-checker',
'--env', 'BP_JVM_VERSION=17',
'--env', 'BP_HEALTH_CHECKER_ENABLED=true',
'--env', 'BPE_SPRING_PROFILES_ACTIVE=prod',
'--env', 'THC_PATH=/actuator/health/liveness',
'--env', 'THC_PORT=8082',
*pack_args,
],
docker_username=docker_username,
docker_password=docker_password,
github_access_token=github_access_token,
ignore=[
'target',
*ignore
]
)

def build_and_push_python_img(
*,
src: Path,
tag_prefix: str,
image_name: str,
docker_username: str,
docker_password: str,
github_access_token: str,
pack_args: List[str] = [],
ignore: List[str] = [],
):
build_and_push_img(
src=src,
tag_prefix=tag_prefix,
image_name=image_name,
pack_args=[
'--builder', 'paketobuildpacks/builder:base',
'--buildpack', 'paketo-buildpacks/python',
*pack_args,
],
docker_username=docker_username,
docker_password=docker_password,
github_access_token=github_access_token,
ignore=[
'target',
*ignore
]
)


def build_and_push_img(
*,
src: Path,
tag_prefix: str,
image_name: str,
docker_username: str,
docker_password: str,
github_access_token: str,
pack_args: List[str] = [],
ignore: List[str] = [],
):
if not github_access_token:
print('GitHub access token is missing', flush=True, file=sys.stderr)
exit(1)

changed, version = get_version(
src=src, tag_prefix=tag_prefix, ignore=ignore)

if not changed:
return

run(['docker', 'login', '--username', docker_username,
'--password-stdin'], input=docker_password.encode(), check=True)
run(['pack', 'build', f'{image_name}:latest', '--path', str(src), '--tag',
f'{image_name}:{version}', '--publish', *pack_args], check=True)

create_release(
tag_prefix=tag_prefix,
version=version,
access_token=github_access_token,
body=dedent(f'''
[Docker image on DockerHub](https://hub.docker.com/repository/docker/{image_name})
```yaml
image: {image_name}:{version}
```
''')
)
print(
f'Docker image pushed successfully for {tag_prefix}:{version}', flush=True)

def build_and_push_docker_img(
*,
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions vars/vault.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$ANSIBLE_VAULT;1.1;AES256
38393661643437653332646263313737663933656331666139626633616663623139363733636633
6638653635653535633033666265633634306139353939650a383435386361663839326466613061
63396165643062363837383465653235366232386263663263386465353361303430333634313234
3562373433323266640a396365666666323839623430396130363565323033393537316536333736
33623861373038313430303534306639373735306131383164366665353739623532333832656434
64383836316664633335323132366366663936336564313335663439343263633262393133336533
30323736343335366637396138386264653063363436633566653630313161633231306333316237
64613131366666343333

0 comments on commit a4cc02c

Please sign in to comment.