|
| 1 | +name: 'artifact-backup' |
| 2 | +description: 'Backup release artifacts to S3' |
| 3 | +inputs: |
| 4 | + release_tag: |
| 5 | + description: 'Tag of the release' |
| 6 | + required: true |
| 7 | + artifact_path: |
| 8 | + description: 'Path to the generated artifact' |
| 9 | + required: true |
| 10 | + aws_account_id: |
| 11 | + description: 'AWS account ID (most probably you need not provide this)' |
| 12 | + required: false |
| 13 | + default: '178185592318' |
| 14 | + aws_region: |
| 15 | + description: 'AWS region (most probably you need not provide this)' |
| 16 | + required: false |
| 17 | + default: 'us-east-1' |
| 18 | + bucket_name: |
| 19 | + description: 'S3 bucket name (most probably you need not provide this)' |
| 20 | + required: false |
| 21 | + default: 'frtos-artifact-bckup-us-east-1-alpha' |
| 22 | + |
| 23 | + |
| 24 | +runs: |
| 25 | + using: "composite" |
| 26 | + steps: |
| 27 | + - name: Extract repository name |
| 28 | + id: repo-name |
| 29 | + shell: bash |
| 30 | + run: | |
| 31 | + REPO_NAME="${GITHUB_REPOSITORY#*/}" |
| 32 | + echo "name=$REPO_NAME" >> $GITHUB_OUTPUT |
| 33 | + echo "Repository: $GITHUB_REPOSITORY" |
| 34 | + echo "Repository Owner: $GITHUB_REPOSITORY_OWNER" |
| 35 | + echo "Repository Name: $REPO_NAME" |
| 36 | +
|
| 37 | + - name: Get temporary AWS credentials |
| 38 | + uses: aws-actions/configure-aws-credentials@v4 |
| 39 | + with: |
| 40 | + aws-region: ${{ inputs.aws_region }} |
| 41 | + role-to-assume: arn:aws:iam::${{ inputs.aws_account_id }}:role/GHRole-${{ github.repository_owner }}-${{ steps.repo-name.outputs.name }} |
| 42 | + role-duration-seconds: 900 |
| 43 | + unset-current-credentials: true |
| 44 | + |
| 45 | + - name: Add file to s3 bucket |
| 46 | + env: |
| 47 | + BUCKET_NAME: ${{ inputs.bucket_name }} |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + # Get the artifact file name and extension |
| 51 | + FILE=${{ inputs.artifact_path }} |
| 52 | + FILENAME=$(basename "$FILE") |
| 53 | + EXTENSION="${FILENAME##*.}" |
| 54 | + BASENAME="${FILENAME%.*}" |
| 55 | +
|
| 56 | + # Rename with timestamp |
| 57 | + TIMESTAMP="$(date +%Y%m%d%H%M%S)" |
| 58 | + NEW_FILENAME="${BASENAME}.${TIMESTAMP}.${EXTENSION}" |
| 59 | +
|
| 60 | + # Upload to S3 without overwriting |
| 61 | + if ! aws s3api put-object \ |
| 62 | + --bucket ${{ env.BUCKET_NAME }} \ |
| 63 | + --key ${{ github.repository }}/${{ inputs.release_tag }}/${NEW_FILENAME} \ |
| 64 | + --body ${{ inputs.artifact_path }} \ |
| 65 | + --if-none-match "*"; then |
| 66 | + echo "::error::Failed to upload artifact to S3" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + echo "Successfully uploaded artifact to S3" |
| 71 | +
|
| 72 | + - name: Upload SHA256 of the artifact |
| 73 | + env: |
| 74 | + BUCKET_NAME: ${{ inputs.bucket_name }} |
| 75 | + shell: bash |
| 76 | + run: | |
| 77 | + # Calculate and upload SHA256 hash with platform compatibility |
| 78 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 79 | + # macOS uses shasum |
| 80 | + HASH=$(shasum -a 256 "${{ inputs.artifact_path }}" | awk '{print $1}') |
| 81 | + else |
| 82 | + # Linux uses sha256sum |
| 83 | + HASH=$(sha256sum "${{ inputs.artifact_path }}" | awk '{print $1}') |
| 84 | + fi |
| 85 | + |
| 86 | + echo "$HASH" > SHA256.txt |
| 87 | + |
| 88 | + echo "Uploading SHA256.txt to S3..." |
| 89 | + if ! aws s3api put-object \ |
| 90 | + --bucket ${{ env.BUCKET_NAME }} \ |
| 91 | + --key ${{ github.repository }}/${{ inputs.release_tag }}/SHA256.txt \ |
| 92 | + --body SHA256.txt \ |
| 93 | + --if-none-match "*"; then |
| 94 | + echo "::error::Failed to upload SHA256 hash to S3" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + |
| 98 | + echo "Successfully uploaded SHA256 hash to S3" |
0 commit comments