2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Github action実行時、 ”unexpected status from HEAD request to <url> 403 Forbidden”エラーの解決

Last updated at Posted at 2024-04-29

状況

  • AWS ECRにGithub actionで作成したDockerイメージをpushしようとしている
  • docker buildxを使用している
  • ローカルからのdocker pushは成功している

解決策

Pushに使用しているAWSロールのポリシーにecr:BatchGetImageを追加する

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": "ecr:GetAuthorizationToken",
			"Effect": "Allow",
			"Resource": "*"
		},
		{
			"Action": [
				"ecr:UploadLayerPart",
				"ecr:PutImage",
				"ecr:InitiateLayerUpload",
				"ecr:CompleteLayerUpload",
				"ecr:BatchCheckLayerAvailability",
				"ecr:BatchGetImage" # 追加
			],
			"Effect": "Allow",
			"Resource": "arn:aws:ecr:YOUR_ACCOUNT:repository/*"
		}
	]
}

理由

Github Actionの定義は以下のようになっているかと思います

docker buildxを使用するとき、docker buildx —push …と言うコマンドを実行しているのですが、そこで追加のAWS ECRパーミッションを必要としてしまっているみたいです。

name: ecr push image

on:
  push:
    branches:
      - main

jobs:
  push-image-to-ecr:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v4

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: ${{ secrets.AWS_REGION }}
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

      - name: Login to Amazon ECR
        uses: aws-actions/amazon-ecr-login@v2
        id: login-ecr

      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v3

     ~~~~~省略~~~~~

      - name: docker build and push ECR
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/arm64 # アーキテクチャを指定
          file: ./dockerfile
          push: true
          provenance: false
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=gha # GithubActionsのCacheを使うという設定
          cache-to: type=gha,mode=max

参考記事

弊社Passinate Geniusでは一緒に働く仲間を募集しています!興味をお持ちいただける方は、ホームページまで!

役に立った場合は、励みになるのでいいねとコメントよろしくお願いします!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?