diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index e4faf24..e3456d6 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -23,11 +23,26 @@ jobs: with: version: 'latest' + - name: Assume AWS Marketplace ECR role + id: ecr-credentials + uses: aws-actions/configure-aws-credentials@v6.3.0 + with: + role-to-assume: arn:aws:iam::588738614642:role/aws-marketplace-ecr + aws-region: us-east-1 + translate-env-variables: false + output-credentials: true + output-env-credentials: false + - name: Run deploy script env: + # Existing credentials publish the Helm repository to S3. AWS_ACCESS_KEY_ID: ${{ vars.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ env.AWS_REGION }} + # dev/ecr.sh uses the OIDC role credentials for Marketplace uploads. + ECR_AWS_ACCESS_KEY_ID: ${{ steps.ecr-credentials.outputs.aws-access-key-id }} + ECR_AWS_SECRET_ACCESS_KEY: ${{ steps.ecr-credentials.outputs.aws-secret-access-key }} + ECR_AWS_SESSION_TOKEN: ${{ steps.ecr-credentials.outputs.aws-session-token }} run: | chmod +x dev/deploy.sh ./dev/deploy.sh diff --git a/dev/aws-marketplace-ecr-trust-policy.json b/dev/aws-marketplace-ecr-trust-policy.json new file mode 100644 index 0000000..16a3027 --- /dev/null +++ b/dev/aws-marketplace-ecr-trust-policy.json @@ -0,0 +1,32 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "GitHubActions", + "Effect": "Allow", + "Principal": { + "Federated": "arn:aws:iam::588738614642:oidc-provider/token.actions.githubusercontent.com" + }, + "Action": "sts:AssumeRoleWithWebIdentity", + "Condition": { + "StringEquals": { + "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" + }, + "StringLike": { + "token.actions.githubusercontent.com:sub": [ + "repo:pgdogdev/helm:*", + "repo:pgdogdev/pgdog:*" + ] + } + } + }, + { + "Sid": "AccountAccess", + "Effect": "Allow", + "Principal": { + "AWS": "arn:aws:iam::588738614642:root" + }, + "Action": "sts:AssumeRole" + } + ] +} diff --git a/dev/deploy.sh b/dev/deploy.sh index 15d1412..7baaf4e 100755 --- a/dev/deploy.sh +++ b/dev/deploy.sh @@ -1,12 +1,14 @@ #!/bin/bash -set -e +set -eo pipefail SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$SCRIPT_PATH/.." CHART_NAME="pgdog" DEPLOY_DIR="$PROJECT_ROOT/deploy" +PACKAGE_DIR="$(mktemp -d)" +trap 'rm -rf "$PACKAGE_DIR"' EXIT echo "Creating deploy directory..." rm -rf "$DEPLOY_DIR" @@ -16,7 +18,8 @@ aws s3 sync s3://pgdog-helm "$DEPLOY_DIR" echo "Packaging Helm chart..." cd "$PROJECT_ROOT" -helm package . --destination "$DEPLOY_DIR" +helm package . --destination "$PACKAGE_DIR" +cp "$PACKAGE_DIR/$CHART_NAME-"*.tgz "$DEPLOY_DIR/" echo "Creating/updating Helm repository index..." helm repo index "$DEPLOY_DIR" --url "https://helm.pgdog.dev" @@ -25,3 +28,5 @@ echo "Deployment artifacts created in $DEPLOY_DIR/" ls -lsha "$DEPLOY_DIR/" aws s3 sync "$DEPLOY_DIR" s3://pgdog-helm + +bash "$SCRIPT_PATH/ecr.sh" "$PACKAGE_DIR/$CHART_NAME-"*.tgz diff --git a/dev/ecr-push-policy.json b/dev/ecr-push-policy.json new file mode 100644 index 0000000..80a2f47 --- /dev/null +++ b/dev/ecr-push-policy.json @@ -0,0 +1,24 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "AuthenticateToECR", + "Effect": "Allow", + "Action": "ecr:GetAuthorizationToken", + "Resource": "*" + }, + { + "Sid": "PushImagesAndHelmCharts", + "Effect": "Allow", + "Action": [ + "ecr:BatchCheckLayerAvailability", + "ecr:BatchGetImage", + "ecr:InitiateLayerUpload", + "ecr:UploadLayerPart", + "ecr:CompleteLayerUpload", + "ecr:PutImage" + ], + "Resource": "arn:aws:ecr:us-east-1:709825985650:repository/pgdog/pgdog" + } + ] +} diff --git a/dev/ecr.sh b/dev/ecr.sh new file mode 100644 index 0000000..1952d7c --- /dev/null +++ b/dev/ecr.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -eo pipefail + +CHART_PACKAGE="${1:?Usage: ecr.sh }" +ECR_REGISTRY="709825985650.dkr.ecr.us-east-1.amazonaws.com" +ECR_REGION="us-east-1" + +if [ -n "${ECR_AWS_ACCESS_KEY_ID:-}" ]; then + export AWS_ACCESS_KEY_ID="$ECR_AWS_ACCESS_KEY_ID" + export AWS_SECRET_ACCESS_KEY="${ECR_AWS_SECRET_ACCESS_KEY:?Missing ECR role secret access key}" + export AWS_SESSION_TOKEN="${ECR_AWS_SESSION_TOKEN:?Missing ECR role session token}" +fi + +echo "Authenticating to AWS Marketplace ECR..." +aws ecr get-login-password --region "$ECR_REGION" | \ + helm registry login --username AWS --password-stdin "$ECR_REGISTRY" + +echo "Pushing Helm chart to AWS Marketplace ECR..." +helm push "$CHART_PACKAGE" "oci://$ECR_REGISTRY/pgdog"