Getting Authentication error when connecting to another private repo from the github actions workflow

I’m using the self-hosted runner in the GitHub actions workflow. Facing issue when running the terraform init command. It throws the Authentication error. I’m using PAT to connect to the other repo.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> terraform:
runs-on: self-hosted
needs: determine_scope
if: needs.determine_scope.outputs.deploy == 'yes'
environment: ${{ inputs.environment }}
env:
TF_VAR_snowflake_account: ${{ vars.SNOWFLAKE_ACCOUNT }}
TF_VAR_snowflake_username: ${{ vars.SNOWFLAKE_USERNAME }}
TF_VAR_snowflake_role: ${{ vars.SNOWFLAKE_ROLE }}
TF_VAR_snowflake_private_key_passphrase: ${{ secrets.SNOWFLAKE_PASSPHRASE }}
TF_VAR_snowflake_private_key: ${{ secrets.SNOWFLAKE_PRIVATE_KEY }}
defaults:
run:
working-directory: ${{ inputs.path }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Login to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZ_CREDENTIALS }}
- name: JSON Parse
id: parse
env:
AZJSON: ${{ secrets.AZ_CREDENTIALS }}
run: |
ARM_CLIENT_ID=$(echo $AZJSON | jq -r '.["clientId"]')
ARM_CLIENT_SECRET=$(echo $AZJSON | jq -r '.["clientSecret"]')
ARM_TENANT_ID=$(echo $AZJSON | jq -r '.["tenantId"]')
ARM_SUBSCRIPTION_ID=$(echo $AZJSON | jq -r '.["subscriptionId"]')
echo ARM_CLIENT_ID=$ARM_CLIENT_ID >> $GITHUB_ENV
echo ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET >> $GITHUB_ENV
echo ARM_TENANT_ID=$ARM_TENANT_ID >> $GITHUB_ENV
echo ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID >> $GITHUB_ENV
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: latest
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Configure Git Credentials
run: |
git config --global credential.helper store
echo "https://x-access-token:${{ secrets.GIT_PAT }}@github.com" > ~/.git-credentials
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
- name: Terraform Init
run: terraform init -backend-config="${{ inputs.backend_config }}"
</code>
<code> terraform: runs-on: self-hosted needs: determine_scope if: needs.determine_scope.outputs.deploy == 'yes' environment: ${{ inputs.environment }} env: TF_VAR_snowflake_account: ${{ vars.SNOWFLAKE_ACCOUNT }} TF_VAR_snowflake_username: ${{ vars.SNOWFLAKE_USERNAME }} TF_VAR_snowflake_role: ${{ vars.SNOWFLAKE_ROLE }} TF_VAR_snowflake_private_key_passphrase: ${{ secrets.SNOWFLAKE_PASSPHRASE }} TF_VAR_snowflake_private_key: ${{ secrets.SNOWFLAKE_PRIVATE_KEY }} defaults: run: working-directory: ${{ inputs.path }} steps: - name: Checkout Repository uses: actions/checkout@v4 - name: Login to Azure uses: azure/login@v2 with: creds: ${{ secrets.AZ_CREDENTIALS }} - name: JSON Parse id: parse env: AZJSON: ${{ secrets.AZ_CREDENTIALS }} run: | ARM_CLIENT_ID=$(echo $AZJSON | jq -r '.["clientId"]') ARM_CLIENT_SECRET=$(echo $AZJSON | jq -r '.["clientSecret"]') ARM_TENANT_ID=$(echo $AZJSON | jq -r '.["tenantId"]') ARM_SUBSCRIPTION_ID=$(echo $AZJSON | jq -r '.["subscriptionId"]') echo ARM_CLIENT_ID=$ARM_CLIENT_ID >> $GITHUB_ENV echo ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET >> $GITHUB_ENV echo ARM_TENANT_ID=$ARM_TENANT_ID >> $GITHUB_ENV echo ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID >> $GITHUB_ENV - name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: latest - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '16' - name: Configure Git Credentials run: | git config --global credential.helper store echo "https://x-access-token:${{ secrets.GIT_PAT }}@github.com" > ~/.git-credentials env: GIT_PAT: ${{ secrets.GIT_PAT }} - name: Terraform Init run: terraform init -backend-config="${{ inputs.backend_config }}" </code>
  terraform:
    runs-on: self-hosted
    needs: determine_scope
    if: needs.determine_scope.outputs.deploy == 'yes'
    environment: ${{ inputs.environment }}
    env:
      TF_VAR_snowflake_account: ${{ vars.SNOWFLAKE_ACCOUNT }}
      TF_VAR_snowflake_username: ${{ vars.SNOWFLAKE_USERNAME }}
      TF_VAR_snowflake_role: ${{ vars.SNOWFLAKE_ROLE }}
      TF_VAR_snowflake_private_key_passphrase: ${{ secrets.SNOWFLAKE_PASSPHRASE }}
      TF_VAR_snowflake_private_key: ${{ secrets.SNOWFLAKE_PRIVATE_KEY }}

    defaults:
      run:
        working-directory: ${{ inputs.path }}
        
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      
      
      - name: Login to Azure
        uses: azure/login@v2
        with:
          creds: ${{ secrets.AZ_CREDENTIALS }}

      - name: JSON Parse
        id: parse
        env:
          AZJSON: ${{ secrets.AZ_CREDENTIALS }}
        run: |
          ARM_CLIENT_ID=$(echo $AZJSON | jq -r '.["clientId"]')
          ARM_CLIENT_SECRET=$(echo $AZJSON | jq -r '.["clientSecret"]')
          ARM_TENANT_ID=$(echo $AZJSON | jq -r '.["tenantId"]')
          ARM_SUBSCRIPTION_ID=$(echo $AZJSON | jq -r '.["subscriptionId"]')
          echo ARM_CLIENT_ID=$ARM_CLIENT_ID >> $GITHUB_ENV
          echo ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET >> $GITHUB_ENV
          echo ARM_TENANT_ID=$ARM_TENANT_ID >> $GITHUB_ENV
          echo ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID >> $GITHUB_ENV
 
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: latest
        
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Configure Git Credentials
        run: |
          git config --global credential.helper store
          echo "https://x-access-token:${{ secrets.GIT_PAT }}@github.com" > ~/.git-credentials
        env:
          GIT_PAT: ${{ secrets.GIT_PAT }}

      - name: Terraform Init
        run: terraform init -backend-config="${{ inputs.backend_config }}"

