chore: Comment image promotion #4
This file contains hidden or 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
| on: | ||
|
Check failure on line 1 in .github/workflows/cd-base.yml
|
||
| workflow_call: | ||
| inputs: | ||
| deployment_env: | ||
| required: true | ||
| type: string | ||
| image_uri: # GHCR Docker image URI | ||
| required: true | ||
| type: string | ||
| jobs: | ||
| pull-tag-push: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| dockerhub_image: ${{ steps.push.outputs.image_uri }} | ||
| steps: | ||
| - name: Log in Github container registry | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Login to DockerHub | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
| - name: Pull -> re-tag -> push | ||
| id: push | ||
| run: | | ||
| DOCKERHUB_IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/mi-app:latest" | ||
| docker pull ${{ env.image_uri }} | ||
| docker tag ${{ env.image_uri }} $DOCKERHUB_IMAGE | ||
| docker push $DOCKERHUB_IMAGE | ||
| echo "image_uri=$DOCKERHUB_IMAGE" >> $GITHUB_OUTPUT | ||
| deploy: | ||
| needs: pull-tag-push | ||
| runs-on: ubuntu-latest | ||
| environment: ${{ inputs.deployment_env }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Setup Terraform | ||
| uses: hashicorp/setup-terraform@v3 | ||
| - name: Auth GCP | ||
| uses: google-github-actions/auth@v2 | ||
| with: | ||
| credentials_json: ${{ secrets.GCP_SA_KEY }} | ||
| - name: Init Terraform | ||
| working-directory: terraform | ||
| run: terraform init | ||
| - name: Use deployment env workspace | ||
| working-directory: terraform | ||
| run: | | ||
| terraform workspace select ${{ inputs.deployment_env }} || \ | ||
| terraform workspace new ${{ inputs.deployment_env }} | ||
| - name: Terraform plan | ||
| working-directory: terraform | ||
| run: | | ||
| terraform plan \ | ||
| -var-file=environments/${{ inputs.deployment_env }}.tfvars \ | ||
| -var="project_id=${{ secrets.GCP_PROJECT_ID }}" \ | ||
| -var="db_name=${{ secrets.DB_NAME }}" \ | ||
| -var="db_user=${{ secrets.DB_USER }}" \ | ||
| -var="db_password=${{ secrets.DB_PASSWORD }}" \ | ||
| -var="image_uri=${{ needs.pull-tag-push.outputs.dockerhub_image }}" \ | ||
| -out=tfplan | ||
| - name: Terraform Apply | ||
| working-directory: terraform | ||
| run: terraform apply tfplan | ||
| - name: Get db public IP | ||
| id: db | ||
| working-directory: terraform | ||
| run: echo "public_ip=$(terraform output -raw db_host)" >> $GITHUB_OUTPUT | ||
| - name: Check if tickets table exists | ||
| id: check_table | ||
| env: | ||
| PGPASSWORD: ${{ secrets.DB_PASSWORD }} | ||
| run: | | ||
| # install postgres client | ||
| sudo apt-get update && sudo apt-get install -y postgresql-client | ||
| TABLE_EXISTS=$(psql -h ${{ steps.db.outputs.public_ip }} \ | ||
| -U ${{ secrets.DB_USER }} \ | ||
| -d ${{ secrets.DB_NAME }} \ | ||
| -tAc "SELECT EXISTS (SELECT FROM pg_tables WHERE tablename = 'tickets');" \ | ||
| ) | ||
| echo "table_exist=$TABLE_EXISTS" >> $GITHUB_OUTPUT | ||
| - name: Create tables and populate (tickets table not existent) | ||
| if: steps.check_table.outputs.table_exist == 'f' | ||
| env: | ||
| PGPASSWORD: ${{ secrets.DB_PASSWORD }} | ||
| run: | | ||
| # if something fails stop | ||
| set -e | ||
| for file in $(ls src/db/scripts/*.sql | sort -V); do | ||
| echo "> Executing: $file" | ||
| psql -h ${{ steps.db.outputs.public_ip }} \ | ||
| -U ${{ secrets.DB_USER }} \ | ||
| -d ${{ secrets.DB_NAME }} \ | ||
| -f "$file" \ | ||
| -v DB_NAME=${{ secrets.DB_NAME }} \ | ||
| -v ON_ERROR_STOP=1 | ||
| done | ||
| - name: Skipped table creation (existent tickets table) | ||
| if: steps.check_table.outputs.table_exist == 't' | ||
| run: echo "" | ||