Last active
January 26, 2025 21:31
-
-
Save hassaku63/0c3e918a149f4362d8cedff748bee6c8 to your computer and use it in GitHub Desktop.
simple python string.Template example
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
aws ecr get-login-password --region ${aws_region} | docker login --username AWS --password-stdin ${aws_account_id}.dkr.ecr.$aws_region.amazonaws.com | |
docker build -t ${image_name} . | |
docker tag ${image_name}:$image_tag ${aws_account_id}.dkr.ecr.$aws_region.amazonaws.com/${image_name}:$image_tag | |
docker push ${aws_account_id}.dkr.ecr.${aws_region}.amazonaws.com/${image_name}:$image_tag |
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
import argparse | |
import pathlib | |
from string import Template | |
here = pathlib.Path(__file__).parent | |
def main(): | |
parser = argparse.ArgumentParser(description='generate docker build & push commands') | |
parser.add_argument('--account', type=str, required=True, dest='aws_account_id', help='aws account id') | |
parser.add_argument('--region', type=str, required=True, dest='aws_region', help='aws region') | |
parser.add_argument('-i', '--image-name', type=str, required=True, dest='image_name', help='image name') | |
parser.add_argument('-t', '--image-tag', type=str, required=True, dest='image_tag', help='image name') | |
args = parser.parse_args() | |
template_path = here / 'commands.template' | |
with template_path.open() as f: | |
t = Template(f.read()) | |
commands = t.substitute( | |
aws_account_id=args.aws_account_id, | |
aws_region=args.aws_region, | |
image_name=args.image_name, | |
image_tag=args.image_tag | |
) | |
print(commands) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage