Skip to content

Latest commit

 

History

History
 
 

087

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Build a Docker Image and Publish It to GCP GCR & Artifact Registry using Github Actions

YouTube Tutorial

Content

  • Create GitHub Repository
  • Create Flask App
  • Create Docker Image for Flask App
  • Create Service Account in GCP
  • Create GitHub Actions Docker Build and Push Workflow
  • Use GitHub Actions to Push Docker to Artifact Registry
  • Increment Git Tag for Each Commit and Use It for Docker Image

Create GitHub Repository

  • Go to GitHub
  • Create empty repository lesson-087, keep it public
  • Clone repository
git clone [email protected]:antonputra/lesson-087.git

Create Flask App

  • Create app.py
  • Create requirements.txt
  • Optionally, create .gitignore

Create Docker Image for Flask App

  • Create Dockerfile
  • Build docker image
docker build -t lesson-087:latest .
  • Run docker locally
docker run -p 5000:5000 lesson-087:latest
  • Use curl to test
curl localhost:5000/hello

Create Service Account in GCP

  • Create github-actions service account
  • Create and download key for service account
  • Add Storage Admin role to the service account, project wide permissions (fix later)

Create GitHub Actions Docker Build and Push Workflow

  • Create .github/workflows/gcp.yml
  • Create GitHub secret SERVICE_ACCOUNT_KEY
  • Commit and push
git add .
git commit -m 'init commit'
git push origin main
  • Check docker image in GCR
  • Fix permissions, remove wide project permissions and add to GS bucket
  • Create empty commit to test CI
git commit --allow-empty -m "Trigger CI"
git push origin main

Use GitHub Actions to Push Docker to Artifact Registry

  • Create images Artifact Registry repository
  • Add github-actions service account to images repo with Artifact Registry Writer role
  • Add gcloud auth configure-docker us-west2-docker.pkg.dev --quiet to Configure Docker Client step
  • Add Push Docker Image to Artifact Registry step
  • Commit and push
git add .
git commit -m 'add step to push docker to artifact registry'
git push origin main

Increment Git Tag for Each Commit and Use It for Docker Image

  • Create scripts/git_update.sh
  • Add Automatic Tagging of Releases step
  • Update GIT_TAG variable to ${{ steps.increment-git-tag.outputs.git-tag }}
  • Commit and test
git add .
git commit -m 'add step to increment git tag'
git push origin main
  • Check GitHub Tag, GCR and Artifact registry

Links

Clean Up

  • Delete github-actions service account
  • Delete lesson-087 GCR repository
  • Delete artifacts.devopsbyexample-325402.appspot.com GS bucket
  • Delete images artifact repository