Skip to content

Instantly share code, notes, and snippets.

@hassaku63
Last active January 26, 2025 21:31
Show Gist options
  • Save hassaku63/0c3e918a149f4362d8cedff748bee6c8 to your computer and use it in GitHub Desktop.
Save hassaku63/0c3e918a149f4362d8cedff748bee6c8 to your computer and use it in GitHub Desktop.
simple python string.Template example
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
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()
@hassaku63
Copy link
Author

Example usage

$ python -m main --account 1234567879012 --region ap-northeast-1 --image-name foo-image --image-tag v0.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment