I am trying to diagnose why this error keeps coming up with my self hosted machine runner when the cleanup_working_directory
is set to true
.
Error:
Detail: failed to remove working directory before starting task: unlinkat /var/lib/circleci/workdir/%s/.gradle/buildOutputCleanup/buildOutputCleanup.lock: permission denied
Below is my runner config. I believe the permissions are ok.
#!/bin/bash
set -eu pipefail
#-------------------------------------------------------------------------------
#
# CircleCI Runner installation script
# https://circleci.com/docs/runner-installation/
#
#-------------------------------------------------------------------------------
platform="linux/amd64"
CONFIG_PATH="/etc/circleci-runner/circleci-runner-config.yaml" # Determines where Runner config will be stored
SERVICE_PATH="/etc/systemd/system/circleci-runner.service" # Determines where the Runner service definition will be stored
TIMESTAMP=$(date +"%g%m%d-%H%M%S-%3N") # Used to avoid Runner naming collisions
HOST_NAME=$(hostname)
AUTH_TOKEN="${auth_token}" # Auth token for CircleCI
RUNNER_NAME="${runner_name}" # A runner name - this is not the same as the Resource class - keep it short, and only with letters/numbers/dashes/underscores
UNIQUE_RUNNER_NAME="$RUNNER_NAME-$HOST_NAME-$TIMESTAMP"
USERNAME="circleci"
# Create circleci user and working directory
id -u circleci &> /dev/null || sudo adduser --disabled-password --gecos GECOS circleci
# Set up the runner directories
echo "Setting up CircleCI Runner directories"
sudo mkdir -p /var/lib/circleci/workdir
sudo chmod 0750 /var/lib/circleci/workdir
sudo chown -R circleci /var/lib/circleci/workdir
# This enables code to execute root commands on the instance and changes to the system may persist after the job is run
echo "circleci ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers
sudo mkdir -p /etc/circleci-runner && sudo touch /etc/circleci-runner/circleci-runner-config.yaml
sudo chown -R circleci: /etc/circleci-runner
sudo chmod 600 /etc/circleci-runner/circleci-runner-config.yaml
echo "Installing CircleCI Runner for $platform"
curl -s https://packagecloud.io/install/repositories/circleci/runner/script.deb.sh?any=true | sudo bash
sudo apt-get install -y -o Dpkg::Options::="--force-confold" circleci-runner
#-------------------------------------------------------------------------------
# Install the CircleCI runner configuration
# CircleCI Runner will be executing as the configured $USERNAME
# Note the short idle timeout - this script is designed for auto-scaling scenarios - if a runner is unclaimed, it will quit and the system will shut down as defined in the below service definition
#-------------------------------------------------------------------------------
cat << EOF >$CONFIG_PATH
api:
auth_token: $AUTH_TOKEN
runner:
name: $UNIQUE_RUNNER_NAME
command_prefix: ["sudo", "-niHu", "$USERNAME", "--"]
working_directory: /var/lib/circleci/workdir/%s
cleanup_working_directory: true
idle_timeout: 1h
max_run_time: 5h
mode: continuous
logging:
file: /var/log/com.circleci.runner.log
EOF
#-------------------------------------------------------------------------------
# Create the service to override the default one in /lib/systemd/system/
# The service will always restart
#-------------------------------------------------------------------------------
cat << EOF >$SERVICE_PATH
[Unit]
Description=CircleCI Runner
After=network.target
[Service]
ExecStart=/usr/bin/circleci-runner machine -c $CONFIG_PATH
Restart=always
User=circleci
Group=circleci
NotifyAccess=exec
TimeoutStopSec=18300
[Install]
WantedBy = multi-user.target
EOF
#-------------------------------------------------------------------------------
# Configure your runner environment
# This script must be able to run unattended - without user input
#-------------------------------------------------------------------------------
sudo apt update && sudo apt upgrade -y
sudo apt install coreutils curl tar gzip zip unzip -y
# Enable CircleCI Runner service and start it
# This MUST be done last, as it will immediately advertise to the CircleCI server that the runner is ready to use
#-------------------------------------------------------------------------------
sudo systemctl enable circleci-runner && sudo systemctl start circleci-runner
# Check status
sudo systemctl status circleci-runner