How can I delete a file from my GitLab repository using gitlab-ci.yml
?
I already used a sh command in my gitlab-ci.yml
file but it didn't work.
Here's the command I tried: sh 'rm -rf [myfile]'
.
Does anyone have another idea or another suggestion?
Answer
When you run a script command in a gitlab runner it is only working on the local files. i.e. rm -rf [myfile]
is only going to delete the local file.
If you want the runner to delete the file from the repository, you will have to script commands to update the repo like any other git user. Perhaps something like this:
delete-and-push:
stage: cleanup
image: alpine:latest
before_script:
- apk add git openssh
- git config --global user.email "
This is making use of a CI_PUSH_TOKEN
from a CI/CD variable to authenticate. The value would be a Personal Access Token or a Project Access Token with write_repository
scope.