I needed to add:

jobs:
   my_job:
   # Need to add these 3 lines to add "id-token" with the intended permissions.
   permissions:
     contents: 'read'
     id-token: 'write'

This is documented here: https://github.com/google-github-actions/auth#usage

Answer from Brian C. on Stack Overflow
🌐
GitHub
github.com › google-github-actions › auth
GitHub - google-github-actions/auth: A GitHub Action for authenticating to Google Cloud. · GitHub
It supports authentication via a Google Cloud Service Account Key JSON and authentication via Workload Identity Federation. Workload Identity Federation is recommended over Service Account Keys as it obviates the need to export a long-lived credential and establishes a trust delegation relationship between a particular GitHub Actions workflow invocation and permissions on Google Cloud.
Starred by 1.3K users
Forked by 295 users
Languages   TypeScript 98.8% | JavaScript 1.2%
Discussions

Authenticate to multiple gcloud accounts with GitHub Actions - Stack Overflow
Using google-github-actions/auth@v0' wipes out the previous service account. More on stackoverflow.com
🌐 stackoverflow.com
Authenticating to docker using gcloud isn't working after this action
TL;DR Authenticating to docker using gcloud isn't working, despite what looks like a successful login. Expected behavior Docker to be successfully authenticated. Observed behavior Docker authen... More on github.com
🌐 github.com
2
January 21, 2022
Github Actions | google cloud authentication - Stack Overflow
I am building a github actions workflow and I am failing to authenticate into google cloud. I get the following error : google-github-actions/auth failed with: failed to parse service account key J... More on stackoverflow.com
🌐 stackoverflow.com
Google artifact regitsry NPM + github action - Stack Overflow
I'm trying to publish a npm package on GAR (Google Artifact Registry) through github using google-github-actions/auth@v0 and google-artifactregistry-auth For the authentication to google from githu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › google-github-actions › auth › tree › v0.3.1
GitHub - google-github-actions/auth at v0.3.1
- id: 'auth' name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@v0.3.0' with: token_format: 'access_token' workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' service_account: 'my-service-account@my-project.iam.gserviceaccount.com' id_token_audience: 'https://myapp-uvehjacqzq.a.run.app' # required, value depends on target id_token_include_email: true # optional # Example of using the output.
Starred by 1.2K users
Forked by 272 users
Languages   TypeScript 98.8% | JavaScript 1.2% | TypeScript 98.8% | JavaScript 1.2%
🌐
Google Cloud
cloud.google.com › blog › products › identity-security › enabling-keyless-authentication-from-github-actions
Enabling keyless authentication from GitHub Actions | Google Cloud Blog
December 7, 2021 - But now, with GitHub's introduction of OIDC tokens into GitHub Actions Workflows, you can authenticate from GitHub Actions to Google Cloud using Workload Identity Federation, removing the need to export a long-lived JSON service account key.
🌐
GitHub
github.com › google-github-actions › auth › issues › 111
Authenticating to docker using gcloud isn't working after this action · Issue #111 · google-github-actions/auth
January 21, 2022 - - name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@v0' with: credentials_json: '${{ secrets.GOOGLE_CLOUD_TOKEN }}' - name: 'Set up Cloud SDK' uses: 'google-github-actions/setup-gcloud@v0' - name: Docker Login run: "gcloud auth configure-docker --quiet us-central1-docker.pkg.dev" - uses: actions/checkout@v2 - name: Build Docker Image run: "docker build ."
Author   atrauzzi
Top answer
1 of 1
4

As per the error seems to be you have given invalid json file of service account which is not parsed by the Git actions workflow. As per this official github :

  1. (Optional) Create a Google Cloud Service Account. If you already have a Service Account, take note of the email address and skip this step :
    # TODO: replace ${PROJECT_ID} with your value below.             
    gcloud iam service-accounts create "my-service-account" \
      --project "${PROJECT_ID}"
  1. Create a Service Account Key JSON for the Service Account.
# TODO: replace ${PROJECT_ID} with your value below.
gcloud iam service-accounts keys create "key.json" \
  --iam-account "my-service-account@${PROJECT_ID}.iam.gserviceaccount.com"
  1. Upload the contents of this file as a GitHub Actions Secret. Use the name of the GitHub Actios secret as the credentials_json value in the GitHub Actions YAML:
 uses: 'google-github-actions/auth@v2'
  with:
      credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}' # Replace with the name of your GitHub Actions secret

