This repo contains code that integrates a github actions workflow to build and push docker images to Google Cloud.
All the steps that run in GitHub actions are in the Makefile
. For clarity it is described below too.
Note that in order to run this repo demo, it is necessary to create a IAM account in GCP with the correct permissions and with a auth key and load it as a secret actions in the repository you will be working on. More info on this on the official Google Actions page.
Run the following command to build an image:
docker build --tag=${TAG_NAME} .
Check it was created running:
docker image ls
To run the image, do the following:
docker run -it ${TAG_NAME} python app.py --name "foo"
And you should see the foo as output.
To test the app run:
pip install --upgrade pip && pip install -r requirements.txt
To test the application run:
python -m pytest -vv -cov=app test_app.py
To lint the Dockerfile
run:
docker run --rm -i hadolint/hadolint < Dockerfile
The following command adds the Docker credHelper
entry to Docker's configuration file, or creates the file if it doesn't exist. This will register gcloud as the credential helper for all Google-supported Docker registries. If the Docker configuration already contains a credHelper entry, it will be overwritten.
gcloud auth configure-docker --quiet
See the official documentation here
Pushing (uploading) images is one of the most common Container Registry tasks. To do this, it is necessary to tag an image to push it later:
docker tag ${IMAGE_NAME}:latest gcr.io/${PROJECT_ID}/${IMAGE_NAME}:latest &&\
docker tag ${IMAGE_NAME}:latest gcr.io/${PROJECT_ID}/${IMAGE_NAME}:${GIT_TAG} &&\
docker push gcr.io/${PROJECT_ID}/${IMAGE_NAME}:latest &&\
docker push gcr.io/${PROJECT_ID}/${IMAGE_NAME}:${GIT_TAG}
Artifact Registry is a single place for your organization to manage container images and language packages (such as Maven and npm).
docker tag ${IMAGE_NAME}:latest ${PROJECT_LOC}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:latest &&\
docker tag ${IMAGE_NAME}:latest ${PROJECT_LOC}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${GIT_TAG} &&\
docker push ${PROJECT_LOC}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:latest &&\
docker push ${PROJECT_LOC}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${GIT_TAG