Skip to content

Commit

Permalink
Image build (#2)
Browse files Browse the repository at this point in the history
* Remove obsolete Lambda packaging script and related configuration files

- Deleted `package_lambda.py` script responsible for packaging AWS Lambda functions and layers.
- Removed `pip.conf` configuration file for pip settings.
- Eliminated `requirements.txt` file that specified Python dependencies.
- Deleted `test_payload.json` used for testing Lambda functions.
- Removed empty JSON and TFVAR files in `varfiles` directory.
- Deleted `variables.tf` file containing Terraform variable definitions.
- Removed `versions.tf` file specifying Terraform version requirements.
- Added new GitHub Actions workflow for building and pushing Lambda container images using Packer.
- Introduced `packer.pkr.hcl` file for Packer configuration to build Docker images for Lambda.

* Refactor GitHub API integration to remove Census-specific references and improve configuration handling

* Update build.yml

---------

Co-authored-by: Dave Arnold <dave@roknsound.com>
  • Loading branch information
2 people authored and GitHub committed Apr 17, 2025
1 parent a35b7f9 commit 0ffd573
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]


permissions:
contents: write
id-token: write
Expand Down Expand Up @@ -96,4 +94,4 @@ jobs:
packer build \
-var "repository_uri=${{ env.repository_uri }}" \
-var "tag=${{ env.next_tag }}" \
packer.pkr.hcl
packer.pkr.hcl
45 changes: 31 additions & 14 deletions eks_automation/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
####################################################################################
# This Lambda function takes JSON input, processes it using a Jinja2 template,
# and writes the output to a file in a cloned GitHub repository.
# The changes are then committed and pushed to the Census GitHub Enterprise Server,
# creating a new repository for the Census EKS CI/CD pipeline.
# The changes are then committed and pushed to the GitHub API,
# creating a new repository for the EKS CI/CD pipeline.
# This implementation uses only pure Python with requests library (no Git CLI dependency).
####################################################################################

Expand All @@ -24,9 +24,12 @@


# Get configuration from environment variables with defaults
CENSUS_GITHUB_API = os.environ.get("CENSUS_GITHUB_API", "https://github.e.it.census.gov/api/v3")
ORG_NAME = os.environ.get("GITHUB_ORG_NAME", "SCT-Engineering")
GITHUB_API = os.environ.get("GITHUB_API") # No default - must be configured
ORG_NAME = os.environ.get("GITHUB_ORG_NAME") # No default - must be configured
SECRET_NAME = os.environ.get("GITHUB_TOKEN_SECRET_NAME", "/eks-cluster-deployment/github_token")
COMMIT_AUTHOR_EMAIL = os.environ.get("GITHUB_COMMIT_AUTHOR_EMAIL", "eks-automation@noreply.github.com")
COMMIT_AUTHOR_NAME = os.environ.get("GITHUB_COMMIT_AUTHOR_NAME", "EKS Automation Lambda")
SOURCE_VERSION = os.environ.get("TEMPLATE_SOURCE_VERSION") # Optional - if not set, uses default branch

ORIG_REPO_NAME = os.environ.get("TEMPLATE_REPO_NAME", "template-eks-cluster")

Expand Down Expand Up @@ -96,8 +99,7 @@ def get_repository(self, repo_name, create=False):
repo_data = {
"name": repo_name,
"description": "EKS Automation CI/CD Pipeline Repo",
"private": True,
"visibility": "internal"
"private": True
}
create_response = requests.post(
create_url,
Expand Down Expand Up @@ -313,8 +315,8 @@ def create_commit(self, repo_name, message, tree_sha, parent_shas):
# Add committer/author information
current_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
data["author"] = {
"name": "EKS Automation Lambda",
"email": "eks-automation@census.gov",
"name": COMMIT_AUTHOR_NAME,
"email": COMMIT_AUTHOR_EMAIL,
"date": current_time
}
data["committer"] = data["author"]
Expand Down Expand Up @@ -354,8 +356,7 @@ def create_reference(self, repo_name, ref, sha):
"""Create a reference in the repository
Args:
repo_name (str): Name of the repository
ref (str): Full reference name (e.g., 'refs/heads/main')
repo_name (str): Full reference name (e.g., 'refs/heads/main')
sha (str): SHA to create the reference at
"""
api_url = f"{self.api_base_url}/repos/{self.org_name}/{repo_name}/git/refs"
Expand All @@ -382,17 +383,33 @@ def clone_repository_contents(self, source_repo, target_dir):
Returns:
str: The default branch name of the repository
"""
# Get default branch of original repo
# Get default branch of original repo for fallback
default_branch = self.get_default_branch(source_repo)
logger.info(f"Default branch for {source_repo}: {default_branch}")

# Get tree from original repository
logger.info(f"Getting file tree from {source_repo}")
tree_sha = self.get_reference_sha(source_repo, f"heads/{default_branch}")
ref = None

if SOURCE_VERSION:
try:
# Try to get the tag/release reference first
ref = f"tags/{SOURCE_VERSION}"
tree_sha = self.get_reference_sha(source_repo, ref)
logger.info(f"Using source version: {SOURCE_VERSION}")
except Exception as e:
logger.warning(f"Failed to get version {SOURCE_VERSION}, falling back to default branch: {str(e)}")
ref = f"heads/{default_branch}"
tree_sha = self.get_reference_sha(source_repo, ref)
else:
# Use default branch
ref = f"heads/{default_branch}"
tree_sha = self.get_reference_sha(source_repo, ref)

tree = self.get_tree(source_repo, tree_sha, recursive=True)

# Download all files from original repo to work directory
logger.info(f"Downloading all files from {source_repo}")
logger.info(f"Downloading all files from {source_repo} using ref: {ref}")
self.download_repository_files(source_repo, tree, target_dir)

return default_branch
Expand Down Expand Up @@ -558,7 +575,7 @@ def operate_github(new_repo_name, eks_settings, output_hcl):
os.makedirs(work_dir, exist_ok=True)

# Initialize GitHub client
github = GitHubClient(CENSUS_GITHUB_API, token, ORG_NAME)
github = GitHubClient(GITHUB_API, token, ORG_NAME)

# Get info about original repo
logger.info(f"Fetching original repository information: {ORIG_REPO_NAME}")
Expand Down

0 comments on commit 0ffd573

Please sign in to comment.