Can you validate the above steps as per your yaml seems to be you need to give correct service account JSON file and also credentials json format need to be taken care.

---------- or else you can also achieve by using Workload Identity Federation through a Service Account by following this official github

Edit : Sinethemba Nontshintshi, achieved this by Changing the approach and used the Workload Identity Federation through a Service Account method to authenticate, by using this guide on how to set it up in GCP and in the YAML file it is passed as follows :

 - name: Authenticate Google Cloud             
   uses: google-github-actions/auth@v2             
   with: 
     service_account: 'your-service account'              
     workload_identity_provider: 'your-workload-identity-provider'
Find elsewhere
Top answer
1 of 1
5

I finally find out !!! BUT I'm not sure in term of security if there is any risk or not so if anyone can advice I'll edit the answer !

What is changing but I'm not sure in term of security is here :

gcloud iam service-accounts add-iam-policy-binding "gh-deploy-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project="${PROJECT_ID}" \
  --role="roles/iam.serviceAccountTokenCreator" \
  --member="principalSet://iam.googleapis.com/projects/MY_PROJECT_NUMBER/locations/global/workloadIdentityPools/github-pool-2/*"


gcloud iam service-accounts add-iam-policy-binding "gh-deploy-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project="${PROJECT_ID}" \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/MY_PROJECT_NUMBER/locations/global/workloadIdentityPools/github-pool-2/*"

I think I don't really get the principalSet option and all the attribute possible so if anyone can advice also on this, I'll be grateful !

Then don't forget to bind your repo to your service account :

gcloud artifacts repositories add-iam-policy-binding npm-repository \
--location asia-east2 --member=serviceAccount:gh-deploy-service-account@${PROJECT_ID}.iam.gserviceaccount.com --role=roles/artifactregistry.writer

And for the github workflow I remove the google-artifactregistry-auth and i use the access_token in the .npmrc file.

Here is the full workflow :

name: Publish Package
on:
  push:
    branches:
      - main

jobs:
  publish:
    timeout-minutes: 10
    runs-on: ubuntu-latest
    permissions:
      contents: "read"
      id-token: "write"
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - uses: actions/setup-node@v2
        with:
          node-version: 16

      - name: Install
        run: npm ci

      - id: "auth"
        name: "Authenticate to Google Cloud"
        uses: "google-github-actions/auth@v0"
        with:
          workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
          service_account: ${{ secrets.SERVICE_ACCOUNT }}
          token_format: 'access_token'

      - name: "Set up Cloud SDK"
        uses: "google-github-actions/setup-gcloud@v0"

      - name: Create .npmrc
        run: |
          cat << EOF > .npmrc
            @example:registry=https://asia-east2-npm.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/npm-repository/
            //asia-east2-npm.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/npm-repository/:_authToken="${{ steps.auth.outputs.access_token }}"
            //asia-east2-npm.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/npm-repository/:always-auth=true
          EOF

      - name: Artifact login
        run: |
          npm publish
