Publish and deploy to Google Cloud Services #3
Workflow file for this run
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
name: Publish and deploy to Google Cloud Services | |
on: | |
release: | |
types: [published] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
env: | |
TAG: ${{ github.ref_name }} | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
environment: | |
name: Google Cloud Services | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- id: 'auth' | |
uses: 'google-github-actions/auth@v2' | |
with: | |
project_id: ${{ secrets.PROJECT_ID }} | |
credentials_json: ${{ secrets.ARTIFACT_REGISTRY_PRIVATE_KEY }} | |
- name: Setup Google Cloud Services | |
uses: google-github-actions/setup-gcloud@v2 | |
with: | |
project_id: ${{ secrets.PROJECT_ID }} | |
- name: Setup Docker with the Artifact Registry | |
run: gcloud auth configure-docker ${{ vars.REGION }}-docker.pkg.dev | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ vars.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ vars.REPOSITORY }}/${{ vars.IMAGE }}:${{ env.TAG }} --no-cache . | |
- name: Push Docker image to the Artifact Registry | |
run: | | |
docker push ${{ vars.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ vars.REPOSITORY }}/${{ vars.IMAGE }}:${{ env.TAG }} | |
deploy: | |
runs-on: ubuntu-latest | |
environment: | |
name: Google Cloud Services | |
steps: | |
- id: 'auth' | |
uses: 'google-github-actions/auth@v2' | |
with: | |
project_id: ${{ secrets.PROJECT_ID }} | |
credentials_json: ${{ secrets.CLOUD_RUN_PRIVATE_KEY }} | |
- name: Setup Google Cloud Services | |
uses: google-github-actions/setup-gcloud@v2 | |
with: | |
project_id: ${{ secrets.PROJECT_ID }} | |
- name: Wait for the Docker image to be available in the Artifact Registry | |
run: | | |
AVAILABLE=false | |
for i in {1..36}; do | |
if gcloud artifacts docker images describe ${{ vars.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ vars.REPOSITORY }}/${{ vars.IMAGE }}:${{ env.TAG }} > /dev/null 2>&1; then | |
echo "The Docker image is available in the Artifact Registry." | |
AVAILABLE=true | |
break | |
else | |
echo "Waiting for the Docker image to be available..." | |
sleep 10 | |
fi | |
done | |
if [ "$AVAILABLE" = false ]; then | |
echo "ERROR: Unable to deploy to Cloud Run. The Docker image is not available in the Artifact Registry. Login to the Google Cloud console for more information." | |
exit 1 | |
fi | |
- name: Deploy to Cloud Run | |
run: | | |
gcloud run deploy ${{ vars.SERVICE }} \ | |
--image=${{ vars.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ vars.REPOSITORY }}/${{ vars.IMAGE }}:${{ env.TAG }} \ | |
--region=${{ vars.REGION }} \ | |
--platform=managed \ | |
--allow-unauthenticated |