Replace folder using upload-cloud-storage action

Viewed 733

I have the following workflow in github actions:

name: Move artifacts

on:
  push:
    branches:
      - main

jobs:
  move_artifacts:
    name: Move artifacts
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - id: upload-files
        uses: google-github-actions/upload-cloud-storage@main
        with:
          credentials: ${{ secrets.gcp_credentials }}
          path: .
          destination: my-bucket/my-folder/

And, when this workflow is triggered, the files from the repo are copied into GCS, but, if there were other files in the GCS folder, then these are not deleted.

How can I make an upload in such a way that the contents of my-bucket/my-folder is exactly the content of the repo? How can I delete the files that are not in the repo anymore?

2 Answers

You could try to delete files from bucket using gsutil as follows:

- name: Set up Cloud SDK
  uses: google-github-actions/setup-gcloud@master
  with:
    project_id: ${{ secrets.GCP_PROJECT_ID }}
    service_account_key: ${{ secrets.GCP_SA_KEY }}
    export_default_credentials: true


- name: Delet files from bucket
  run: gsutil rm gs://my-bucket/my-folder/**

You could use the following Github Action to have the same files in the destination as in the source:

name: Copy files to GCS
on:
  push:
    branches: [main]

jobs: 
  copy-files-gcs:
    name: Copy directory to GCS
    runs-on: ubuntu-latest

    # Add "id-token" with the intended permissions.
    permissions:
      contents: 'read'
      id-token: 'write'   
  
    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - id: 'auth'
      uses: 'google-github-actions/auth@v0'
      with:
        credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

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

    - name: 'Use gcloud CLI'
      run: 'gsutil -m rsync -R -d kavach gs://<your-bucket>'

The key for syncing the files in destination folder as you are looking for is the -d flag for rsync which deletes the files in the destination that are not present in the source.

Related