🌐
GitHub
github.com › google-github-actions › auth › issues › 104
Failed to generate Google Cloud ID token for service_account · Issue #104 · google-github-actions/auth
January 3, 2022 - Run google-github-actions/auth@v0 with: token_format: id_token workload_identity_provider: projects/XXXX/locations/global/workloadIdentityPools/ABCD/providers/ABCD-provider service_account: abcd@***.iam.gserviceaccount.com id_token_audience: projects/XXXX/locations/global/workloadIdentityPools/ABCD/providers/ABCD-provider create_credentials_file: true cleanup_credentials: true access_token_lifetime: 3600s access_token_scopes: https://www.google***s.com/auth/cloud-platform id_token_include_email: false env: WORKLOAD_IDENTITY_POOL_PROVIDER: projects/XXXX/locations/global/workloadIdentityPools/AB
Author   VikramTiwari
🌐
GitHub
github.com › google-github-actions › auth › blob › main › README.md
auth/README.md at main · google-github-actions/auth
It supports authentication via a Google Cloud Service Account Key JSON and authentication via Workload Identity Federation. Workload Identity Federation is recommended over Service Account Keys as it obviates the need to export a long-lived credential and establishes a trust delegation relationship between a particular GitHub Actions workflow invocation and permissions on Google Cloud.
Author   google-github-actions
🌐
Frankel
blog.frankel.ch › authenticate-google-cloud-github
Securely authenticate to Google Cloud from GitHub
May 1, 2022 - jobs: metrics: runs-on: ubuntu-latest permissions: contents: 'read' id-token: 'write' steps: - uses: actions/checkout@v3 (1) - uses: actions/setup-python@v3 (2) with: python-version: 3.9.10 - uses: 'google-github-actions/auth@v0' (3) with: service_account: '${SERVICE_ACCOUNT_EMAIL}' workload_identity_provider: 'projects/${PROJECT_ID}/locations/global/workloadIdentityPools/${WI_POOL_NAME}/providers/${WI_PROVIDER_NAME}' - run: 'python main.py' (4)
🌐
Stack Overflow
stackoverflow.com › questions › 71799198 › python-gcp-github-action-google-oauth-without-committing-id
Python / GCP - GitHub Action & Google OAuth without committing ID - Stack Overflow
steps: - id: checkout name: Checkout repository uses: actions/checkout@v3 - id: auth name: Authenticate to Google Cloud uses: google-github-actions/auth@v0 with: token_format: 'access_token' access_token_scopes: 'https://www.googleapis.com/auth/youtube.force-ssl' workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' service_account: '[email protected]' # TODO: Install dependencies, program execution...
🌐
GitHub
github.com › google-github-actions › auth › blob › main › docs › TROUBLESHOOTING.md
auth/docs/TROUBLESHOOTING.md at main · google-github-actions/auth
# Ignore generated credentials from google-github-actions/auth gha-creds-*.json · This requires the auth action be v0.6.0 or later.
Author   google-github-actions
🌐
GitHub
github.com › google-github-actions › auth › blob › main › action.yml
auth/action.yml at main · google-github-actions/auth
Authenticate to Google Cloud from GitHub Actions via Workload Identity · Federation or service account keys. · inputs: project_id: description: |- ID of the default project to use for future API calls and invocations.
Author   google-github-actions
🌐
Medium
medium.com › google-cloud › ci-cd-on-github-actions-enabling-keyless-authentication-and-workload-identity-f55efb95343c
CI CD Github Actions enabling Keyless Authentication Workload Identity | Google Cloud - Community
April 29, 2024 - We need to pass the permissions ... google-github-actions/auth action, allows to authenticate the current repository to Google Cloud via the Workload Identity Provider and the associated Service Account...
🌐
GitHub
github.com › marketplace › actions › authenticate-to-google-cloud
Authenticate to Google Cloud · Actions · GitHub Marketplace · GitHub
If you want to use this GitHub Action with Domain-Wide Delegation, you must manually add the "Service Account Token Creator" role onto the external identity. You will also need to customize the access_token_scopes value to correspond to the OAuth scopes required for the API(s) you will access. The following inputs are for generating ID tokens for authenticating to Google Cloud as an output for use in future steps in the workflow.
🌐
GitHub
github.com › google-github-actions › auth › blob › main › docs › EXAMPLES.md
auth/docs/EXAMPLES.md at main · google-github-actions/auth
This example demonstrates authenticating via a Google Cloud Service Account Key JSON. After you export a Google Cloud Service Account Key, insert the value into a GitHub Secret named 'GOOGLE_CREDENTIALS'. jobs: job_id: steps: - uses: 'actions/checkout@v4' - uses: 'google-github-actions/auth@v3' with: credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
Author   google-github-actions
🌐
GitHub
github.com › fastlane › fastlane › discussions › 19869
Support for google-github-actions/auth · fastlane/fastlane · Discussion #19869
- id: 'auth' name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@v0' with: create_credentials_file: true project_id: 'REDACTED' service_account: 'REDACTED@REDACTED.iam.gserviceaccount.com' workload_identity_provider: 'projects/REDACTED/locations/global/workloadIdentityPools/REDACTED/providers/REDACTED' Important: These kinds of credentials work perfectly for interacting with Google Cloud using gcloud, the Google Cloud SDK for Go, and many other tools.
Author   fastlane
🌐
GitHub
github.com › apache › beam › issues › 31491
[Task]: Use google-github-actions/auth@v2 to authenticate with google cloud SA · Issue #31491 · apache/beam
June 4, 2024 - What needs to happen? Currently some workflows use google-github-actions/auth@v2 while others default to built in runner SA for authentication on google cloud platform. We need to specifically add ...
Author   volatilemolotov