There is train, bus, and airplane service from Madrid to Pamplona. The most comfortable is the train. The ALSA bus has the most frequent service and some buses pick up at the Madrid airport T4. There is seasonal limited CONDA bus service from Pamplona to SJPdP.
The other way is to go to Irun or Hendaye by Renfe train. Then cross the border (if the train doesn't terminate in Hendaye) and take an SNCF train from Hendaye to Bayonne. There is another SNCF train/bus service from Bayonne to SJPdP - typically 4 times per day all year. There are a lot of variables on this route including bus from Madrid to San Sebastian, Euskotren from San Sebastian to Hendaye, Transports64 bus to Bayonne, and the San Sebastian Airport is located 3km from Irun Renfe station.