Error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>You said:
I'm getting this error in terraform init
│ Error: Failed to download module
│ on main.tf line 124:
│ 124: module "tag_associations" ***
│ Could not download module "tag_associations" (main.tf:124) source code from
│ "git::https://github.com/g/platform-terraform.git?ref=main":
│ error downloading
│ 'https://github.com/g/platform-terraform.git?ref=main':
│ /usr/bin/git exited with 128: Cloning into
'.terraform/modules/tag_associations'...
│ remote: Invalid username or password.
│ fatal: Authentication failed for
'https://github.com/g/platform-terraform.git/'
</code>
<code>You said: I'm getting this error in terraform init │ Error: Failed to download module │ │ on main.tf line 124: │ 124: module "tag_associations" *** │ │ Could not download module "tag_associations" (main.tf:124) source code from │ "git::https://github.com/g/platform-terraform.git?ref=main": │ error downloading │ 'https://github.com/g/platform-terraform.git?ref=main': │ /usr/bin/git exited with 128: Cloning into │ '.terraform/modules/tag_associations'... │ remote: Invalid username or password. │ fatal: Authentication failed for │ 'https://github.com/g/platform-terraform.git/' </code>
You said:
I'm getting this error in terraform init

│ Error: Failed to download module
│ 
│   on main.tf line 124:
│  124: module "tag_associations" ***
│ 
│ Could not download module "tag_associations" (main.tf:124) source code from
│ "git::https://github.com/g/platform-terraform.git?ref=main":
│ error downloading
│ 'https://github.com/g/platform-terraform.git?ref=main': 
│ /usr/bin/git exited with 128: Cloning into
│ '.terraform/modules/tag_associations'...
│ remote: Invalid username or password.
│ fatal: Authentication failed for
│ 'https://github.com/g/platform-terraform.git/'

When I use the Github-hosted runner (ubuntu-latest), it works fine. But when using the self-hosted runner, it throws above error.

Recognized by CI/CD Collective

The issue is likely caused by your self hosted runner using a different user. What does ~ resolve to?

A potential solution is to configure git this way

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
</code>
<code>git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/" </code>
git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật